Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Can JScript re-trigger a business rule on a Form?

Posted on by 25

Hi 

We have subscribed to a phone verification service and integrated it with Dynamics 365.  Basically it is an async web service which is called whenever the phone number is changed.  As it physically verifies the phone line it can take a couple of seconds to return a result.  We have Javascript that executes the web service call and presents a confirm message if the phone number is not verified.  We store the verification state in a separate attribute.

If the phone number is not verified we are using a Business Rule Recommendation to provide a visual reminder that the phone number is not verified, so anyone dealing with the contact can reconfirm that their phone details are correct.  

The problem I have is that due to the execution order of Business Rules and JScript the business rule will display the recommendation while the service is verifying the phone number but the recommendation remains remains even after the JScript web service call returns a positive result.

So to recap this is the execution order assuming a blank phone number.

1) Business rule checks if the verification state is verified and phone number is populated, displays a recommendation if not.

2) User corrects the phone number, triggers OnChange event of the phone number control

3) OnChange executes the verification service

4) verification service returns a result and sets the verification state

Unfortunately the Business Rule will still display the recommendation, so is there a way to force the Business Rule to re-execute without forcing the form to save?

To solve this problem I have added a formcontext.data.entity.save(); which works but can be annoying for the user.

The only reason for using a Business Rule Recommendation is to provide a visual prompt without the need to display the verification status on the form, which has implications for the design of the form in the UCI.  I am happy to remove the business rule if there is a supported way of providing a visual indication like changing the font color of the field label or phone number in JScript.

Appreciate any feedback,

Cheers

Alan  

  • LeoAlt Profile Picture
    LeoAlt 16,329 on at
    RE: Can JScript re-trigger a business rule on a Form?

    Hi alan,

    Thanks so much for your sharing.

    Have a nice day!

    Best Regards,

    Leo

  • alan.abery Profile Picture
    alan.abery 25 on at
    RE: Can JScript re-trigger a business rule on a Form?

    Thanks Leo

    I did end up abandoning the Business Rules and used a combination of setFormNotifications and setRequiredLevel ended up being a lot less work and the method is shared.

    Below is the code.

    function SetFormVerificationNotications(executionContext, fieldName, fieldVerifiedState) {

       var formContext = executionContext.getFormContext(); // get formContext

       var controlLabel = formContext.getControl(fieldName).getLabel();

       var controlValue = formContext.getAttribute(fieldName).getValue();

       if (formContext.getAttribute(fieldVerifiedState).getValue() == AddressVerificationStateEnum.NotVerified && controlValue != "" && controlValue != undefined) {

           formContext.ui.setFormNotification("The " + controlLabel + " has not been verified.  Please check the details or click the Re-Verify Details button to run the verification again.", "INFO", fieldName);

           formContext.getAttribute(fieldName).setRequiredLevel("recommended");

       }

       else {

           formContext.ui.clearFormNotification(fieldName);

           formContext.getAttribute(fieldName).setRequiredLevel("none");

       }

    }

    Cheers

    Alan

  • Verified answer
    LeoAlt Profile Picture
    LeoAlt 16,329 on at
    RE: Can JScript re-trigger a business rule on a Form?

    Hi partner,

    1.The business rule is triggered when loading the form which means you should refresh the form again after you getting the result from the web service.

    You could consider to add a refresh form JS method after getting the result and then the Business rule will be triggered again.

    2.You could also use JS code in onload event to show the recommendation instead of using Business rule and add another JS code in the Onchange event of the phone field, when user corrects the phone number, hide the recommendation.

    You could use "setFormNotification" method to do this.

    https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/formcontext-ui/setformnotification

    Best Regards,

    Leo

  • Suggested answer
    meelamri Profile Picture
    meelamri 13,204 User Group Leader on at
    RE: Can JScript re-trigger a business rule on a Form?

    Hello, 

    As Samira said, you can create a New field, lets say "isVerifiy". The tricky part is to verify the phone number whithin a plugin. The plugin should be registred on retrieve message. So Who the new field is populated before the form is loaded. So you can use your BR to show your message. 

    Mehdi,

  • alan.abery Profile Picture
    alan.abery 25 on at
    RE: Can JScript re-trigger a business rule on a Form?

    Thanks Samira

    Two interesting options, the reason why I used a business rule was because I liked the way the Recommendation is rendered in UCI, and Classic for that matter as it doesn't impact the layout of the form.  

    We currently do have it implemented using JavaScript by displaying the Verification Status = Not Verified or Verified, however this really causes issues in UCI.   So if I take the JavaScript approach I would need to figure out how to either add an icon next to the label, change the font color of the label or font color in the control.

    The second option, I thought that the execution sequence would be a problem and as far as I know the sequence On Load is JavaScript then Business Rules then On Change Business Rules then JavaScript, so would probably end up in the same situation of having to force a save for the OnLoad business rules to trigger.

    Cheers

    Alan

  • SamiraM Profile Picture
    SamiraM 571 on at
    RE: Can JScript re-trigger a business rule on a Form?

    Hi Alan,

    There are two options to solve this issue:

    1) Convert your business rule to javascript and call that javascript once you webservices has finished verifying the phone number.

    2) Second option is you can add the additional field. Let Say "isCheck" of type two options. When your webservice is done  verifying, then update this filed "isCheck" to true. Modify you business rule has when isCheck is true show the recommendation and once recommendation is shown reset the isCheck fileds to false.

    Please Verify if you like the answer.

    Regards

    Samira

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans