onChange to onSubmit client script change

Jori
Giga Guru

Hi,

i made a client script that checks from case choise "is incident" and if value is 'Incident' the script creates automatically incident by calling the create incident ui action. How ever, i was requested to change this from onChange to onSubmit and im stuck 🙂

here is the code, can someone help me to adjust this to onSubmit. Now the code is trying to submit and asks if you want to leave the page and all unsaved work will be lost and no matter what you choose the sn_customerservice_case parent does not create the incident.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var incValue = g_form.getValue('u_incident');
if (incValue == 'Incident') {
gsftSubmit(null, g_form.getFormElement(), '48c8ae1a877313003c1c8467a7cb0b00');
} else {
return;
}
}

1 ACCEPTED SOLUTION

Rather than calling the UI action I'd just copy the content of the UI action's script into the BR.
Sure, if there are changes in the logic, you'd have to update it in two places, but it should still be manageable.

Only thing to note is that if the Ui action is client callable then it won't be possible to directly copy the script, but if it's just server side stuff then there's no problem.

View solution in original post

5 REPLIES 5

Sandeep Rajput
Tera Patron
Tera Patron

@Jori try the following script inside your onSubmit script.

 

function onSubmit() {
    //Type appropriate comment here, and begin script below   
    var incValue = g_form.getValue('u_incident');
    if (incValue == 'Incident') {
        gsftSubmit(null, g_form.getFormElement(), '48c8ae1a877313003c1c8467a7cb0b00');
    } else {
        return;
    }
}

Screenshot 2023-04-19 at 1.22.09 PM.png

Weird
Mega Sage

This is running on a case? Is there a specific reason to use a onSubmit client script?
You could just create a Before Insert Business Rule and have it run the same script as the UI action.

Of course there are some things that might not work directly and you'd need to figure out your requirement completely, but generally speaking if your UI action just creates an incident with content from the case, then your Business rule should work by copying the UI action content.

The Business rule should probably be after insert, but you might also want it to be after update if you want to check that the field changes to incident after an update on a existing case, but again it depends on your use case.

For onSubmit client script the behaviour is dependent on how the UI action is setup, but you at least have to note that onSubmit looks different from onChange script. If you've changed the type to onSubmit, then you also need to change the code to something like

function onSubmit() {
	var incValue = g_form.getValue('u_incident');
	if (incValue == 'Incident') {
		gsftSubmit(null, g_form.getFormElement(), '48c8ae1a877313003c1c8467a7cb0b00');
	} 

}

But I'd personally just use BR's if creating records during insert/update since those are simple and you don't have to worry about any client issues.

Jori
Giga Guru

hey, i tried both of the provided solutions before posting and they always fail when submitting. the page gives error that unsaved work etc. the onchange solution works, the onsubmit variation does not.

I quess BR would also be OK as long as it works 😄 

i have never tried to call ui action from business rule before. any tips for the script?

Rather than calling the UI action I'd just copy the content of the UI action's script into the BR.
Sure, if there are changes in the logic, you'd have to update it in two places, but it should still be manageable.

Only thing to note is that if the Ui action is client callable then it won't be possible to directly copy the script, but if it's just server side stuff then there's no problem.