One of the cool things about CRM has been the ability to define and create relationships between customers. In v3.0, we used this a lot as a workaround for not being able to create a relationship from one entity to itself or many-many relationships. In CRM 4.0, we’ve seen the use of the Customer Relationship almost fall by the wayside, but the functionality still exists and I believe it is still useful.
One of the more recent “to-do” items I received was creating a way to open the Customer Relationship form in CRM 3.0 and have both of the relationship parties be pre-populated with records selected from the standard account grid. I figured it was blog-worthy because the code works in CRM 4.0 as well with only a minor tweak.
JavaScript
First off, we’re dealing with accessing objects in the grid. If you haven’t dealt with the grid before, I’ve got a couple of previous articles describing my experiences: Fun w/ the CRM Grid and More Grid Goodies.
In order to open a form in CRM with attributes pre-populated, you’ll need to pass those attribute values in the url. For the Customer Relationship entity, we need to populate the “customerid” and “partnerid” attributes.
Because we are populating lookup attributes, we have to specify the Object Type Code of the entity the lookup is referring to and also the text we want to display in the lookup.
The CRM 4.0 SDK contains an excellent tutorial on this and it applies to many areas in 3.0 as well.
So, the url we want to open is going to look like the following:
Now all we have to do is fill in the blanks:
var a = document.all['crmGrid'].InnerGrid.SelectedRecords;
if(a.length == 2) {
var selectedItems = new Array(a.length);
var selectedNames = new Array(a.length);
for(var i=0; i < a.length; i++) {
selectedItems[i] = a[i][0];
selectedNames[i] = a[i][3].cells[2].innerText.replace(/&/g,'%26');
}
var customerId = selectedItems[0];
var customerName = selectedNames[0];
var partnerId = selectedItems[1];
var partnerName = selectedNames[1];
var url = '/sfa/customerrelationships/edit.aspx?customerid=' + customerId + '&customeridtype=1&customeridname=' + customerName + '&partnerid=' + partnerId + '&partneridtype=1&partneridname=' + partnerName;
window.showModalDialog(url,'','dialogHeight:500px;dialogWidth:700px;status:no;resizable:yes');
}
else {
alert('You must select 2 records to use this feature.');
}
What the above code does is first check to make sure only 2 records are selected, otherwise we alert the user. Then, create two arrays: one for the id of the record (selectedItems[]) and one for the display name of the record (selectedNames[]).
Next, we iterate through the grid’s SelectedRecords array (a[]) and fill our two variables mentioned previously. Now each selected record in the grid is an array also, so it’s nested. The first element of the a[] array is the record id, so selectedItems[i] = a[i][0].
Next we need to get the display name from the grid record. The 4th element of the a[] array is a ROW object. That means we need to use a[i][3] and then find which cell within the row object the display name resides. This is tricky because it depends on which column the name is in.If it’s the first column in the grid, then selectedNames[i] = a[i][3].cells[2]. Why am I using cells[2]? Well, cells[0] = the arrow that is displayed and cells[1] = the icon for the entity that is displayed. So, the first data column will always be cells[3], the 2nd will be cells[4], etc. We want to take the innerText of the appropriate cell.
You’ll notice I perform a replace() that finds all ampersands in the text and encodes them for the url. that is important of your records are likely to have those characters. Although I don’t do it here, you will want to allow for other characters that are reserved in the url (like '”?”).
All that left is to create our url and pop the window.
Note: For CRM 4.0 you’ll want to create your URL, then pass it to the prependOrgName() function to get your final url. If you are running in single-tenant mode, this isn’t really necessary.
ISV.Config
Finally, just wrap the javascript inside the ISV config XML. For 3.0, this looked like:
<Entity name="account">
<Grid>
<MenuBar>
<Buttons>
<Button Title="Create Relationship" ToolTip="Create Relationship Between Select Records" Icon="/_imgs/ico_18_4502.gif"
JavaScript="var a = document.all['crmGrid'].InnerGrid.SelectedRecords;
if(a.length == 2) {
var selectedItems = new Array(a.length);
var selectedNames = new Array(a.length);
for(var i=0; i < a.length; i++) {
selectedItems[i] = a[i][0];
selectedNames[i] = a[i][3].cells[2].innerText.replace(/&/g,'%26');
}
var customerId = selectedItems[0];
var customerName = selectedNames[0];
var partnerId = selectedItems[1];
var partnerName = selectedNames[1];
var url = '/sfa/customerrelationships/edit.aspx?customerid=' + customerId + '&customeridtype=1&customeridname=' + customerName + '&partnerid=' + partnerId + '&partneridtype=1&partneridname=' + partnerName;
window.open(url);
}
else {
alert('You must select 2 records to use this feature.');
}
"/>
</Buttons>
</MenuBar>
</Grid>
</Entity>
If you want this functionality for 4.0, then you can work on the appropriate XML wrapper.
Cheers!

Comments