Monthly Archives: March 2014

Create N to N relationship in Javascript (Associate and Dissasociate)

To associate and dissasocite N to N relationships I found this very usefull libraries thanks to Dave Berry.

To use it we simply have to call:


//AssociateEntities(relationshipSchemaName, entity1SchemaName, entity1KeyValue, entity2SchemaName, entity2KeyValue, skipCheck)

AssociateEntities("lema_lema_sitevisit_epa_noncompliance", "epa_noncompliance", CIID, "lema_sitevisit", createResponse, true);

Find here the Associate Library. As usual I upload it as a .doc file. Rename it to .js to view it.

Advertisements

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.