cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Axal
Super User
Super User

Can Business Rule run after formload ?

Hello everyone,

 

Can Business Rule applied or run after form load ? So I have a needs whereby, my field will only set to be mandatory, when the record is saved on the 1st time. For example : I'm creating new record, in one of the field lets say "Description", is not mandatory, user can save this new record. But after 1st saved, I want this Description to be mandatory. Problem is I have a Javascript to make an autosaved at first time the form load.

 

In my head, I would think like this :

  1. Had 1 business rule to check some field, I have 1 field which is an autonumber, which will have value when it's saved.
  2. So when this field is contain data, my business rule is Set Business Required this Description, if not contain data, don't make field Description as Business Required.

Is this make sense ?

Well, I tried like that, but it's not working, because it's looks like when it is saving (my java script to auto save running) then in that instance, the field "Description" is mandatory, so it is already return error.

 

Kindly advice whether it's possible to make the business rule run after form load, so my data will be saved first then displayed fully on the screen, then right after that my field become mandatory. So only at the 2nd time and after, when user click save, it will throw message that field is empty.

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
EricRegnier
Super User
Super User

Hi @Axal,

It's not working because business rules run on load (no guarantee of the execution order) and after a field referenced in the business rule changes. So what @rampprakash is suggesting won't work.  If you absolutely need logic after a save event then you'll need JavaScript to register a function on post save event: https://docs.microsoft.com/powerapps/developer/model-driven-apps/clientapi/reference/events/postsave 
Hope this helps!

View solution in original post

7 REPLIES 7
rampprakash
Super User
Super User

Hey @Axal,

 

Please try this logic in Business Rule

 

Condition :

 

If CREATEDON contains Data

Set Description field as Mandatory

Else Description field not Mandatory

 

Createdon Field will only store values if record gets created.

 

Please mark as Answer if it is helpful and provide Kudos

 

Subscribe : https://www.youtube.com/channel/UCnGNN3hdlKBOr6PXotskNLA

Blog : https://microsoftcrmtechie.blogspot.com

Hi @rampprakash 

 

Unfortunately, it is not working. With that rule, when my form load, it already give me error saying "Description" is empty.

 

The form not shown first.

 

 

Thanks,

 

rampprakash
Super User
Super User

Hi @Axal,

 

Can you please remove all your existing logic and check the Entity type to form and tray again.

Hi @rampprakash ,

 

I'm sorry, what should I check on the entity ? and what is entity type to form ?

Thanks

Prakash4691
Super User
Super User

@Axal 

 

I think you might be using 

formContext.data.save(saveOptions), saveoption->savemode set to 70 to autosave using javascript.
 
If you use above client API it runs asynchronously, that is why on onload your business rule triggers first and throwing an exception even before javascript executes.  
 
To overcome the issue, deactivate business rule and handle everything using javascript.
 
Including sample code below,
function onload(executionContext) {
  var formContext = executionContext.getFormContext();
  if (formContext.ui.getFormType() == 2) {// check for form type it should run only on update and not on create
    var saveOptions = { saveMode: 70 };
    formContext.data.save(saveOptions).then(
      function (success) {
        formContext.getAttribute("cr5b9_jh").setRequiredLevel("required");// set required level
      },
      function (error) {
        console.log(error.message);
      }
    );
  }
}
 
If it answers your question, kindly give kudo and accept it as solution.
 
 
Regards,
Prakash

Hi @Axal,

 

Please find the sample screenshot for business Rule for your reference.

 

rampprakash_1-1631250526263.png

Please mark as Answer if it is helpful and provide Kudos

 

Subscribe : https://www.youtube.com/channel/UCnGNN3hdlKBOr6PXotskNLA

Blog : https://microsoftcrmtechie.blogspot.com

 

EricRegnier
Super User
Super User

Hi @Axal,

It's not working because business rules run on load (no guarantee of the execution order) and after a field referenced in the business rule changes. So what @rampprakash is suggesting won't work.  If you absolutely need logic after a save event then you'll need JavaScript to register a function on post save event: https://docs.microsoft.com/powerapps/developer/model-driven-apps/clientapi/reference/events/postsave 
Hope this helps!

Helpful resources

Announcements
Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

May UG Leader Call Carousel 768x460.png

June User Group Leader Call

Join us on June 28 for our monthly User Group leader call!

PA Virtual Workshop Carousel 768x460.png

Register for a Free Workshop

This training provides practical hands-on experience in creating Power Apps solutions in a full-day of instructor-led App creation workshop.

PA.JPG

New Release Planning Portal (Preview)

Check out our new release planning portal, an interactive way to plan and prepare for upcoming features in Power Platform.

Users online (2,150)