Thursday, August 23, 2012

Call WebService using Javascript Soap request.

Use the following line of code to access webservice in Javascript

function WSRetrieveMultipleByMultipleCondition(crmEntity, resultAttribute, queryAttributeandValue) {
var resultXml,arrayqueryAttribute,xmlAttributeandValue,arrayresultAttribute,xmlresultAttribute;
arrayqueryAttribute = queryAttributeandValue.split(",");
    for (var i = 0; i < arrayqueryAttribute.length; i++) {
        var Attribute,Value,arrayAttributeandValue;
        AttributeandValue=arrayqueryAttribute[i].split("|");
        Attribute=AttributeandValue[0];
        Value=AttributeandValue[1];       
        xmlAttributeandValue +=""+
        ""+Attribute+""+
        "Like"+
        ""+
        ""+Value+""+
        "
"+
        "
"
    }

arrayresultAttribute = resultAttribute.split(",");
    for (var i = 0; i < arrayresultAttribute.length; i++) {
        var AttributeName;
        AttributeName=arrayresultAttribute[i];
        xmlresultAttribute +="" + AttributeName + ""
    }   
   
var oXMLSoapMsg = ""+
"" xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'"+
" xmlns:xsd='http://www.w3.org/2001/XMLSchema'>"+
GenerateAuthenticationHeader().toString()+
""+
""+
"" xsi:type='q1:QueryExpression'>"+
""+crmEntity+""+
""+
""+
xmlresultAttribute+
"
"+
"
"+
"false"+
""+
"And"+
""+
xmlAttributeandValue +
"
"+
"
"+
"
"+
"
"+
"
"+
"
";
var oXMLHTTPReq = new ActiveXObject("Msxml2.XMLHTTP");
oXMLHTTPReq.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
oXMLHTTPReq.setRequestHeader("SOAPAction","http://schemas.microsoft.com/crm/2007/WebServices/RetrieveMultiple");
oXMLHTTPReq.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
oXMLHTTPReq.setRequestHeader("Content-Length", oXMLSoapMsg.length);
oXMLHTTPReq.send(oXMLSoapMsg);
resultXml = oXMLHTTPReq.responseXML;

var errorCount = resultXml.selectNodes('//error').length;
if (errorCount != 0){
var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
alert(msg);
}
else{
    return resultXml;
}
}

//You can call the above function using the below line of code
function UserRole(checkrolename)
{
    //Get Current User Roles RoleID
    var currentUserRoles = Xrm.Page.context.getUserRoles();
    var roleidName='';
    for (var i = 0; i < currentUserRoles.length; i++) {
        var userRoleId = currentUserRoles[i];
        var resultAttribute, queryAttributeandValue;
        resultAttribute="name";
        queryAttributeandValue="roleid|"+userRoleId;
        var resultXml=WSRetrieveMultipleByMultipleCondition("role",resultAttribute,queryAttributeandValue);
        var buNodes = resultXml.selectNodes("//BusinessEntity/q1:name");
        var NodeExist = buNodes.length;
        if (NodeExist >=1)
        {
            //alert('Role Name - ' + buNodes[0].text);
            roleidName= buNodes[0].text;
            if(roleidName==checkrolename)
            {break;}
        }
    }
    return roleidName;
}

How to Create a parent child optionSet/PickList in MS CRM 2011?

To make a Parent Child OptionSet or PickList in MS CRM 2011 use the following line of code:

Also You can change values in second OptionSet based on first OptionSet Value

function PicklistOneOnchange() {
    var picklistOneName = "new_healthplantype"; //name of the first picklist
    var picklistTwoName = "new_healthinsuranceplan";  //name of the picklist with dynamic values
   
    var picklistOne = Xrm.Page.getControl(picklistOneName);
    var picklistOneAttribute = picklistOne.getAttribute();
   
    var picklistTwo = Xrm.Page.getControl(picklistTwoName);
    var picklistTwoAttribute = picklistTwo.getAttribute();
       
       var picklistOneSelectedOption = picklistOneAttribute.getSelectedOption();
   
    var picklistOneSelectedText = "";   
    if (picklistOneSelectedOption != null)
    {
        picklistOneSelectedText = picklistOneSelectedOption.text;
    }

    //This "if" statement stores the original values from the dynamic picklist.
    //Very important if the user hasn't made a selection on the first picklist or if the selection changes
    if (picklistTwo.flag == true)
    {
        picklistTwo.clearOptions();
        var origOptions = picklistTwo.originalPicklistValues;
       
        for (var i = origOptions.length - 1; i >= 0; i--)
        {
            if(origOptions[i].text != "")
            {
                picklistTwo.addOption(origOptions[i]);
            }
        }       
    }
    else
    {       
        picklistTwo.originalPicklistValues = picklistTwoAttribute.getOptions();
        picklistTwo.flag = true;
    }

    if (picklistOneSelectedText != null && picklistOneSelectedText != "")
    {       
        var picklistTwoOptions = picklistTwoAttribute.getOptions();
        for (var i = picklistTwoOptions.length - 1; i >= 0; i--) { 
           
            if (picklistTwoOptions[i].value != null && picklistTwoOptions[i].value != "") {
                var optionText = picklistTwoOptions[i].text;
                var optionValue = picklistTwoOptions[i].value;
               
                //BEGIN: If the picklist is set to HMO
                if(picklistOneSelectedText == "HMO")
                {                           
                    //Remove these values
                    if (optionText == "PPO 500" || optionText == "PPO 750" || optionText == "POS Silver" || optionText == "POS Gold")
                    {
                        picklistTwo.removeOption(optionValue);
                    }

                }
                //END: HMO Selection
               
                //BEGIN: If the picklist is set to PPO
                if(picklistOneSelectedText == "PPO")
                {
                    //Remove these values
                    if (optionText == "HMO 5K" || optionText == "HMO 7K" || optionText == "POS Silver" || optionText == "POS Gold")
                    {
                        picklistTwo.removeOption(optionValue);
                    }

                }           
                //END: PPO Selection
               
                //BEGIN: If the picklist is set to POS
                if(picklistOneSelectedText == "POS")
                {
                    //Remove these values
                    if (optionText == "HMO 5K" || optionText == "HMO 7K" || optionText == "PPO 500" || optionText == "PPO 750")
                    {
                        picklistTwo.removeOption(optionValue);
                    }

                }       
                //END: POS Selection
            }
        }
    }
   
}

How to make Read Only Tab in CRM? How to Make read only all the attribute in a specified Tab?

Use the following line of code to make all the fields within a tab as read only or disabled.

// get the div element corresponding to tab, first tab would have index tab0, second as tab1 and so on ..
var el=document.getElementById('tab2')

// create a recursive function to Disable Attributes
function toggleDisabled(el) {
try {
el.disabled = el.disabled ? false : true;
}
catch(E){}
if (el.childNodes && el.childNodes.length > 0) {
for (var x = 0; x < el.childNodes.length; x++) {
toggleDisabled(el.childNodes[x]);
}
}
}

// create a recursive function to Enable Attributes
function toggleEnabled(el) {
try {
el.disabled = el.disabled ? true : false;
}
catch(E){}
if (el.childNodes && el.childNodes.length > 0) {
for (var x = 0; x < el.childNodes.length; x++) {
toggleEnabled(el.childNodes[x]);
}
}
}

Split the String values with a special character in MS Flow to convert this into Array

 Many times we have a requirement to prepare the Mailing address for some of the documents, suppose there are Address Line1, Address Line2, ...