Update Selected Records in Home Page entity grid in CRM 2011

Posted: November 29, 2012 in CRM 2011, Ribbons

In one of my requirement i need to update the selected records in the home page contact entity grid by clicking on the custom ribbon button. Below is the code to add a ribbon button in the home page contact entity and update the selected records.

To update the records using OData, you need to add jquery1.4.1.min and json2 files. You can get those files from sdk
path – \sdk\samplecode\js\restendpoint\jqueryrestdataoperations\jqueryrestdataoperations\scripts.

Create a new solution, add the contact entity and export the solution. Open the customizations.xml file and replace the <RibbonDiffXml> with following code.

<RibbonDiffXml>
        <CustomActions>
          <CustomAction Id="Isv.contact.MainTab.CustomAction" Location="Mscrm.HomepageGrid.contact.MainTab.Collaborate.Controls._children" Sequence="41">
            <CommandUIDefinition>
              <Button Id="Isv.Grid.Contact.MainTab" Command="Isv.Grid.Contact.UpdateRecord.Command" LabelText="$LocLabels:Isv.Grid.Contact.UpdateRecord.LabelText" ToolTipTitle="$LocLabels:Isv.Grid.Contact.UpdateRecord.ToolTip" ToolTipDescription="$LocLabels:Isv.Grid.Contact.UpdateRecord.ToolTip" TemplateAlias="o1" Image16by16="/_imgs/ribbon/SendShortcut_16.png" Image32by32="/_imgs/ribbon/SendShortcut_32.png" />
            </CommandUIDefinition>
          </CustomAction>
        </CustomActions>
        <Templates>
          <RibbonTemplates Id="Mscrm.Templates"></RibbonTemplates>
        </Templates>
        <CommandDefinitions>
          <CommandDefinition Id="Isv.Grid.Contact.UpdateRecord.Command">
            <EnableRules>
              <EnableRule Id="Isv.Grid.Contact.OneSelected.EnableRule" />
            </EnableRules>
            <DisplayRules>
              <DisplayRule Id="Isv.Grid.Contact.UpdateRecord.DisplayRule" />
            </DisplayRules>
            <Actions>
              <JavaScriptFunction Library="$webresource:crm_json2.js" FunctionName="IsNAN">
              </JavaScriptFunction>
              <JavaScriptFunction Library="$webresource:crm_jquery1.4.1.min.js" FunctionName="IsNAN">
                </JavaScriptFunction>
              <JavaScriptFunction Library="$webresource:crm_contact.js" FunctionName="updateRecord">
                <CrmParameter Value="SelectedControlSelectedItemIds" />
              </JavaScriptFunction>
            </Actions>
          </CommandDefinition>
        </CommandDefinitions>
        <RuleDefinitions>
          <TabDisplayRules />
          <DisplayRules>
            <DisplayRule Id="Isv.Grid.Contact.UpdateRecord.DisplayRule">
              <CrmClientTypeRule Type="Web" />
            </DisplayRule>
          </DisplayRules>
          <EnableRules>
            <EnableRule Id="Isv.Grid.Contact.OneSelected.EnableRule">
              <SelectionCountRule AppliesTo="SelectedEntity" Minimum="1" />
            </EnableRule>
          </EnableRules>
        </RuleDefinitions>
        <LocLabels>
          <LocLabel Id="Isv.Grid.Contact.UpdateRecord.LabelText">
            <Titles>
              <Title languagecode="1033" description="Update Contacts" />
            </Titles>
          </LocLabel>
          <LocLabel Id="Isv.Grid.Contact.UpdateRecord.ToolTip">
            <Titles>
              <Title languagecode="1033" description="Update the selected Contact records" />
            </Titles>
          </LocLabel>
        </LocLabels>
      </RibbonDiffXml>

After replacing the code save the file and import the solution into the CRM.

Make sure the jscript file and function name exists in your webresource. Below is my jscript function

function updateRecord(selectedIds) {
    if (selectedIds != null && selectedIds != "") {
        var strIds = selectedIds.toString();
        var arrIds = strIds.split(",");
        for (var indxIds = 0; indxIds < arrIds.length; indxIds++) {
            updateContactRecords(arrIds[indxIds]);
        }
        alert("Selected Records Updated Successfully");
    }
    else {
        alert("No records selected!");
    }
}

function updateContactRecords(contactId) {
    var objContact = new Object();
    objContact.CreditOnHold = true;

    var jsonEntity = window.JSON.stringify(objContact);

    var serverUrl = Xrm.Page.context.getServerUrl();
    var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc/ContactSet";
    var ODataPath = serverUrl + ODATA_ENDPOINT;
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        url: ODataPath + "(guid'" + contactId + "')",
        data: jsonEntity,
        beforeSend: function (XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader("Accept", "application/json");
            XMLHttpRequest.setRequestHeader("X-HTTP-Method", "MERGE");
        },
        error: function (xmlHttpRequest, textStatus, errorThrown) {
            alert("Status: " + textStatus + "; ErrorThrown: " + errorThrown);
        }
    });
}

Click here to view all the <CrmParameter> values that can be passed.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s