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

Community site session details

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

Dynamics 365 Force BPF using Javascript

(0) ShareShare
ReportReport
Posted on by 28

Hello,

For a client I am trying to force to activate the standard Opportunity Sales Process when a lead is qualified (and an opportunity is created). I tried using the WorkflowTools solution, but this only works a few times before doing nothing again (even though the workflow states it is succeeded).

So now I am trying to force to activate this Opportunity Sales Process using the following method: https://powerobjects.com/tips-and-tricks/business-process-flows-switch-bpf-dynamics-365-javascript/

Since I constantly received the error 'Webresource method does not exist', I copied the name of the function of another javascript I have running (that actually works). This is the script I have created:

function FieldChange_OnChange(executionContext){
var formContext = executionContext.getFormContext();
var oppTopic = formContext.getAttribute(“name”).getValue();
var oppType = formContext.getAttribute(“mkb365_voortgekomenuitlead”).getText();
var activeProcessID = formContext.data.process.getActiveProcess().getId();
if (oppTopic != null) {
if (oppType == “Ja” && activeProcessID.toUpperCase() != “3E8EBEE6-A2BC-4451-9C5F-B146B085413A”) formContext.data.process.setActiveProcess(“3E8EBEE6-A2BC-4451-9C5F-B146B085413A”, callBackFunction);
}
};
function callBackFunction(result) {
if (result == “success”) {} else {}

When a lead is qualified, I have a workflow that automatically sets the field 'mkb365_voortgekomenuitlead' to 'Ja'. In the javascript I am using this field as a condition to activate the business process flow.

I have added this script to the form and the Event Handlers of the form. It didn't do anything. No response whatsoever. So then I added the javascript to the field itself (OnChange). The problem is: it doesn't do anything at all. No errors, no business process flow, nothing.

I am not really familiar with Javascripts, so I am probably doing something wrong. Could anyone please help me with this? Or maybe there is another way to activate the Opportunity Sales Process automatically?

Thank you in advance!

I have the same question (0)
  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at
    RE: Dynamics 365 Force BPF using Javascript

    Hi,

    There were quiet a few syntax error in your JS code. I have fixed it -

    function FieldChange_OnChange(executionContext){

    var formContext = executionContext.getFormContext();

    var oppTopic = formContext.getAttribute("name").getValue();

    var oppType = formContext.getAttribute("mkb365_voortgekomenuitlead").getText();

    var activeProcessID = formContext.data.process.getActiveProcess().getId();

    if (oppTopic != null) {

    if (oppType == "Ja" && activeProcessID.toUpperCase() != "3E8EBEE6-A2BC-4451-9C5F-B146B085413A")

    formContext.data.process.setActiveProcess("3E8EBEE6-A2BC-4451-9C5F-B146B085413A", callBackFunction);

    }

    };

    function callBackFunction(result) {

    if (result == "success") {} else {}

    }

    I use below website to check JS syntax problem

    esprima.org/.../validate.html

    Please mark my answer verified if this is helpful!

    Regards,

    Bipin Kumar

    Follow my Blog: xrmdynamicscrm.wordpress.com/

  • Stieben Profile Picture
    28 on at
    RE: Dynamics 365 Force BPF using Javascript

    Hello Bipin,

    Thank you for your quick reply and for fixing the JS code! I have replaced my code with your code and tested it. However it still doesn't do anything. If I place this line of code on the form (OnLoad) it doesn't do anything.

    If I put it on the field (OnChange) the record only saves when changing the value of the field. The Opportunity Sales Process does not appear.

    Is there something missing in the code?

  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at
    RE: Dynamics 365 Force BPF using Javascript

    Hi,

    Add alert in your code as below to see till what point your code is getting executed -

    function FieldChange_OnChange(executionContext){

    var formContext = executionContext.getFormContext();

    var oppTopic = formContext.getAttribute("name").getValue();

    var oppType = formContext.getAttribute("mkb365_voortgekomenuitlead").getText();

    var activeProcessID = formContext.data.process.getActiveProcess().getId();

    alert("Active Process ID"+activeProcessID);

    if (oppTopic != null) {

    alert("Opp Type"+oppType);

    if (oppType == "Ja" && activeProcessID.toUpperCase() != "3E8EBEE6-A2BC-4451-9C5F-B146B085413A")

    alert("Condition satisfied");

    formContext.data.process.setActiveProcess("3E8EBEE6-A2BC-4451-9C5F-B146B085413A", callBackFunction);

    }

    };

    function callBackFunction(result) {

    if (result == "success") {} else {}

    }

    if activeProcessID returns with {} then write below code to replace curly  brances with empty string

    activeProcessID.replace('{','').replace('}','');

    Please mark my answer verified if this is helpful!

    Regards,

    Bipin Kumar

    Follow my Blog: xrmdynamicscrm.wordpress.com/

  • Stieben Profile Picture
    28 on at
    RE: Dynamics 365 Force BPF using Javascript

    Thank you for your reply.

    I have put in the alert in the code and tried to run the script again, on form OnLoad as well as on field OnChange. However no alert is popping up. It still behaves the same as the other script. When I put the script on the field, the record only saves when I switch the value of the field. When I put it OnLoad of the form, nothing happens.

    Maybe it's the 'function' that isn't correct? This is the only real difference from the example here: powerobjects.com/.../

    However when I put in the function 'onLoadChangeBPFByType(executionContext)' I get the message that the web resource method does not exist.

    Maybe we need to fix that message?

  • Stieben Profile Picture
    28 on at
    RE: Dynamics 365 Force BPF using Javascript

    On little sidenote: when trying the code again with the 'onLoadChangeBPFByType(executionContext)' function, I am now getting the error:

    'Cannot read properties of undefined (reading 'getFormContext')'

    This is the script I am using now:

    function onLoadChangeBPFByType(executionContext){

    var formContext = executionContext.getFormContext();

    var oppTopic = formContext.getAttribute("name").getValue();

    var oppType = formContext.getAttribute("mkb365_voortgekomenuitlead").getText();

    var activeProcessID = formContext.data.process.getActiveProcess().getId();

    alert("Active Process ID"+activeProcessID);

    if (oppTopic != null) {

    alert("Opp Type"+oppType);

    if (oppType == "Ja" && activeProcessID.toUpperCase() != "3E8EBEE6-A2BC-4451-9C5F-B146B085413A")

    alert("Condition satisfied");

    formContext.data.process.setActiveProcess("3E8EBEE6-A2BC-4451-9C5F-B146B085413A", callBackFunction);

    }

    };

    function callBackFunction(result) {

    if (result == "success") {} else activeProcessID.replace('{','').replace('}','');

    }

  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at
    RE: Dynamics 365 Force BPF using Javascript

    Hi,

    Makse sure you have selected below highlighted checkbox.

    CR83.PNG

    Also below line of code is not required now, we will add after checking the value in alert.

    activeProcessID.replace('{','').replace('}','');

    Please mark my answer verified if this is helpful!

    Regards,

    Bipin Kumar

    Follow my Blog: xrmdynamicscrm.wordpress.com/

  • Stieben Profile Picture
    28 on at
    RE: Dynamics 365 Force BPF using Javascript

    Hi Bipin,

    Thank you for your reply! I am now getting another error message. This one:

    'Cannot read properties of null (reading 'getId')'

    The opportunity does not have an activated business process flow at the moment. Maybe this is the issue? Or we could change/remove this line?

  • Stieben Profile Picture
    28 on at
    RE: Dynamics 365 Force BPF using Javascript

    Wait, I might have solved it. I removed 'getId()' and then I was getting a lot of alerts, so I knew something went right. And then the opportunity process appeared as well. So I changed the javascript, to remove all the alerts and to remove the 'getActiveProcess' check (as we don't really need that, we just need to process to activate). I now have this javascript running and it seems to work:

    function onLoadChangeBPFByType(executionContext){

    var formContext = executionContext.getFormContext();

    var oppTopic = formContext.getAttribute("name").getValue();

    var oppType = formContext.getAttribute("mkb365_voortgekomenuitlead").getText();

    var activeProcessID = formContext.data.process.getActiveProcess()

    if (oppTopic != null) {

    if (oppType == "Ja")

    formContext.data.process.setActiveProcess("3E8EBEE6-A2BC-4451-9C5F-B146B085413A", callBackFunction);

    }

    };

    function callBackFunction(result) {

    if (result == "success") {} else activeProcessID.replace('{','').replace('}','');

    }

    Could you confirm that this is a correct javascript?

    Thank you so much!

  • Suggested answer
    Bipin D365 Profile Picture
    28,983 Moderator on at
    RE: Dynamics 365 Force BPF using Javascript

    Hi,

    Add null check condition before getting Id of the active process.

    I updated your code below -

    function onLoadChangeBPFByType(executionContext){

    var formContext = executionContext.getFormContext();

    var oppTopic = formContext.getAttribute("name").getValue();

    var oppType = formContext.getAttribute("mkb365_voortgekomenuitlead").getText();

    if(formContext.data.process.getActiveProcess()!=null)

    {

    var activeProcessID = formContext.data.process.getActiveProcess().getId();

    alert("Active Process ID"+activeProcessID);

    if (oppTopic != null) {

    alert("Opp Type"+oppType);

    if (oppType == "Ja" && activeProcessID.toUpperCase() != "3E8EBEE6-A2BC-4451-9C5F-B146B085413A")

    {

    alert("Condition satisfied");

    formContext.data.process.setActiveProcess("3E8EBEE6-A2BC-4451-9C5F-B146B085413A", callBackFunction);

    }

    }

    }

    };

    function callBackFunction(result) {

    if (result == "success") {} else activeProcessID.replace('{','').replace('}','');

    }

    Please mark my answer verified if this is helpful!

    Regards,

    Bipin Kumar

    Follow my Blog: xrmdynamicscrm.wordpress.com/

  • Verified answer
    Bipin D365 Profile Picture
    28,983 Moderator on at
    RE: Dynamics 365 Force BPF using Javascript

    Hi,

    It looks good to me.

    The only code which you don't need is

    activeProcessID.replace('{','').replace('}','');

    Apart from that everything looks good.

    I would highly recommend you validate your code by doing testing on multiple records with different use cases.

    Please mark my answer verified if this is helpful!

    Regards,

    Bipin Kumar

    Follow my Blog: xrmdynamicscrm.wordpress.com/

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Rishabh Kanaskar Profile Picture

Rishabh Kanaskar 149

#2
MVP-Daniyal Khaleel Profile Picture

MVP-Daniyal Khaleel 142

#3
Tom_Gioielli Profile Picture

Tom_Gioielli 125 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans