Query N to N relationship in Javascript

Using this function we will be able to query N to N relationshipt in JS


//Main function

function Main()
{
   var sectors = getSectorsFromAnnualPlan(AnnualPlanId);

    if (sectors.length > 0) {
        logMessage += 'For each Sector:\n';
    }

    for (var k = 0; k < sectors.length; k++) {
        var sectorguid = sectors[k].childNodes[0].text;
        var sectorName = sectors[k].childNodes[1].text;
}

}

//Calling function
function getSectorsFromAnnualPlan(annualplanid) {

    var EntityName = "lema_sector";
    var AttributeList = new Array();
    AttributeList[0] = "lema_sectorid";
    AttributeList[1] = "lema_name";
    AttributeList[2] = "lema_sectoroption";
    var RetrieveDistinct = false;
    var LinkFromEntityName = "lema_sector";
    var LinkFromAttributeName = "lema_sectorid";
    var LinkToEntityName = "lema_annualsitevisitsplan_lema_sector";
    var LinkToAttributeName = "lema_sectorid"
    var LinkEntityFilterAttributeName = "lema_annualsitevisitsplanid";
    var LinkEntityFilterAttributeValue = annualplanid; //set the initial entity GUI

    var responseXml = RetrieveMultipleFromLinkEntity(EntityName, AttributeList, false, LinkFromEntityName
                                                , LinkFromAttributeName, LinkToEntityName, LinkToAttributeName
                                                , LinkEntityFilterAttributeName, LinkEntityFilterAttributeValue);

    return responseXml;
}

//Retreive Function

function RetrieveMultipleFromLinkEntity(entityName, arrColumnName, enableDistinct, linkFromEntityName, linkFromAttributeName, linkToEntityName, linkToAttributeName, linkEntityFilterAttributeName, linkEntityFilterAttributeValue) {

    if ((entityName != null) && (entityName.length > 0) && (arrColumnName != null) && (arrColumnName.length > 0) &&
    (linkFromEntityName != null) && (linkFromEntityName.length > 0) && (linkFromAttributeName != null) && (linkFromAttributeName.length > 0) &&
    (linkToEntityName != null) && (linkToEntityName.length > 0) && (linkToAttributeName != null) && (linkToAttributeName.length > 0) &&
    (linkEntityFilterAttributeName != null) && (linkEntityFilterAttributeName.length > 0) && (linkEntityFilterAttributeValue != null) && (linkEntityFilterAttributeValue.length > 0)) {

        var ColumnsSetAttributes = '';
        var IsDistinctValue = false;

        // Build columns to be retrieved
        for (var i = 0; i < arrColumnName.length; i++) {
            ColumnsSetAttributes += " " + arrColumnName[i] + ""
        }

        //Set Distinct value
        if (enableDistinct != null && typeof (enableDistinct) != "undefined" && (enableDistinct == "true" || enableDistinct == true)) {
            IsDistinctValue = true;
        }

        // Build link entity filter
        var LinkFilterCondition = " " +
                " " + linkEntityFilterAttributeName + "" +
                " Equal" +
                " " +
                " " + linkEntityFilterAttributeValue + "" +
                " " +
                " ";

        var xml = "<!--?xml version=\"1.0\" encoding=\"utf-8\"?-->" +
         "" +
         GenerateAuthenticationHeader() +
         " " +
         " " +
         " " +
         " " + entityName + "" +
         " " +
         " " +
         ColumnsSetAttributes +
         " " +
         " " +
         " " + IsDistinctValue + "" +
         " " +
         " " +
         " " + linkFromAttributeName + "" +
         " " + linkFromEntityName + "" +
         " " + linkToEntityName + "" +
         " " + linkToAttributeName + "" +
         " Inner" +
         " " +
         " And" +
         " " +
         LinkFilterCondition +
         " " +
         " " +
         " " +
         " " +
         " " +
         " " +
         " " +
         "";

        var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
        xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
        xmlHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple");
        xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
        xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
        var oXml;

        try {
            xmlHttpRequest.send(xml);
            oXml = xmlHttpRequest.responseXML;

            // Check for errors.
            if (oXml != null) {
                var errorCount = oXml.selectNodes('//error').length;
                if (errorCount != 0) {
                    var msg = oXml.selectSingleNode('//description').nodeTypedValue;
                    alert(msg);
                }
                else {
                    //return the Business Entity node
                    return oXml.selectNodes("//BusinessEntity");
                }
            }
            else {
                alert("Error: No response received.");
            }
        }
        catch (e) {
            if (oXml != null) {
                var err = oXml.getElementsByTagName("description")[0].childNodes[0].nodeValue;
                alert("Error: " + e.description + ": " + err);
            }
            else {
                alert("Error: Unexpected error occurred.");
            }
        }
    }
    return null;
}

The fetch code is not complete so here NtoNquery you can find the full code in an attachment. As usuall WordPress dont allow me to upload .js files so donwload it and rename it to .js or .txt to view it.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s