g_form.save() command causing looping behavior

Dom G
Tera Expert

The g_form.save() in the below client script is causing looping behavior. Any suggestion on how to correct this?

Thank you..

 

var opened_by;
var requested_for;

requested_for = g_form.getValue('requested_for').toString();
opened_by = g_form.getValue('opened_by').toString();

if (opened_by != requested_for) {
//alert('hello');
g_form.setValue('watch_list', g_form.getValue('watch_list').toString() + ',' + g_form.getValue('opened_by').toString());
g_form.save();

1 ACCEPTED SOLUTION

Why don't you write display Business rule to set Value?

You can setValue while form is displayed to end user and it will save the value to backend.

find_real_file.png

 

In script you can use:

find_real_file.png

 

(function executeRule(current, previous /*null when async*/) {
	
if(current.watch_list.toString().indexOf(current.opened_by.toString())<0){
	current.watch_list = current.watch_list+','+current.opened_by;
	current.update();
}

})(current, previous);

 

Using current.update(); in business rule is not recommended but you can use it in display BR. It will not impact performance as this BR only works when form is being displayed, so it will not make recursive call. 

 

This way you can achieve the same results.

if you have observed g_form.save() saves the form but the values are not being saved that's why checking opened_by in watchlist was not working. g_form.save() workes in UI Action and UI pages.

 

Thanks,
Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

View solution in original post

9 REPLIES 9

As suggested by AnirudhKumar, you can remove the g_form.save(); from onload client script. As it is onLoad and saving right after loading the form automatically will be recursive.

 

An alternative way is to apply save only when watchlist is updated.

Modify your logic as given below:

if (opened_by != requested_for) {
var watch_list = g_form.getValue('watch_list).toString();
if(watch_list.indexOf(opened_by)<0){
//alert('hello');
g_form.setValue('watch_list', g_form.getValue('watch_list').toString() + ',' + g_form.getValue('opened_by').toString());
g_form.save();
}
}

 

This way it will save your form only once if. If user is aleready added to watch list then it will not call g_form.save() again.

 

Thanks,
Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

Hi Anil,

Thanks for the suggestion. That was a good idea. But it didn't work. It's still behaving the same way.

 

I'm not sure I understand, though. you're telling me to remove the g_form.save() from the onload client script. Then, how should I run it?

 

Why don't you write display Business rule to set Value?

You can setValue while form is displayed to end user and it will save the value to backend.

find_real_file.png

 

In script you can use:

find_real_file.png

 

(function executeRule(current, previous /*null when async*/) {
	
if(current.watch_list.toString().indexOf(current.opened_by.toString())<0){
	current.watch_list = current.watch_list+','+current.opened_by;
	current.update();
}

})(current, previous);

 

Using current.update(); in business rule is not recommended but you can use it in display BR. It will not impact performance as this BR only works when form is being displayed, so it will not make recursive call. 

 

This way you can achieve the same results.

if you have observed g_form.save() saves the form but the values are not being saved that's why checking opened_by in watchlist was not working. g_form.save() workes in UI Action and UI pages.

 

Thanks,
Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

Cool. If it's onload, still remove the g_form.save().

What will happen is this.

Form loads -> System sets the value on form (But its not saved back-end, but that's ok) -> After user works with the form, he has to save it by clicking submit/save -> That will cause the value you set to be saved back-end

Patrick71
Tera Contributor

When you save a form, you load the form again, hence the looping.