web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Error using JavaScript to switch BPF

(0) ShareShare
ReportReport
Posted on by 39

Hello,

Due to a new requirement, I wanted to try using Javascript to switch the BPF for an opportunity based on a specific field value. In short, we are treating Contract Renewals as an Opportunity but want to use a different form and BPF. Switching the form will be easy enough for the team to handle, but it would be nice if the corresponding BPF could load when that form is selected. The approach I am starting with is using JS to switch the BPF when the "Contract Renewal" field is set to "Yes". I tried using this post as a guide:    

I am completely new to JavaScript so please bear with me, these may be very basic questions that follow. 

I modified the JS to match the fields and IDs as needed, I created the web resource, and I tried adding a new OnLoad event. When I save and publish, I get a JS error on the form: ReferenceError: Web resource method does not exist

I think part of the problem is...I don't know what the function is based on the code below. I tried checkSwitchBPFbyType and SwitchBPF but those do not appear to work.

Here is the JS I used:

var SwitchBPF ={
checkSwitchBPFbyType: function(executionContext)
{

var formContext = executionContext.getFormContext();
if(formContext.data.entity.getEntityName() == "opportunity")
{
if(formContext.data.process.getActiveProcess() != null)
{

var currentBpfID = formContext.data.process.getActiveProcess().getId();
if(formContext.getAttribute("cre14_contractrenewal"))
{
var leadType = formContext.getAttribute("cre14_contractrenewal").getValue();
if(leadType == "1" && currentBpfID != "aef5b4f7-2a6f-ec11-8943-00224825fcd0")
formContext.data.process.setActiveProcess("736d16c2-63d5-ed11-a7c7-00224822f0a9");
else if(leadType == "0" && currentBpfID != "aef5b4f7-2a6f-ec11-8943-00224825fcd0")
formContext.data.process.setActiveProcess("aef5b4f7-2a6f-ec11-8943-00224825fcd0");
}
}
}

I appreciate any help you can provide!

I have the same question (0)
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    According to your code, you should try using SwitchBPF.checkSwitchBPFbyType as an event handler.

    Also, you had 2 non-closed curly brackets. This code is valid:

    var SwitchBPF = {
    		checkSwitchBPFbyType: function(executionContext) {
    
    			var formContext = executionContext.getFormContext();
    			if (formContext.data.entity.getEntityName() == "opportunity") {
    				if (formContext.data.process.getActiveProcess() != null) {
    
    					var currentBpfID = formContext.data.process.getActiveProcess().getId();
    					if (formContext.getAttribute("cre14_contractrenewal")) {
    						var leadType = formContext.getAttribute("cre14_contractrenewal").getValue();
    						if (leadType == "1" && currentBpfID != "aef5b4f7-2a6f-ec11-8943-00224825fcd0")
    							formContext.data.process.setActiveProcess("736d16c2-63d5-ed11-a7c7-00224822f0a9");
    						else if (leadType == "0" && currentBpfID != "aef5b4f7-2a6f-ec11-8943-00224825fcd0")
    							formContext.data.process.setActiveProcess("aef5b4f7-2a6f-ec11-8943-00224825fcd0");
    					}
    				}
    			}
    		}
    };

  • Verified answer
    Leah Ju Profile Picture
    Microsoft Employee on at

    Hi shogg,

    You can write function and name it directly:

    function SwitchBPF(executionContext) {}

    For you code details, you should change it as following shown:

    function SwitchBPF(executionContext) {
        var formContext = executionContext.getFormContext();
        if (formContext.data.entity.getEntityName() == "opportunity") {
            if (formContext.data.process.getActiveProcess() != null) {
                var currentBpfID = formContext.data.process.getActiveProcess().getId();
                if (formContext.getAttribute("cre14_contractrenewal")) {
                    var leadType = formContext.getAttribute("cre14_contractrenewal").getValue();
                    if (leadType == 1 && currentBpfID != "aef5b4f7-2a6f-ec11-8943-00224825fcd0")
                        formContext.data.process.setActiveProcess("736d16c2-63d5-ed11-a7c7-00224822f0a9");
                    else if (leadType == 0 && currentBpfID != "aef5b4f7-2a6f-ec11-8943-00224825fcd0")
                        formContext.data.process.setActiveProcess("aef5b4f7-2a6f-ec11-8943-00224825fcd0");
                }
            }
        }
    }

    Also, I guess that "Contract Renewal" field should be two options type, it has labels and values, Right?

    0 and 1 should be the value, so you don't need to add ""

    if (leadType == 1 && currentBpfID != "aef5b4f7-2a6f-ec11-8943-00224825fcd0")

    else if (leadType == 0 && currentBpfID != "aef5b4f7-2a6f-ec11-8943-00224825fcd0")
  • shogg Profile Picture
    39 on at

    Hello,

    Thanks very much for this suggestion. After making the suggested tweaks, then adjusting my form Event Type with the correct function name (and enabling the option to Pass execution context as first parameter), the script no longer gives me an error when the form loads. 

    However...the script doesn't work as expected. It is not switching the BPF when the field contractrenewal = Yes (1) and the incorrect BPF is loaded. I double-checked the BPF IDs again (using the console and var currentBpfID = Xrm.Page.data.process.getActiveProcess().getId(); as suggested in the other post). I also tried both with "1" and "0" in quotations and outside of quotations.

    Any ideas of why it may not be working as expected?

  • Verified answer
    Leah Ju Profile Picture
    Microsoft Employee on at

    Hi shogg,

    You added onLoad event?

    Try to add js to onChange event, select the "Contract Renewal" field.

    pastedimage1681461174708v1.png

  • shogg Profile Picture
    39 on at

    Thanks for the suggestion; I made this adjustment but still am not seeing the results I expect. Any other troubleshooting suggestions?

  • Verified answer
    Leah Ju Profile Picture
    Microsoft Employee on at

    Hi shogg,

    Through checking original code, i notice that one process id has appeared three times in js: 

    pastedimage1681797042223v1.png

    Refer to the blog and think about the logic, the id in the judgment condition and the set ID should be the same:

    pastedimage1681797220307v2.png

    Make the following changes to the code:

                    if (leadType == 1 && currentBpfID != "736d16c2-63d5-ed11-a7c7-00224822f0a9")
                        formContext.data.process.setActiveProcess("736d16c2-63d5-ed11-a7c7-00224822f0a9");
                    else if (leadType == 0 && currentBpfID != "aef5b4f7-2a6f-ec11-8943-00224825fcd0")
                        formContext.data.process.setActiveProcess("aef5b4f7-2a6f-ec11-8943-00224825fcd0");

  • shogg Profile Picture
    39 on at

    Hi Leah,

    Thank you so much, this did work!

    I had a few things in the way of this working beautifully at first -- basically I had required fields in the way that prevented the OnChange event from working as expected. If there were required fields on the form that weren't filled out at the time of changing that field, the BPF didn't switch. I had to switch around the fields on the form a little and change a business rule but now this works consistently. 

    Thank you again for your help and patience working through this issue!

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 81 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 49 Most Valuable Professional

#3
#ManoVerse Profile Picture

#ManoVerse 40

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans