Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

JavaScript help to check if Business Process Flow exists on the form

Posted on by 420

Hi,

Please note my CRM version: 2015 On-premises update 0.1

Given the version of CRM I have here I only have the Xrm.Page.ui.process methods of set visible and set display state. 

I have 2 different case types in CRM and for one of them I want to always hide the BPF and the other always show. 

I have successfully used JavaScript and the setVisible method to achieve this for most scenarios.

Unfortunately I have a lot of resolved cases which came over from CRM2011 and as such do not have any reference to the BPF on the form and I get this error when viewing the resolved case record "Unable to get property 'setVisible' of undefined or null reference". 

Ideally my JavaScript would say: Does a BPF exists on the form? If Yes, check the type then set visible true or false.

I have tried methods such as Xrm.Page.data.process.getActiveProcess() and Xrm.Page.data.process.getEnabledProcesses() to try and return a null value but I get the same error message as above on load.

A workaround is to re-open then close the cases but this is not very feasible due to volume.

Any suggestions appreciated. Thanks.

*This post is locked for comments

  • Erik Schaap Profile Picture
    Erik Schaap 25 on at
    RE: JavaScript help to check if Business Process Flow exists on the form

    Thanks for sharing the outcome Jason, it helped me a lot!

  • AlexJay Profile Picture
    AlexJay 420 on at
    RE: JavaScript help to check if Business Process Flow exists on the form

    Hi Jason,

    That's great, glad you've got it sorted!

    All the best

    Alex

  • JasonMcandrew Profile Picture
    JasonMcandrew 40 on at
    RE: JavaScript help to check if Business Process Flow exists on the form

    Hi Alex,

    Thankyou for replying, your code snippet was very helpful.

    My original stab at this wasn't far off, i was missing and additional if statement.

    This is what i ended up with and it's working perfectly to collapse the process flow if it exists on the form or do nothing if a process flow doesn't exist.

    function Collapsebpf(executionContext)

    {

    var formContext = executionContext.getFormContext();

    if(formContext.data.process) {

    var activeProcess = formContext.data.process.getActiveProcess();

    if(activeProcess && activeProcess.isRendered()) {

    var displayState = formContext.ui.process.getDisplayState();

    if (displayState == "expanded")

    {

    formContext.ui.process.setDisplayState("collapsed");

    }

    else if (displayState == "collapsed")

    {

    formContext.ui.process.setDisplayState("collapsed")

    }

    }

    else {

    // No BPF exists

    }

    }

    }

    Many thanks

    Jason

  • AlexJay Profile Picture
    AlexJay 420 on at
    RE: JavaScript help to check if Business Process Flow exists on the form

    Hi Jason, 

    In case it helps this is the full code snippet I used:

    function showHideProcessFlow(){
    var caseType = Xrm.Page.getAttribute("casetypecode").getText(); // Get Case Type
    var stateCode = Xrm.Page.getAttribute("statecode").getValue(); // Get Status
    if(Xrm.Page.data.process) {
    var activeProcess = Xrm.Page.data.process.getActiveProcess();
    if(activeProcess && activeProcess.isRendered()) {
    if (caseType == "Customer Support"){
    Xrm.Page.ui.process.setVisible(false);
    }
    else if (caseType == "Code of Conduct"){
    Xrm.Page.ui.process.setVisible(true);
    }
    } else {
    // No BPF exists
    }
    } else {
    // No BPF exists
    }
    }

    Was a while ago so perhaps much has changed. Such as using formContext instead of XRM.Page

    Sounds like the Xrm.Page.data.process.getActiveProcess() call is returning null or undefined when looking for any active processes in one of your scenarios.  Perhaps you could incorporate an additional null check on that in order to not run into your error, something like if(activeProcess == null) should check for whether it's undefined or null. I'm not a developer though so not the best person to be giving javascript advice! Hopefully someone else can chip in. 

    Best wishes, Alex. 

  • JasonMcandrew Profile Picture
    JasonMcandrew 40 on at
    RE: JavaScript help to check if Business Process Flow exists on the form

    I'm trying to achieve the same but struggling with the error message Unable to get property 'isRendered' of undefined or null reference

    My script works if the record has a process running but the error above occurs if the process has not been started.
    Do you have an example of how you implemented this?
    Thanks
    Jason
  • AlexJay Profile Picture
    AlexJay 420 on at
    RE: JavaScript help to check if Business Process Flow exists on the form

    Thank you Mehrdad, that has worked. Much appreciated.

  • Verified answer
    MehrCrm Profile Picture
    MehrCrm 290 on at
    RE: JavaScript help to check if Business Process Flow exists on the form

    Hi,

    Please try the following.

    if(Xrm.Page.data.process) {
    	var activeProcess = Xrm.Page.data.process.getActiveProcess();
    	if(activeProcess && activeProcess.isRendered()) {
    		// BPF exists on the form
    		// Do something
    	} else {
    		// No BPF exists
    	}
    } else {
    	// No BPF exists
    }


    Hope this helps,

    Mehrdad

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans