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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / [plugin] infinite loop...
Power Apps
Answered

[plugin] infinite loop of update record

(0) ShareShare
ReportReport
Posted on by 115

Hi everybody,

 

I want to register 2 plugins to make a infinite loop of updating field.

Plugin1 monitors the [update] message of cr2ab_folder entity on cr2ab_prop1 attribute and executes on PreOperation stage.

sukie_y_0-1646463535576.png

And here is the source of plugin1:

if (context.Depth > 1) {
 trace.Trace("loop !!!");
 throw new InvalidPluginExecutionException("loop hassei");
}

cr2ab_Folder f = (context.InputParameters["Target"] as Entity).ToEntity<cr2ab_Folder>();
f.cr2ab_prop2 = f.cr2ab_prop1;

Plugin2 monitors the [update] message of cr2ab_folder entity on cr2ab_prop2 attribute and also executes on PreOperation stage.

sukie_y_1-1646463645503.png

And here is the source of plugin2:

if (context.Depth > 1) {
 trace.Trace("loop !!!");
 throw new InvalidPluginExecutionException("loop hassei 。");
}
cr2ab_Folder f = (context.InputParameters["Target"] as Entity).ToEntity<cr2ab_Folder>();
f.cr2ab_prop1 = f.cr2ab_prop2 + 1;

When I change the prop1 on form to 10, then prop2 will be 10 and the prop1 will be 101, And I am sure the plugin1 and plugin2 is only executed one time. 

I am confused why the infinite loop error did not happen??

 

The question is :

1, Although I changed only the prop2 value in plugin1, the prop1 is also set in the variable f. Why it does not triger the Update message of prop1 when the system updates the [folder] entity on MainOperation stage?

2, If the update of prop2 happened in plugin1 trigered the plugin2, why the update of prop1 did not triger the plugin1 again?

3, If I change the plugin1 to below:

if (context.Depth > 1) {
 trace.Trace("loop !!!");
 throw new InvalidPluginExecutionException("loop hassei");
}
cr2ab_Folder f = (context.InputParameters["Target"] as Entity).ToEntity<cr2ab_Folder>();
trace.Trace("not loop !!");
f.cr2ab_prop2 = f.cr2ab_prop1;
service.Update(f);

I got the infinite loop error ,

sukie_y_4-1646465637706.png

but the trace log will never stop ......

You can see the message is outputted per minute and paired,  one is "loop !!!" and one is "not loop !!"

sukie_y_3-1646465553664.png

I know the service.update(f) makes the error, but when the dialog is showed, the plugin should be stopped by system, why the execution continues??

 

Thank you very much!

Any reply or web resource will be appreciated !!

 

Best regards

justin

I have the same question (0)
  • Ram Prakash Duraisamy Profile Picture
    5,593 Super User 2025 Season 2 on at

    Hello @sukie_y 

     

    As per the Condition for Context.depth, it will stop your flow

     

    just give an try like low

     

     

    if(contet.depth>1){

    return;

    }

    else{

    // operations

    }

     

    Note:

     

    Are you updating same field again?

     

    Please mark as Answer if it is helpful and provide Kudos


    Subscribe : https://www.youtube.com/channel/UCnGNN3hdlKBOr6PXotskNLA
    Blog : https://microsoftcrmtechie.blogspot.com

  • Ethan.s Profile Picture
    115 on at

    Hi @rampprakash 

    Thank you for the reply!

     

    >>Are you updating same field again?

    No, I show you all of the test code except the initialization of <service> and <context> variable.
     
    I do not want some solution of infinite loop, I just want to know the reason of question 1/2/3 because it makes me understand the mechanism of plugin.
     
    Best Regards
    Justin
  • Ethan.s Profile Picture
    115 on at

    Anybody knows the answer of question 1/2/3?😫

  • Ram Prakash Duraisamy Profile Picture
    5,593 Super User 2025 Season 2 on at

    Hello   

    1. As per Below Code 

    f.cr2ab_prop1 = f.cr2ab_prop2 + 1;

    As you are setting value prop1 on 2 hence its updating.

     

    2. It's not triggering because that plugin already triggered and hence context.depth fails and came out.

     

    3. No you should not user Update in the PreOperation else it will go with Loop Error only.

     

    Please mark as Answer if it is helpful and provide Kudos


    Subscribe : https://www.youtube.com/channel/UCnGNN3hdlKBOr6PXotskNLA
    Blog :
    https://microsoftcrmtechie.blogspot.com

     

    @sukie_y

  • Verified answer
    ansrikanth Profile Picture
    329 on at

    That's an interesting one @Justin. All these days we hear people asking why my plugin is going into loops but here is a question why it is NOT going into loops 🙂

     

    Any ways, My theory based on best of my understanding about plugins is - even though you are setting the values of prop2 & prop1, but the plugin context is still same. It is running under the same pipe line. it is as same as resetting the field value in the same plugin.

    So, when the platform gets an update request,

    it first goes and sees there is a pre update plugin, lets execute it,

    it then sets the value of Prop2

    but then again it immediately realises that there is one more plugin that has to be executed because there is a change in Prop2.

    It then executes next plugin where the prop1 changes again

    but for that execution (pipeline/context) its done with Plugin1, so it don't bother to re-execute the plugin

    that's when it see all pre plug ins are done and lets go for main operation in the pipe line.

    That's the reason why you see the value should get update only once and it never goes in to loop. 

     

    But the moment you introduce an Update() in the plugin, which means you have triggered another execution of update and so a new pipeline (context) will start and immediately goes in to loop.

     

    Hope that makes some sense, I didn't understand your last question on Plugin trace log - could you explain a bit more what you meant by its going in to loop?

     

    Thank you

    -Srikanth

  • Ethan.s Profile Picture
    115 on at

    Thank you very much, but it is not the answer of my question...

  • Ethan.s Profile Picture
    115 on at

    THANK YOU!! @ansrikanth 

    So it is the execution pipeline! I understand what you say, Thank you very much !!!

     

    About question 3, when I trigger the plugin(the source what I posted is ALL), I got the standard alert dialog which say the Business Process Error, and even I close the dialog, the trace is continuously outputted to the plug-in trace log table.

    One trace is like below, it says "not loop!!": 

    sukie_y_1-1646742403736.png

     

    And one trace says "loop !!!"

    sukie_y_2-1646742494372.png

    Accrording to my source, ONE pair is correct because if depth > 1 I throw a exception, but, the trace is not stopable ... You could see that one execution has generated 14 traces (and will be more ) and the "not loop!!" and "loop !!" comes in pairs.

    sukie_y_3-1646742698322.png

    What cause that happended ...? Why exception can not stop the loop ?

     

    Best Regards

    Justin

  • ansrikanth Profile Picture
    329 on at

    ahh, I got what you are saying. I have a theory but to prove it I have to build the plugin my self and see what's happening. I need some time to do that.

    But if you have time, try to separate the messages clearly and you should see one time the exception should be coming from the Plugin1 and the other time from the plugin 2. 

    just change all the traces & exceptions to clearly mention from which plugin it is coming.. like "Plugin1 Loop", "Plugin1 outside loop", "plugin 1 excepton" and the same for other plugin as well .. 

     

    check the traces and popup messages you are seeing (post it over here if possible, that would be interesting to see :)) ..

     

    Thank you

    -Srikanth

  • Ethan.s Profile Picture
    115 on at

    Hi @ansrikanth 

     

    For question 3, I only registered plugin 1 for cr2ab_folder entity, but anyway I did what you said.

    sukie_y_1-1646750279265.png

    sukie_y_2-1646750413499.png

     

    You can see that all the trace is from plug1 

    sukie_y_0-1646750198172.png

    and the message is like this :

    sukie_y_3-1646750458340.pngsukie_y_4-1646750486903.png

    Thank you !

     

    Best Regards

    Justin

  • Ethan.s Profile Picture
    115 on at

    Could any MS supporter explain the question 3? ......

    Should I create a support ticket?

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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 721 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 320 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard