RSS

Tag Archives: Marketing List

CRM 2013 – Refresh Associated Grid of an Entity

Introduction

Today I got a requirement to add a custom Ribbon Button in the SubGrid of Marketing List Entity and execute some logic and refresh the Grid. Last point was the trickiest one for me and that is why I decided to share the approach how to do it.

Steps

To achieve this, let’s go step by step.

Step-1: Create a JavaScript library that will be executed when Button is Clicked

function OpenLookUp(grid){
    //your logic

    //code that will refresh the grid
    grid.refresh();
}

Step-2: Add Custom Button using Ribbon Workbench and follow the Screen Shots

img1

img4
Select WebResource

Add the Selected Control as parameter.
img3

Step-3: Publish Solutions using Ribbon Workbench

Now the view will refresh after the Button is clicked.

By Puja Jain
Senior Software EngineerĀ @Team DynamicsCRM.
Mindfire Solutions

 
2 Comments

Posted by on December 27, 2013 in WebResources

 

Tags: , , , , , , , , , , , , , , , , , , , , , , , ,

How to Associate Marketing List with Lead, Contact or Account Entity using SOAP Service

Introduction

As we know SDK.REST.js is a JavaScript library having functionality to do many ‘Client-Server‘ operations, so it provides a function to associate two records of different Entities.

Background

I have a requirement in my project to open a Marketing List Lookup on click of a custom button and able to add multiple Marketing List to Lead, Contact or Account records. I have tried this using REST service, but it threw an error message of “Associate not support for Marketing List“. Then I tried using SOAP service and got a fruitful solution.

Let’s do it…

Create a Web Resource for Custom Button and go through the following code.
Here “OpenLookUp function will be fired on button click.

var relationname;
var entityName;
function OpenLookUp(grid) {
    // prepare the CRM URL
    //var serverurl = Xrm.Page.getServerUrl();
    var customServerURL = document.location.protocol + "//" + document.location.host;

    var membertypecode;
    entityName = Xrm.Page.data.entity.getEntityName();
    if (entityName == "lead")
        membertypecode = "4";
    else if (entityName == "account")
        membertypecode = "1";
    else if (entityName == "contact")
        membertypecode = "2";
    
    var url = customServerURL + '/_controls/lookup/lookupinfo.aspx?AllowFilterOff=1&objecttypes=4300&DefaultType=4300&DefaultViewId=%7b7D7737D8-1A05-4ABB-BEBD-843404A83DAD%7d&DisableQuickFind=0&DisableViewPicker=1&LookupStyle=multi&ShowNewButton=1&ShowPropButton=1&browse=0&listType=static&membertypecode=' + membertypecode + '';
    var lookUp = window.showModalDialog(url, "entity", "dialogWidth:600px0px;dialogHeight:600px0px;,resizable=1,menubar=0,status=1,location=0,toolbar=0,scrollbars=1");
    
    if (lookUp != null) {
        if (typeof lookUp != 'object') {
            lookUp = $.parseJSON(lookUp);
            associateMarketingListToEntity(lookUp);
        }
    }
}

///<summary>
/// Associates records asynchronously and displays messages so you can verify the association
///</summary>
function associateMarketingListToEntity(lookup) {
    var id = Xrm.Page.data.entity.getId();
    
    relationname = "Referenced" + entityName + "_parent_list";
    entityName = (entityName.charAt(0)).toUpperCase() + entityName.substr(1, entityName.length - 1);

    var itemLists = lookup.items;
    for (var i = 0; i < itemLists.length; i++) {
        AddMemberListRequest(id.replace(/[{}]/g, ""), (itemLists[i].id).replace(/[{}]/g, ""));
    }
}

function AddMemberListRequest(id, Listid) {
    var requestMain = "";

    requestMain += "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">";
    requestMain += "  <s:Body>";
    requestMain += "    <Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";
    requestMain += "      <request i:type=\"b:AddListMembersListRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\" xmlns:b=\"http://schemas.microsoft.com/crm/2011/Contracts\">";
    requestMain += "        <a:Parameters xmlns:c=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">";
    requestMain += "          <a:KeyValuePairOfstringanyType>";
    requestMain += "            <c:key>ListId</c:key>";
    requestMain += "            <c:value i:type=\"d:guid\" xmlns:d=\"http://schemas.microsoft.com/2003/10/Serialization/\">" + Listid + "</c:value>";
    requestMain += "          </a:KeyValuePairOfstringanyType>";
    requestMain += "          <a:KeyValuePairOfstringanyType>";
    requestMain += "            <c:key>MemberIds</c:key>";
    requestMain += "            <c:value i:type=\"d:ArrayOfguid\" xmlns:d=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">";
    requestMain += "              <d:guid>" + id + "</d:guid>";

    requestMain += "            </c:value>";
    requestMain += "          </a:KeyValuePairOfstringanyType>";
    requestMain += "        </a:Parameters>";
    requestMain += "        <a:RequestId i:nil=\"true\" />";
    requestMain += "        <a:RequestName>AddListMembersList</a:RequestName>";
    requestMain += "      </request>";
    requestMain += "    </Execute>";
    requestMain += "  </s:Body>";
    requestMain += "</s:Envelope>";

    var req = new XMLHttpRequest();
    req.open("POST", _getServerUrl(), true);

    // Responses will return XML. It isn't possible to return JSON.
    req.setRequestHeader("Accept", "application/xml, text/xml, */*");
    req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    req.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
    var successCallback = null;
    var errorCallback = null;
    req.onreadystatechange = AddListMembersListResponse(req, null, null);
    req.send(requestMain);
}

function ReLoadForm() {
    window.location.reload(true);
}

///<summary>
/// Receives the assign response
///</summary>
///<param name="req" Type="XMLHttpRequest">
/// The XMLHttpRequest response
///</param>
///<param name="successCallback" Type="Function">
/// The function to perform when an successful response is returned.
/// For this message no data is returned so a success callback is not really necessary.
///</param>
///<param name="errorCallback" Type="Function">
/// The function to perform when an error is returned.
/// This function accepts a JScript error returned by the _getError function
///</param>
function AddListMembersListResponse(req, successCallback, errorCallback) {
    if (req.readyState == 4) {
        if (req.status == 200) {
            if (successCallback != null)
            { successCallback(); }
        }
        else {
            _getError(req.responseXML);
        }
    }
}

///<summary>
/// Returns the URL for the SOAP endpoint using the context information available in the form
/// or HTML Web Resource.
///</summary>
function _getServerUrl() {   
    var OrgServicePath = "/XRMServices/2011/Organization.svc/web";
    var serverUrl = "";
    if (typeof GetGlobalContext == "function") {
        var context = GetGlobalContext();
        serverUrl = context.getServerUrl();
    }
    else {
        if (typeof Xrm.Page.context == "object") {
            serverUrl = Xrm.Page.context.getServerUrl();
        }
        else
        { throw new Error("Unable to access the server URL"); }
    }
    if (serverUrl.match(/\/$/)) {
        serverUrl = serverUrl.substring(0, serverUrl.length - 1);
    }
    return serverUrl + OrgServicePath;
}

///<summary>
/// Parses the WCF fault returned in the event of an error.
///</summary>
///<param name="faultXml" Type="XML">
/// The responseXML property of the XMLHttpRequest response.
///</param>
function _getError(faultXml) {    
    var errorMessage = "Unknown Error (Unable to parse the fault)";

    if (typeof faultXml == "object") {
        try {
            var bodyNode = faultXml.firstChild.firstChild;
            //Retrieve the fault node
            for (var i = 0; i < bodyNode.childNodes.length; i++) {
                var node = bodyNode.childNodes[i];
                //NOTE: This comparison does not handle the case where the XML namespace changes
                if ("s:Fault" == node.nodeName) {
                    for (var j = 0; j < node.childNodes.length; j++) {
                        var faultStringNode = node.childNodes[j];
                        if ("faultstring" == faultStringNode.nodeName) {
                            errorMessage = faultStringNode.text;
                            break;
                        }
                    }
                    break;
                }
            }
        }
        catch (e) { };
    }
    return new Error(errorMessage);
}

By Sukanta Mangal
Software Engineer @Team DynamicsCRM.
Mindfire Solutions

 

Tags: , , , , , , , , , , , , , , , , , ,

 
%d bloggers like this: