Thursday, December 30, 2010

Default the address on a new contact to an address for their parent account

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);
}

2 comments:

  1. Helper Object: http://blogs.inetium.com/blogs/azimmer/archive/2009/02/01/crm-4-0-javascript-web-service-helper-objects.aspx

    ReplyDelete
  2. Link to original Blog entry:
    http://blogs.inetium.com/blogs/azimmer/archive/2009/08/08/dynamics-crm-4-0-javascript-web-service-helper-objects-part-ii.aspx

    ReplyDelete