How to Set field values in CRM through parameters passed to a new form using JavaScript
In this blog I am showing how we can add default values to the new form when open new form.
You can set default values for new records created by users by specifying attribute values in the URL that is used to open the form. For this you need to add a Button placed on the form(Existing Record) and on click of this button we'll copy data to the new form from the existing record.
In this example I placed a button of account entity form which is enable only if record exists and on click of this a new form opens with few values copied to new form using parameter passed in URL.
Add code as shown below
function GetIdFromLookup(lkpFieldSchemaName) {
var lkpField = null;
var sGuid = null;
lkpField = Xrm.Page.data.entity.attributes.get(lkpFieldSchemaName);
if (lkpField != null && lkpField.getValue() != null)
sGuid = lkpField.getValue()[0].id;
return sGuid;
}
function GetNameFromLookup(lkpFieldSchemaName) {
var lkpField = null;
var sName = null;
lkpField = Xrm.Page.data.entity.attributes.get(lkpFieldSchemaName);
if (lkpField != null && lkpField.getValue() != null)
sName = lkpField.getValue()[0].name;
return sName;
}
function btnCopyContact_OnClick() {
try {
//Retrieves the form type 1 - New, 2 - Edit, 3 - Read only, 4 - Bulk edit
var FormTypeValue = Xrm.Page.ui.getFormType();
if (FormTypeValue != null) {
//For Update FormType is 2, Create=1, Undefined=0, Read Only=3, Disabled=4,
//QuickCreate(Deprecated)=5, Bulk Edit=6, Read Optimized(Deprecated)=11
if(FormTypeValue == 2) {
var parameters = {};
var fullname = Xrm.Page.data.entity.attributes.get("fullname").getValue();
var jobtitle = Xrm.Page.data.entity.attributes.get("jobtitle").getValue();
var businessphone = Xrm.Page.data.entity.attributes.get("telephone1").getValue();
var mobilephone = Xrm.Page.data.entity.attributes.get("mobilephone").getValue();
var parentaccount = GetIdFromLookup("parentcustomerid");
var parentaccountname = GetNameFromLookup("parentcustomeridname");
var parentaccounttype = "account";
var emailaddress = Xrm.Page.data.entity.attributes.get("emailaddress1").getValue();
var maritalstatus = Xrm.Page.data.entity.attributes.get("familystatuscode").getValue();
if (fullname != null) {
parameters["fullname"] = fullname;
}
if (jobtitle != null) {
parameters["jobtitle"] = jobtitle;
}
if (mobilephone != null) {
parameters["mobilephone"] = mobilephone;
}
if (businessphone != null) {
parameters["telephone1"] = businessphone;
}
if (parentaccount != null) {
parameters["parentcustomerid"] = parentaccount;
}
if (parentaccounttype != null) {
parameters["parentcustomeridtype"] = parentaccounttype;
}
if (parentaccountname != null) {
parameters["parentcustomeridname"] = parentaccountname;
}
if (emailaddress != null) {
parameters["emailaddress1"] = emailaddress;
}
if (maritalstatus !=null)
{
parameters["familystatuscode"] = maritalstatus;
}
var newWindow = Xrm.Utility.openEntityForm("contact", null, parameters);
}
}
}
catch (e) {
alert(e.message.toString())
}
}
Existing Form:
When Copy Contact Button clicked then new forms open with data:
You can set default values for new records created by users by specifying attribute values in the URL that is used to open the form. For this you need to add a Button placed on the form(Existing Record) and on click of this button we'll copy data to the new form from the existing record.
In this example I placed a button of account entity form which is enable only if record exists and on click of this a new form opens with few values copied to new form using parameter passed in URL.
Add code as shown below
function GetIdFromLookup(lkpFieldSchemaName) {
var lkpField = null;
var sGuid = null;
lkpField = Xrm.Page.data.entity.attributes.get(lkpFieldSchemaName);
if (lkpField != null && lkpField.getValue() != null)
sGuid = lkpField.getValue()[0].id;
return sGuid;
}
function GetNameFromLookup(lkpFieldSchemaName) {
var lkpField = null;
var sName = null;
lkpField = Xrm.Page.data.entity.attributes.get(lkpFieldSchemaName);
if (lkpField != null && lkpField.getValue() != null)
sName = lkpField.getValue()[0].name;
return sName;
}
function btnCopyContact_OnClick() {
try {
//Retrieves the form type 1 - New, 2 - Edit, 3 - Read only, 4 - Bulk edit
var FormTypeValue = Xrm.Page.ui.getFormType();
if (FormTypeValue != null) {
//For Update FormType is 2, Create=1, Undefined=0, Read Only=3, Disabled=4,
//QuickCreate(Deprecated)=5, Bulk Edit=6, Read Optimized(Deprecated)=11
if(FormTypeValue == 2) {
var parameters = {};
var fullname = Xrm.Page.data.entity.attributes.get("fullname").getValue();
var jobtitle = Xrm.Page.data.entity.attributes.get("jobtitle").getValue();
var businessphone = Xrm.Page.data.entity.attributes.get("telephone1").getValue();
var mobilephone = Xrm.Page.data.entity.attributes.get("mobilephone").getValue();
var parentaccount = GetIdFromLookup("parentcustomerid");
var parentaccountname = GetNameFromLookup("parentcustomeridname");
var parentaccounttype = "account";
var emailaddress = Xrm.Page.data.entity.attributes.get("emailaddress1").getValue();
var maritalstatus = Xrm.Page.data.entity.attributes.get("familystatuscode").getValue();
if (fullname != null) {
parameters["fullname"] = fullname;
}
if (jobtitle != null) {
parameters["jobtitle"] = jobtitle;
}
if (mobilephone != null) {
parameters["mobilephone"] = mobilephone;
}
if (businessphone != null) {
parameters["telephone1"] = businessphone;
}
if (parentaccount != null) {
parameters["parentcustomerid"] = parentaccount;
}
if (parentaccounttype != null) {
parameters["parentcustomeridtype"] = parentaccounttype;
}
if (parentaccountname != null) {
parameters["parentcustomeridname"] = parentaccountname;
}
if (emailaddress != null) {
parameters["emailaddress1"] = emailaddress;
}
if (maritalstatus !=null)
{
parameters["familystatuscode"] = maritalstatus;
}
var newWindow = Xrm.Utility.openEntityForm("contact", null, parameters);
}
}
}
catch (e) {
alert(e.message.toString())
}
}
When Copy Contact Button clicked then new forms open with data:
Comments
Post a Comment