Saturday 31 March 2012

Create connection entity record using JavaScript SOAP request in MS CRM 2011 !!!


We had a requirement, create a ribbon button on account form to create connection entity record to connect current user with the account using existing role once ribbon button pressed. here is the javascript function to create reccord :-

function CreateConnectionRecord(UserID, Accountid, RoleID) {

var request = "<s:Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'>" +
"<s:Body>" +
"<Create xmlns='http://schemas.microsoft.com/xrm/2011/Contracts/Services' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>" +
"<entity xmlns:a='http://schemas.microsoft.com/xrm/2011/Contracts'>" +
"<a:Attributes xmlns:b='http://schemas.datacontract.org/2004/07/System.Collections.Generic'>" +
"<a:KeyValuePairOfstringanyType>" +
"<b:key>record2id</b:key>" +
"<b:value i:type='a:EntityReference'>" +
"<a:Id>" + UserID + "</a:Id>" +
"<a:LogicalName>systemuser</a:LogicalName>" +
"<a:Name i:nil='true'/>" +
"</b:value>" +
" </a:KeyValuePairOfstringanyType>" +

"<a:KeyValuePairOfstringanyType>" +
"<b:key>record1id</b:key>" +
"<b:value i:type='a:EntityReference'>" +
"<a:Id>" + Accountid + "</a:Id>" +
"<a:LogicalName>account</a:LogicalName>" +
"<a:Name i:nil='true'/>" +
"</b:value>" +
" </a:KeyValuePairOfstringanyType>" +

"<a:KeyValuePairOfstringanyType>" +
"<b:key>record2roleid</b:key>" +
"<b:value i:type='a:EntityReference'>" +
"<a:Id>" + RoleID + "</a:Id>" +
"<a:LogicalName>connectionrole</a:LogicalName>" +
"<a:Name i:nil='true'/>" +
"</b:value>" +
" </a:KeyValuePairOfstringanyType>" +

"<a:KeyValuePairOfstringanyType>" +
"<b:key>record1roleid</b:key>" +
"<b:value i:type='a:EntityReference'>" +
"<a:Id>" + RoleID + "</a:Id>" +
"<a:LogicalName>connectionrole</a:LogicalName>" +
"<a:Name i:nil='true'/>" +
"</b:value>" +
" </a:KeyValuePairOfstringanyType>" +

"</a:Attributes>" +
"<a:EntityState i:nil='true'/>" +
"<a:FormattedValues xmlns:b='http://schemas.datacontract.org/2004/07/System.Collections.Generic'/>" +
"<a:Id>00000000-0000-0000-0000-000000000000</a:Id>" +
"<a:LogicalName>connection</a:LogicalName>" +
"<a:RelatedEntities xmlns:b='http://schemas.datacontract.org/2004/07/System.Collections.Generic'/>" +
"</entity>" +
"</Create>" +
"</s:Body>" +
"</s:Envelope>";

ExecuteRequest(request, "Create");

}

function ExecuteRequest(_XML, Message) {
    try {
        var _ResultXML = null;
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.open("POST", Xrm.Page.context.getServerUrl() + "/XRMServices/2011/Organization.svc/web", false);
        xmlhttp.setRequestHeader("Accept", "application/xml, text/xml, */*");
        xmlhttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
        xmlhttp.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/" + Message);
        xmlhttp.send(_XML);
        _ResultXML = xmlhttp.responseXML;
        var errorCount = _ResultXML.selectNodes('//error').length;
        if (errorCount != 0) {
            var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
            alert(msg);
            _ResultXML = null;
            return _ResultXML;
        }
        else {
            return _ResultXML;
        }
    }
    catch (Err) {
        alert(Err);
        return;
    }
}


Pass current userid, accountid and roleid to CreateConnectionRecord function.

Hope this helps………!!!



No comments:

Post a Comment