Tag Archives: Reactivate records

How To – Series 3: How to bulk Activate/Reactivate Dynamics CRM Records using CRM Update Web Service in Java Script

 Today I wish to share with you the frequently needed functionality “how to bulk Activate/Reactivate records on ISV Toolbar button click”.  This involves following simple steps to be performed.
1.       Creating an ISV button on the crmGrid
2.       Retrieving selected record’s GUIDs
3.       Updating the state of the selected records using CRM Update Service
Lets start with creating a Toolbar button on CRM grid. It is simple and straight forward. Just add following lines of code to the respective entity in the ISV.Config.

1.       Creating an ISV button on the “Lead” entity’s crmGrid
<Entity name=”lead”>
          <ToolBar ValidForCreate=”0″ ValidForUpdate=”1″>
            <Button Icon=”/_imgs/ico_18_debug.gif” JavaScript=”alert(“Your JavaScript code will be here”)”>
                <Title LCID=”1033″ Text=”Reactivate” />
                <ToolTip LCID=”1033″ Text=”Reactivate” />
</Entity> />

2.       Retrieving selected record’s GUIDs
var selectedrecords = document.all[‘crmGrid’].InnerGrid.SelectedRecords;
var selectedItems = new Array(selectedrecords.length);
for (var r=0; i < selectedrecords.length; i++)
 selectedItems[r] = selectedrecords [r][0];
                Selectedrecords[r][0] returns guid of the respected selected records
3.       Updating the state of the selected records using CRM Update Service
In order to change/set state of the “Lead” record we have to use “SetStateLeadRequest” class which has following fields EntityId, LeadState and LeadStatus. We need to pass an instance of this class as a request parameter to Execute() method. Check the possible state and status values for “Lead” entity in msdn documentation here
The possible state and status code for “Lead” entity in our requirement is “Open” and “New/Contacted”. We can also give statuscode as “-1” so that respective statuscode will be taken as required.
Below is the MS CRM Update Web Service Method code to reactivate closed leads
var xml = &apos;&apos; +
 &apos;&lt;?xml version=&apos;1.0&apos; encoding=&apos;utf-8&apos;?&gt;&apos; +
 &apos;&lt;soap:Envelope xmlns:soap=&apos;http://schemas.xmlsoap.org/soap/envelope/&apos;
 +  GenerateAuthenticationHeader()
  + &apos; &lt;soap:Body&gt;&apos;
  +  &apos; &lt;Execute xmlns=&apos;http://schemas.microsoft.com/crm/2007/WebServices&apos;&gt;&apos; +
 &apos; &lt;Request xsi:type=&apos;SetStateLeadRequest&apos;&gt;&apos; + &apos; &lt;EntityId&gt;&apos;+<selectedItemGUID>+&apos;&lt;/EntityId&gt;&apos; + &apos; &lt;LeadState&gt;Open&lt;/LeadState&gt;&apos; + &apos; &lt;LeadStatus&gt;-1&lt;/LeadStatus&gt;&apos; + &apos; &lt;/Request&gt;&apos; + &apos; &lt;/Execute&gt;&apos; + &apos; &lt;/soap:Body&gt;&apos; + &apos;&lt;/soap:Envelope&gt;&apos; + &apos;&apos;;
 var xmlHttpRequest = new ActiveXObject(&apos;Msxml2.XMLHTTP&apos;);
 xmlHttpRequest.Open(&apos;POST&apos;,&apos;/mscrmservices/2007/CrmService.asmx&apos;, false);
 xmlHttpRequest.setRequestHeader(&apos;Content-Type&apos;, &apos;text/xml; charset=utf-8&apos;);
 xmlHttpRequest.setRequestHeader(&apos;Content-Length&apos;, xml.length);
 var resultXml = xmlHttpRequest.responseXML;
Here I am setting SetStateLeadRequest’s EntityId to selected record’s GUID, LeadState to Open and LeadStatus to -1. This code snippet will does reactivate the specified GUID Lead record.
Now, finally we need to wire above three steps code together so that selected grid records will be reactived when crm grid toolbar’s button is clicked. Below is the final version of the code which you can directly use it in your ISV.config file. Here I kept CRM Update webservice code snippet inside the for loop of selected grid items so that update web service will be called with each selected record’s GUID.
<Entity name=”lead”>
<Button Icon=”/_imgs/ico_18_debug.gif” JavaScript=”var a = document.all[‘crmGrid’].InnerGrid.SelectedRecords; var selectedItems = new Array(a.length); for (var i=0; i &lt; a.length; i++) { selectedItems[i] = a[i][0]; var xml = &apos;&apos; + &apos;&lt;?xml version=&apos;1.0&apos; encoding=&apos;utf-8&apos;?&gt;&apos; + &apos;&lt;soap:Envelope xmlns:soap=&apos;http://schemas.xmlsoap.org/soap/envelope/&apos; xmlns:xsi=&apos;http://www.w3.org/2001/XMLSchema-instance&apos; xmlns:xsd=&apos;http://www.w3.org/2001/XMLSchema&apos;&gt;&apos; + GenerateAuthenticationHeader() + &apos; &lt;soap:Body&gt;&apos; + &apos; &lt;Execute xmlns=&apos;http://schemas.microsoft.com/crm/2007/WebServices&apos;&gt;&apos; + &apos; &lt;Request xsi:type=&apos;SetStateLeadRequest&apos;&gt;&apos; + &apos; &lt;EntityId&gt;&apos;+selectedItems[i]+&apos;&lt;/EntityId&gt;&apos; + &apos; &lt;LeadState&gt;Open&lt;/LeadState&gt;&apos; + &apos; &lt;LeadStatus&gt;-1&lt;/LeadStatus&gt;&apos; + &apos; &lt;/Request&gt;&apos; + &apos; &lt;/Execute&gt;&apos; + &apos; &lt;/soap:Body&gt;&apos; + &apos;&lt;/soap:Envelope&gt;&apos; + &apos;&apos;; var xmlHttpRequest = new ActiveXObject(&apos;Msxml2.XMLHTTP&apos;); xmlHttpRequest.Open(&apos;POST&apos;,&apos;/mscrmservices/2007/CrmService.asmx&apos;, false); xmlHttpRequest.setRequestHeader(&apos;SOAPAction&apos;,&apos;http://schemas.microsoft.com/crm/2007/WebServices/Execute&apos;); xmlHttpRequest.setRequestHeader(&apos;Content-Type&apos;, &apos;text/xml; charset=utf-8&apos;); xmlHttpRequest.setRequestHeader(&apos;Content-Length&apos;, xml.length); xmlHttpRequest.send(xml); var resultXml = xmlHttpRequest.responseXML;} window.crmGrid.Refresh();”>
<Title LCID=”1033″ Text=”Reactivate” />
<ToolTip LCID=”1033″ Text=”Reactivate” />
Hope it helps you…
Vikranth P