Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Answered

Error using JavaScript to switch BPF

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!

  • shogg Profile Picture
    shogg 39 on at
    RE: Error using JavaScript to switch BPF

    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!

  • Verified answer
    Leah Ju Profile Picture
    Leah Ju Microsoft Employee on at
    RE: Error using JavaScript to switch BPF

    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
    shogg 39 on at
    RE: Error using JavaScript to switch BPF

    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
    Leah Ju Microsoft Employee on at
    RE: Error using JavaScript to switch BPF

    Hi shogg,

    You added onLoad event?

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

    pastedimage1681461174708v1.png

  • shogg Profile Picture
    shogg 39 on at
    RE: Error using JavaScript to switch BPF

    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
    Leah Ju Microsoft Employee on at
    RE: Error using JavaScript to switch BPF

    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")
  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Error using JavaScript to switch BPF

    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");
    					}
    				}
    			}
    		}
    };

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,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans