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………!!!



Retrieve multiple records using JavaScript SOAP request in MS CRM 2011 !!!


Below example to retrieve accountid based on the account name from account entity:-

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;
    }

function GetMultipleAccounts(AccountName) {

    var request = "<s:Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'>" +
                 "<s:Body>" +
                 "<RetrieveMultiple xmlns='http://schemas.microsoft.com/xrm/2011/Contracts/Services'     xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>" +
                 "<query i:type='a:QueryExpression' xmlns:a='http://schemas.microsoft.com/xrm/2011/Contracts'>" +
                 "<a:ColumnSet>" +
                 "<a:AllColumns>false</a:AllColumns>" +
                 "<a:Columns xmlns:b='http://schemas.microsoft.com/2003/10/Serialization/Arrays'>" +
                 "<b:string>accountid</b:string>" +
                 "</a:Columns>" +
                 "</a:ColumnSet>" +
                 "<a:Criteria>" +
                 "<a:Conditions>" +
                 "<a:ConditionExpression>" +
                 "<a:AttributeName>name</a:AttributeName>" +
                 "<a:Operator>Equal</a:Operator>" +
                 "<a:Values xmlns:b='http://schemas.microsoft.com/2003/10/Serialization/Arrays'>" +
                 "<b:anyType i:type='c:string' xmlns:c='http://www.w3.org/2001/XMLSchema'>" + AccountName + "</b:anyType>" +
                 "</a:Values>" +
                 "</a:ConditionExpression>" +
                 "</a:Conditions>" +
                 "<a:FilterOperator>And</a:FilterOperator>" +
                 "<a:Filters />" +
                 "</a:Criteria>" +
                 "<a:Distinct>false</a:Distinct>" +
                 "<a:EntityName>account</a:EntityName>" +
                 "<a:LinkEntities />" +
                 "<a:Orders />" +
                 "<a:PageInfo>" +
                 "<a:Count>0</a:Count>" +
                 "<a:PageNumber>0</a:PageNumber>" +
                 "<a:PagingCookie i:nil='true' />" +
                 "<a:ReturnTotalRecordCount>true</a:ReturnTotalRecordCount>" +
                 "</a:PageInfo>" +
                 "<a:NoLock>false</a:NoLock>" +
                 "</query>" +
                 "</RetrieveMultiple>" +
                 "</s:Body>" +
                 "</s:Envelope>";

    var _ResultXML = ExecuteRequest(request, "RetrieveMultiple");
    var count = _ResultXML.selectSingleNode("//a:TotalRecordCount").text;
    if (count >= 1) {
        var ID = _ResultXML.selectSingleNode("//a:Id").text
        return ID;
    }
    else
        return null;
}

Call GetMultipleAccounts function like as below:-

var _Id = GetMultipleAccounts(“Microsoft”);

Cheers !!!

Saturday, 30 July 2011

Filter Custom Unit of measurement (Unit) Lookup with multiple conditions

Please read simple filter lookup in ms crm2011, my last post before implement this.
Let’s take a simple scenario, we have custom unit lookup in opportunity product entity. We want to filter Custom Unit Lookup based on different product’s set to Custom Media entity where Media and Product entity have many-to-many relationship.
1.       Use link-entity in FetchXML

Here is the code sample:-

function SetUnitFilterLookup() {
                if (Xrm.Page.getAttribute("new_mediaid").getValue() != null) {
                var _Value = Xrm.Page.getAttribute("new_mediaid").getValue()[0].id;
                var viewId = "{65EC9B45-EE81-4F89-BAF6-E8603FF8E1E4}";
                var entityName = "uom";
                var viewDisplayName = "Active Units For Media"
                var fetchXml = "<fetch version='1.0' output-format='xml-platform' mapping='logical'      distinct='true'>" +
                  "<entity name='uom'>" +
                   "<attribute name='uomid'/>" +
                   "<attribute name='name'/>" +
                   "<link-entity name='product' to='uomid' from='defaultuomid' visible='false' intersect='true'>" +
                   "<link-entity name=new_new_media_product' to='productid' from='productid' visible='false' intersect='true'>" +
                   "<link-entity name=new_media' alias='ab' to=new_mediaid' from=new_mediaid' visible='false' intersect='true'>" +
                   "<filter type='and'>" +
                   "<condition attribute=new_mediaid' value='" + _Value + "' operator='eq'/>" +
                   "</filter></link-entity></link-entity></link-entity></entity></fetch>";

        //build grid layout

        var layoutXml = "<grid name='resultset' " +
                             "object='1' " +
                             "jump='name' " +
                             "select='1' " +
                             "icon='1' " +
                             "preview='1'>" +
                             "<row name='result' " +
                             "id='uomid'>" +
                             "<cell name='name' " +
                             "width='300' />" +
                             "</row>" +
                            "</grid>";

        //add new view view

        Xrm.Page.getControl("new_unitofsaleid").addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, true);
    }
}

Call this function on OnChange Event of Media Lookup field.
Hope it helps someone!!!