06 January 2020

Hiding multiple fields with JavaScript in business process flows

Dropped in Dynamics 365 CRM

Jon
Formulated by Jon

We recently had a request from a client to modify their business process flow and it seemed worthwhile to share our experience and the lessons learned in this scenario.

This business process flow was incredibly complicated and was designed to move the record between various staff members who each performed a separate task (with one stage per task). However, at any point within this process, a staff member could update the record and stop all further stages and they used a field on the business process flow to manage this. This had been working well for them, but they wanted to move the field from the business process flow to the header and still wanted to keep the functionality.

So, they had the same field on multiple stages of their business process flow where ALL instances needed to be hidden.

If you have a quick look on the internet, you’ll see lots of blog posts about hiding a field in a business process flow using JavaScript. So here it is again!

function hideFields(executionContext)
{
var formContext = executionContext.getFormContext();
formContext.getControl('header_process_tm_fieldname').setVisible(false);
}

If you call the ‘hideFields’ function from your form, then the field with the schema name ‘tm_fieldname’ will be hidden on the business process flow. As I say, I’m not breaking any new ground here. If you run this code, it hides the first one, so all the others remain. How do you do this? I couldn’t find anything out there that could help me.

After a bit of experimentation with trial and error, the answer, as it turns out, is quite simple – and it’s not just a case of repeating that last line multiple times (trust me, I tried that too!).

I updated the function to the one below:

function hideFields(executionContext)
{
var formContext = executionContext.getFormContext();
formContext.getControl('header_process_tm_fieldname').setVisible(false);
formContext.getControl('header_process_tm_fieldname_1').setVisible(false);
...
formContext.getControl('header_process_tm_fieldname_8').setVisible(false);
}

Yep, it’s just a case of appending ‘_1’, ‘_2’, etc to the field name and it will pick that instance of the field. I was delighted when I tested this and it worked but, when I opened a closed record, it started to give me a script error.
It turns out that it’s a simple case of referring to fields that are no longer there. As the business process branches off, the fields are no longer available, so the code is trying to update the visibility of fields which aren’t available which is never a good thing. It’s then just a case of adding an ‘if’ around each of the ‘setVisible’ statements to check if the field is actually there:

function hideFields(executionContext)
{
var formContext = executionContext.getFormContext();
if (formContext.getControl('header_process_tm_fieldname') != null)
formContext.getControl('header_process_tm_fieldname').setVisible(false);
if (formContext.getControl('header_process_tm_fieldname_1') != null)
formContext.getControl('header_process_tm_fieldname_1').setVisible(false);
...
if (formContext.getControl('header_process_tm_fieldname_8') != null)
formContext.getControl('header_process_tm_fieldname_8').setVisible(false);
}

That’s it! This isn’t something that’s cropped up a lot over the years but, when I did need help, I found absolutely nothing out there to help me. Hopefully, this will help you.

Enhanced on 02 Jan 2020

Reply to Jon's post

Get in touch with one of the UK's best Microsoft Dynamics partner

Start your journey towards excellence with Microsoft Dynamics 365

Let us know your details and we'll get in touch with you to learn how we can transform your business processes and operations with Microsoft Dynamics 365 Business Central & Dynamics 365 Customer Engagement:

*

*

*

*

*

*

Protected by GDPR. We'll never share your personal data.

0
Years established
0
Happy customers
0
Projects
0
People supported
Brandauer
Pipers Crisps Co
Aden + Anais
Costa Coffee
Charlton & Jenrick
World Animal Protection

Get in touch

Wolverhampton

St Mark's Church
St Mark's Road
Wolverhampton WV3 0QH

Newcastle

19 Kingsway House
Kingsway
Team Valley
Gateshead NE11 0HW

Call: 01902 578 300
Skype: Call now

Co. Reg. 03100541
VAT No. GB559725692

Follow us

If you want even more, you can stalk us on any of our social media platforms: