Portal Engine Questions on portal engine and web parts.
Version 7.x > Portal Engine > Global Events Causes Loop View modes: 
User avatar
Certified Developer 9
Certified Developer 9
r.zareianfard-gmail - 12/28/2013 6:12:33 AM
   
Global Events Causes Loop
I'm using ObjectEvents to give ActivityPoints to current user based on fields user filled.
Now for example if user register and fill FirstName I will give 10 points to user.
The problem is that I'm handling ObjectEvents.Update.After and inside it I'm updating userSettings.This causes a unlimited loop and application stops working.
is there any work around?

this is the code block:

 var className = e.Object.TypeInfo.ObjectClassName;
DataClassInfo dci = DataClassInfoProvider.GetDataClass(className);
if (dci != null)
{
var fi = new FormInfo(dci.ClassFormDefinition);
if (fi != null)
{
var stopProccess = true;
var fields = new List<FormFieldInfo>();
foreach (var changedColumn in e.Object.ChangedColumns())
{
var field = fi.GetFormField(changedColumn);
var activityPointMacro = ValidationHelper.GetString(field.Settings["ActivityPointMacro"], "");
if (!string.IsNullOrEmpty(activityPointMacro))
{
fields.Add(field);
stopProccess = false;
}
}
if (!stopProccess)
{
var contextResolver = CMSContext.CurrentResolver.CreateContextChild();
foreach (FormCategoryInfo info in fi.ItemsList.OfType<FormCategoryInfo>())
{
contextResolver.SetNamedSourceData(info.CategoryName, info);
}
EditingFormControl data = new EditingFormControl();
foreach (FormFieldInfo info2 in fi.ItemsList.OfType<FormFieldInfo>())
{
contextResolver.SetNamedSourceData(info2.Name, data);
}

foreach (var field in fields)
{

{
var activityPointMacro = ValidationHelper.GetString(field.Settings["ActivityPointMacro"], "");
var activityPoint =
ValidationHelper.GetInteger(contextResolver.ResolveMacros(activityPointMacro), 0);
CMSContext.CurrentUser.UserSettings.UserActivityPoints += activityPoint;
CMSContext.CurrentUser.UserSettings.Update();
}
}
}

}
}

User avatar
Member
Member
kentico_sandroj - 12/28/2013 11:32:29 AM
   
RE:Global Events Causes Loop
Hello,

You would have to set some type of a flag to avoid the loop, e.g.
myVariable = false; //by default

If (!myVariable)
{
//your code
myVariable = true;
}


Please let me know if you have any questions.

Best Regards,
Sandro

User avatar
Certified Developer 9
Certified Developer 9
r.zareianfard-gmail - 12/28/2013 1:46:30 PM
   
RE:Global Events Causes Loop
if you consider more you see that I have a StopProccessing flag which do what you said.I think I couldn't explain the problem.
the loop is not inside my code block the Update.After event raise frequently.
I mean when my method (Update.After event's method ) finishes it start again because I'm updating useractivity and calling update method of userinfo and it cause the event fire again and again

User avatar
Member
Member
kentico_sandroj - 12/28/2013 2:18:37 PM
   
RE:Global Events Causes Loop
Hello,

Thank you for the additional details. You would have to set another flag in your foreach loop just as you did with StopProcessing. At the beginning of the method you will check that flag and at the end of that specific loop you can reset it. The approach for accomplishing this is not specific to Kentico, it is a general programming task to avoid infinite loops. If I can help clarify anything, please let me know.

Best Regards,
Sandro

User avatar
Certified Developer 9
Certified Developer 9
r.zareianfard-gmail - 12/28/2013 2:49:57 PM
   
RE:Global Events Causes Loop
thanks for your reply,
in fact if I declare this variable inside method its worthless because the expression will always be true and If I declare this variable out of method's scope it will disable the event method completely for next changes because.the stop processing is not a flag for my loop it checks if there is no change then I will escape methods execution.

I know all about infinite loops but its not my case this is a logical loop as I'm updating entity again and its apparent that the event will call again.I think I need some property in the entity to check instead of declaring a variable.I mean I have to determine that event raised for this entity changes and shouldn't evaluate points again until user change the fields value.

User avatar
Member
Member
kentico_sandroj - 12/28/2013 4:30:51 PM
   
RE:Global Events Causes Loop
Hello,

Thank you for your reply. I have a better idea of what the issue is now, sorry for the misunderstanding. I believe you should call BizFormInfo.TYPEINFO.Events.Update.Continue = false; before doing the update in the loop, then set it back to true after the loop is done.
                BizFormInfo.TYPEINFO.Events.Update.Continue = false;
//update
BizFormInfo.TYPEINFO.Events.Update.Continue = true;
Please let me know if you have any issues with this approach.

Best Regards,
Sandro

User avatar
Certified Developer 9
Certified Developer 9
r.zareianfard-gmail - 12/28/2013 4:27:56 PM
   
RE:Global Events Causes Loop
My problem is just like FroggEye's problem in this post :
https://devnet.kentico.com/Forums/f68/fp6/t34320/User-Last-Login-Date.aspx

User avatar
Member
Member
kentico_sandroj - 12/28/2013 5:26:22 PM
   
RE:Global Events Causes Loop
Hello,

In this case the approach I have outlined should work: BizFormInfo.TYPEINFO.Events.Update.Continue = false;

I have tested it with DocumentEvents and I am setting up an example for BizForms at the moment. Please let me know if you run into any issues.

Best Regards,
Sandro

User avatar
Certified Developer 9
Certified Developer 9
r.zareianfard-gmail - 12/29/2013 12:43:56 AM
   
RE:Global Events Causes Loop
No chance, I have tested with ObjectEvents and used e.Object.TypeInfo.Events.Update.Continue = false; but no difference.

User avatar
Certified Developer 9
Certified Developer 9
r.zareianfard-gmail - 12/29/2013 2:42:06 AM
   
RE:Global Events Causes Loop
Any Idea??!!!

User avatar
Member
Member
kentico_sandroj - 12/29/2013 10:07:59 AM
   
RE:Global Events Causes Loop
Hello,

Please keep in mind that the forums are currently intended for Kentico community. Although we monitor them time to time, we cannot guarantee a timely response. If you need urgent help with Kentico CMS, please feel free to send your message to support@kentico.com.

As for the resolving the loop, you should set that property on the entire infoobject, not the object that is being handled:

BizFormInfo.TYPEINFO.Events.Update.Continue = false;

This seemed to work as expected for me, could you please confirm your hotfix so that I could check for any bugs?