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”)”>
<Titles>
<Title LCID=”1033″ Text=”Reactivate” />
</Titles>
<ToolTips>
<ToolTip LCID=”1033″ Text=”Reactivate” />
</ToolTips>
</Button>
</ToolBar>
</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 = '' +
'<?xml version='1.0' encoding='utf-8'?>' +
'<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns:xsd='http://www.w3.org/2001/XMLSchema'>'
+ GenerateAuthenticationHeader()
+ ' <soap:Body>'
+ ' <Execute xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>' +
' <Request xsi:type='SetStateLeadRequest'>' + ' <EntityId>'+<selectedItemGUID>+'</EntityId>' + ' <LeadState>Open</LeadState>' + ' <LeadStatus>-1</LeadStatus>' + ' </Request>' + ' </Execute>' + ' </soap:Body>' + '</soap:Envelope>' + '';
var xmlHttpRequest = new ActiveXObject('Msxml2.XMLHTTP');
xmlHttpRequest.Open('POST','/mscrmservices/2007/CrmService.asmx', false);
xmlHttpRequest.setRequestHeader('SOAPAction','http://schemas.microsoft.com/crm/2007/WebServices/Execute');
xmlHttpRequest.setRequestHeader('Content-Type', 'text/xml; charset=utf-8');
xmlHttpRequest.setRequestHeader('Content-Length', xml.length);
xmlHttpRequest.send(xml);
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”>
<Grid>
<MenuBar>
<Buttons>
<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 < a.length; i++) { selectedItems[i] = a[i][0]; var xml = '' + '<?xml version='1.0' encoding='utf-8'?>' + '<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>' + GenerateAuthenticationHeader() + ' <soap:Body>' + ' <Execute xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>' + ' <Request xsi:type='SetStateLeadRequest'>' + ' <EntityId>'+selectedItems[i]+'</EntityId>' + ' <LeadState>Open</LeadState>' + ' <LeadStatus>-1</LeadStatus>' + ' </Request>' + ' </Execute>' + ' </soap:Body>' + '</soap:Envelope>' + ''; var xmlHttpRequest = new ActiveXObject('Msxml2.XMLHTTP'); xmlHttpRequest.Open('POST','/mscrmservices/2007/CrmService.asmx', false); xmlHttpRequest.setRequestHeader('SOAPAction','http://schemas.microsoft.com/crm/2007/WebServices/Execute'); xmlHttpRequest.setRequestHeader('Content-Type', 'text/xml; charset=utf-8'); xmlHttpRequest.setRequestHeader('Content-Length', xml.length); xmlHttpRequest.send(xml); var resultXml = xmlHttpRequest.responseXML;} window.crmGrid.Refresh();”>
<Titles>
<Title LCID=”1033″ Text=”Reactivate” />
</Titles>
<ToolTips>
<ToolTip LCID=”1033″ Text=”Reactivate” />
</ToolTips>
</Button>
</Buttons>
</MenuBar>
</Grid>
</Entity>
Hope it helps you…
Vikranth P