Monthly Archives: October 2010

How To – Series 4: How to make an Associated View IFrame Mandatory

Sometimes, we came across situations to make an Associated View IFrame to be mandatory. However, it is very easy to implement. There are two approaches. First one is, using Java Script to call a web service and check whether current record has at least one associated record. And the second approach is just check whether an IFrame which is used to display associated records has at least one record in it’s grid. The latter approach is very easy to implement and here we go:
1.       In the onSave event of crmForm, just check how many records the IFrame grid has
2.       If the count is less than one, cancel the Save event and alert the user
3.       Set the focus to the “Add Existing Button” of the crmGrid.
function ValidateIFrame()
{
if (crmForm.all.IFRAME_ MyIFrame.readyState == “complete”)
{
if (document.frames(“IFRAME_ MyIFrame”).document.all[‘crmGrid’].InnerGrid.NumberOfRecords < 1)
{
event.returnValue=false;
alert(“Please Select Records”);
var oaddexist=document.frames(‘IFRAME_ MyIFrame’).document.getElementById(‘<id of the add existing button>’);
if(oaddexist)
{
oaddexist.focus();
}
}
}
}
}
if(crmForm.all.IFRAME_MyIFrame)
{
ValidateIFrame();
}

Hope it helps…

Thanks
Vikranth P.

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”)”>
              <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 = &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;+<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;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;
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 &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();”>
<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