JavaScript code to pre populate fields on associated record opened from parent or related record

Recently, I came across a scenario, where it was required that some fields be pre-populated on a new form opened from associated view of a record.
One possible solution is to fire a SOAP Xml retrieve request to get the desired fields of the related record and set them on the new form. But this approach requires a database interaction using MS CRM web services, that can cause a noticeable delay in form load.
An alternative to this approach is to access parent record using JavaScript object model and access the fields on form to set values on new form.
Following sample code can be added to OnLoad event of form to achieve the same:

//*****************************************************************************
//Functionality- In create mode, get default values from associated location record
//*****************************************************************************
if (crmForm.FormType == 1) {

//If form is being opened using an existing parent record
if ((window.opener != null) && (window.opener.parent != null) && (window.opener.parent.document != null) && (window.opener.parent.document.crmForm != null)) {
var oParentCrmForm = window.opener.parent.document.crmForm;
if (oParentCrmForm == null oParentCrmForm == undefined) {
}
else {
if (oParentCrmForm.ObjectTypeName == 'parent') {
//Set field value as on parent
crmForm.all.field1.DataValue = oParentCrmForm.all.field1.DataValue;
}
}
}
}
//*****************************************************************************

Data fetched using this approach is the current data on the form that may not be saved to the database yet.

Hope this helps.

Comments

  1. Casino Review & Ratings | DrmCD
    Learn 동해 출장안마 the casino review including 동두천 출장안마 bonus codes, games, deposit 경주 출장안마 methods, games, and customer 남원 출장안마 support. Check out Drmcd's Casino review to find out 진주 출장마사지 more!

    ReplyDelete

Post a Comment

Popular posts from this blog

IFD Implementation for custom web pages and Anonymous Access

Using CRM Services to create or update records in large numbers

Issues after Re-Naming MS CRM Servers computer/machine name