Sunday, August 16, 2009

Dynamics CRM 4.0 - JavaScript Web Service Helper Objects Part II

In February I posted a helper library for making JavaScript call to CRM - Original Post. Since then I have made a few enhancements.




  • Adding ordering to your results

  • Allowing for Null/Not Null Checks


The below example is used to default the address on a new contact to an address for their parent account. It pulls the first address (ordering by address number) where the address line 1 is not null (not null check).


try {
if (crmForm.all.parentaccountid.DataValue != null) {
// Create object passing in the entity you are selecting from
var crmService = new CrmService("customeraddress", LOGICAL_OPERATOR_AND);
crmService.AddColumn("name");
crmService.AddColumn("line1");
crmService.AddColumn("line2");
crmService.AddColumn("line3");
crmService.AddColumn("city");
crmService.AddColumn("stateorprovince");
crmService.AddColumn("postalcode");
crmService.AddColumn("country");

// Order by address number ascending
crmService.AddOrder("addressnumber", "Ascending");



// Add filter conditions (note: the "AND" logical operator was specified in constructor)
crmService.AddFilterCondition("parentid", crmForm.all.parentaccountid.DataValue[0].id, CONDITION_OPERATOR_EQUAL);

// Where line1 is not null
crmService.AddFilterCondition("line1", "", "NotNull");



// Retrieve the result object
var result = crmService.RetrieveMultiple();


// Loop through rows and select values (they return strings)
if (result.Rows.length > 0) {
var row = result.Rows[0];
// Get Column By Name
crmForm.all.address1_name.DataValue = row.GetValue("name");
crmForm.all.address1_line1.DataValue = row.GetValue("line1");
crmForm.all.address1_line2.DataValue = row.GetValue("line2");
crmForm.all.address1_line3.DataValue = row.GetValue("line3");
crmForm.all.address1_city.DataValue = row.GetValue("city");
crmForm.all.address1_stateorprovince.DataValue = row.GetValue("stateorprovince");
crmForm.all.address1_postalcode.DataValue = row.GetValue("postalcode");
crmForm.all.address1_country.DataValue = row.GetValue("country");



}
}
}
catch (e) {
alert(e.message);


}


The new library is attached below. Enjoy!


-Andrew



This release is provided "AS IS" and contains no warranty and confers no rights.



No comments: