Thursday, September 3, 2015

Filtering Lookup Attributes in Business Process Flows

I know that this has been posted elsewhere on the Interwebs, but now I can't find the original article, so here goes...

You can, in fact, apply JavaScript to attributes appearing in the Form Header and Business Process Flow display in CRM 2013/2015.

The attributes are not addressed as normal, but with header_ tag for attributes in the Header section, and header_process_ prefix for elements in the Business Process Flow section.

So if we had an attribute that existed in the Business Process Flow of the Incident form, called:

new_caseaccountid

we would address that element in Javascript with:

Xrm.Page.getControl("header_process_new_caseaccountid")

One of the things that we might want to do is to filter the lookup values for another field, (say, Case Contact) based on the value in the Case Account attribute. That being the case, we can attach an event handler to the 'Pre Search' event on the lookup attribute:

1:  if (Xrm.Page.getControl("header_process_new_casecontactid") != null) {  
2:    Xrm.Page.getControl("header_process_new_casecontactid").addPreSearch(addFilter);  
3:  }  

We can define addFilter as:

1:  function addFilter() {  
2:    var accountId = null;  
3:    var accountLookup;  
4:    var fetchQuery;  
5:    try {  
6:      if (Xrm.Page.getControl("header_process_new_caseaccountid") != null &&   
7:        Xrm.Page.getControl("header_process_new_caseaccountid").getAttribute().getValue() != null) {  
8:        accountLookup = Xrm.Page.getControl("header_process_new_caseaccountid").getAttribute().getValue();  
9:        accountId = accountLookup[0].id;  
10:      }  
11:      if (accountId != null || accountId != undefined) {  
12:        fetchQuery = "<filter type='and'>" +  
13:        "<condition attribute='statecode' operator='eq' value='0' />" +  
14:        "<condition attribute='parentcustomerid' operator='eq' value='" + accountId + "' />" +  
15:        "</filter>";  
16:        Xrm.Page.getControl("header_process_new_casecontactid").addCustomFilter(fetchQuery);  
17:      }  
18:    } catch (e) {  
19:      Xrm.Utility.alertDialog("addFilter Error: " + (e.description || e.message));  
20:    }  
21:  }  

Now we have a search filter on the Contact Case element on the Business Process Flow:

Found it!! The original article is here: http://inogic.com/blog/2014/07/how-to-apply-script-on-header-fields-and-bpf-fields/

1 comment:

  1. nice and informative article on CRM. really learned a lots of things. Get in tough for Quality CRM Solutions

    ReplyDelete