Wednesday 30 January 2013

Count Sub-grid records in entity in CRM 2011 using javascript



function requisitionproducts() {
//Get Current record Id
    var Guid = Xrm.Page.data.entity.getId();
    var fetchXml =
    "<fetch mapping='logical'>" +
       "<entity name='quotedetail'>" + //SubGrid entity name
    //Condition to Check the subGrid record is associated with current record      
          "<filter>" +  
             "<condition attribute='quoteid' operator='eq' value='" + Guid + "' />" +
          "</filter>" +
       "</entity>" +
    "</fetch>";
   
    var fetchedRecord = XrmServiceToolkit.Soap.Fetch(fetchXml)
    if (fetchedRecord.length > 0) {
        return true;
    }
}


After adding this web-resource you need to add the following files to your form page Libraries:

  1. Json
  2. jquery
  3. xrmservicetoolkit


Monday 21 January 2013

Get Current user business unit in CRM 2011 using javascript


function GetBusinessUnit() {  
        var request = "<request i:type='b:WhoAmIRequest' xmlns:a='http://schemas.microsoft.com/xrm/2011/Contracts' xmlns:b='http://schemas.microsoft.com/crm/2011/Contracts'>" +
                            "<a:Parameters xmlns:c='http://schemas.datacontract.org/2004/07/System.Collections.Generic' />" +
                            "<a:RequestId i:nil='true' />" +
                            "<a:RequestName>WhoAmI</a:RequestName>" +
                          "</request>";
        var resultXml = XrmServiceToolkit.Soap.Execute(request);
        var buid = resultXml.getElementsByTagName("a:Results")[0].childNodes[1].childNodes[1].text;
        var cols = ["name"];
        var retrievedContact = XrmServiceToolkit.Soap.Retrieve("businessunit", buid, cols);
        var businessUnitName = retrievedContact.attributes['name'].value;
       alert(businessUnitName);
    }
After that you need to add the following files to the Form Libraries.

  1. Jquery
  2. Json2
  3. XrmServiceToolkit
Download these files from the following link: XrmServiceToolkit

Get Partylist/Lookup field id in CRM 2011 using Javascript


var lookup = new Array();
        lookup = Xrm.Page.getAttribute("to").getValue();
        for (var indxAttendees = 0; indxAttendees < lookup.length; indxAttendees++) {
        var lookupId = lookup[indxAttendees].id;
        var lookupName=lookup[indxAttendees].name;
        var lookupType=lookup[indxAttendees].entityType;
}

Tuesday 8 January 2013

Hide section/field based on related entity/records in CRM 2011 using javascript


function retrieveworkorder() {

    var ordernumber = Xrm.Page.data.entity.getId();
    if (ordernumber != null) {
        var fetchXml =
                    "<fetch mapping='logical'>" +
                       "<entity name='jmh_workorder'>" +
                          "<attribute name='jmh_workordernumber' />" +
                          "<filter>" +
                             "<condition attribute='jmh_bespokeorderid' operator='eq' value='" + ordernumber + "' />" +
                          "</filter>" +
                       "</entity>" +
                    "</fetch>";

        var retrievedOrder = XrmServiceToolkit.Soap.Fetch(fetchXml);
//        alert(retrievedOrder.length);
        if (retrievedOrder.length > 0) {
            Xrm.Page.ui.tabs.get("general").sections.get("workorder").setVisible(false);
        }
    }

}

Reference: http://xrmservicetoolkit.codeplex.com/wikipage?title=Soap%20Functions 

Monday 7 January 2013

Get Business Unit in CRM 2011 using javascript

  • Add the following script to your web resources:
function GetBusinessUnit() {
    var currentstore = Xrm.Page.getAttribute('jmh_store').getValue();
    if (currentstore == null) {
        var request = "<request i:type='b:WhoAmIRequest' xmlns:a='http://schemas.microsoft.com/xrm/2011/Contracts' xmlns:b='http://schemas.microsoft.com/crm/2011/Contracts'>" +
                            "<a:Parameters xmlns:c='http://schemas.datacontract.org/2004/07/System.Collections.Generic' />" +
                            "<a:RequestId i:nil='true' />" +
                            "<a:RequestName>WhoAmI</a:RequestName>" +
                          "</request>";
        var resultXml = XrmServiceToolkit.Soap.Execute(request);
        var buid = resultXml.getElementsByTagName("a:Results")[0].childNodes[1].childNodes[1].text;
        var cols = ["name"];
        var retrievedContact = XrmServiceToolkit.Soap.Retrieve("businessunit", buid, cols);
        var buName = retrievedContact.attributes['name'].value;
        SetLookupValue('jmh_store', buid, buName, 'businessunit');
    }

    function SetLookupValue(fieldName, id, name, entityType) {
        if (fieldName != null) {
            var lookupValue = new Array();
            lookupValue[0] = new Object();
            lookupValue[0].id = id;
            lookupValue[0].name = name;
            lookupValue[0].entityType = entityType;
            Xrm.Page.getAttribute(fieldName).setValue(lookupValue);
        }
    }
}


Note : Add XrmServiceToolkit in the page library
Reference: http://xrmservicetoolkit.codeplex.com/wikipage?title=Soap%20Functions