CRM 2013 now allows java script coding on header fields as well as BPF fields. Sometimes we come across with the requirement where we need to write script on the fields existing in the business process flow and not on the form. Same with the fields on header of any form.
When a form displays a business process flow control in the header, additional controls gets added for each attribute that is been displayed in that BPF. These controls hold unique names like: header_process_<attribute name>. So using this control we can write script around these fields.
The Controls displayed in the form header are also accessible and hold unique name like: header_<attribute name>.
We can perform all those operations that we can perform on the attribute added on the form. Below we have mentioned some of the sample scripts around such fields.
- Show/Hide field
We can also show/hide the fields from the BPF same like we do for the other form attributes. Below we have hidden the field based on the value selected on the form.
We can simply use below line of code to show/hide the field from the BPF.
Xrm.Page.getControl(“header_process_parentcontactid”).setVisible(false);
You can use below line of code to show/hide the field from the Header.
Xrm.Page.getControl(“header_leadsourcecode”).setVisible(false);
- Make the field Read only
The field will be read only after applying script on the BPF controls.
Below is a line of code where we have disabled the control from the BPF.
//Check if the control exist on the form
if (Xrm.Page.getControl(“header_process_parentcontactid”) != null) {
//Set the control disabled
Xrm.Page.getControl(“header_process_parentcontactid”).setDisabled(true);
}
The same script you can use to apply to the header fields.
//Check if the control exist on the form
if (Xrm.Page.getControl(“header_leadsourcecode”) != null) {
//Set the control disabled
Xrm.Page.getControl(“header_leadsourcecode”).setDisabled(true);
}
- Filter Lookup
We can also filter on lookup based on another. Below we have written script to filter Existing Contact lookup based on Existing Account selected.
We have selected Fourth Coffee Account in the Existing Account as you can see in below screen shot:
And the Existing Contact lookup will now show only Account associated Contacts in this lookup, where as previously it was showing All the contacts exist in CRM regardless of any condition.
Below is a code snippet which will help you to filter a lookup:
function filterLookup() {
//Check if the control exist on the form
if (Xrm.Page.getControl(“header_process_parentcontactid”) != null) {
// add the event handler for PreSearch Event
Xrm.Page.getControl(“header_process_parentcontactid”).addPreSearch(addFilter);
}
}
function addFilter() {
var accountId = null;
var accountLookup;
var fetchQuery;
try {
//Check if control exist on form
if (Xrm.Page.getControl(“header_process_parentaccountid”) != null && Xrm.Page.getControl(“header_process_parentaccountid”).getAttribute().getValue() != null) {
//Get Account lookup value
accountLookup = Xrm.Page.getControl(“header_process_parentaccountid”).getAttribute().getValue();
//Get the account id
accountId = accountLookup[0].id;
}
//Build fetch
if (accountId != null || accountId != undefined) {
fetchQuery = “<filter type=’and’>” +
“<condition attribute=’statecode’ operator=’eq’ value=’0′ />” +
“<condition attribute=’parentcustomerid’ operator=’eq’ value='” + accountId + “‘ />” +
“</filter>”;
//add custom filter
Xrm.Page.getControl(“header_process_parentcontactid”).addCustomFilter(fetchQuery);
}
} catch (e) {
Xrm.Utility.alertDialog(“addFilter Error: ” + (e.description || e.message));
}
}
- Get/Set Value
We need to use below method to get/set the value on the BPF fields.
Xrm.Page.getControl(“header_process_descriptions”).getAttribute().getValue();
Xrm.Page.getControl(“header_process_description”).getAttribute().setValue(“Hello World”);
If you want to write a script on the header fields then you can use below line of code for this.
Xrm.Page.getControl(“header_leadsourcecode”).getAttribute().setValue(parseInt(value));
Key Points:
- We can only perform actions like set/get value, show/hide fields, add custom filter etc. but we cannot add event handlers on the BPF fields. For this we need to add the same field on the form and then add event handler on that field.
- As we cannot apply OOB filter lookups on the BPF fields, achieving through scripts is an advantage.
Marketing4Dynamics – Mailchimp and Dynamics 365 CRM integration to plan effective sales strategies, increase sales and improve ROI
- Sync Audiences, Members and Tags from Mailchimp to CRM
- Sync CRM Marketing List (Contacts/Leads) to Mailchimp
- Sync Campaigns and Member activities from Mailchimp to CRM
- Monitor and analyze Mailchimp campaign statistics through Dashboards in CRM
Hi,
On Interactive Service Hub Case form we are getting null reference of Status Reason field which is present on Header. we have used javascript code to access
var statusReason_header = Xrm.Page.getControl(“header_statuscode”);.
This code is working fine in Web client but interactive service hub is returning null reference.
Thanks.
Hi,
In case of Interactive service hub Case form, though the “Status Reason” field is in the Header it can be retrieved using just Xrm.Page.getControl(“statuscode”).
There is no need to append “header_” to the field to the field name.
Thanks!
Sam
Hi,
I have a lookup that occurs in two places on a business process flow, but the filter that I’ve applied to it isn’t only affecting the first instance. Do you know how I might apply it to both?
Thanks,
Adam
I meant IS only affecting the first instance, i.e. the second occurrence of the field is unfiltered.
If you have two instance of field on BPF then and you want to perform any operation on second instance then use 1 after field logical name.
For example if you have two instance of Parent contact field on BPF then
for first instance use following code : Xrm.Page.getControl(“header_process_parentcontactid”).setVisible(false);
for second instance use following code : Xrm.Page.getControl(“header_process_parentcontactid1”).setVisible(false);
Please let us know if it works for you.
Thanks!
Thanks, it did work, I wrote an additional function to iterate through the fields adding a number each pass until there were no more left. Works great!
Hi I tried this code
Xrm.Page.getControl(“header_process_willis_probability1”).setDisabled(true);
but it shows me field not present.
Hi,
I am trying to implement this, I have a many to many relationship between the two entities. I am wondering how the fetch looks like? I tried modifying yours but no luck. Also is statecode needed in the fetch?
Thanks
Hi ,
Regarding statecode, to get only active records we use “statecode” in fetch query. If you want records to be filter based on the Status of record then use “statecode” in the query. For example, show only active account, show only open opportunities etc.
Regarding FetchQuery for two entities,
Are you trying to filter the lookup having filter on the N:N relationship?
Could you please elaborate your query so it will help us to understand the exact requirement.
Thanks!
Sam