How to run 2 onSubmit client scripts based on different conditions.

Pratiksha Lang1
Kilo Sage

How to run 2 onSubmit client scripts based on different conditions. when I am trying to run it, both the scripts are running. In this 2 client scripts the only difference is management level condition. but it is showing both the confirm boxed from both the client scripts.

 

1st Client script :

 

function onSubmit() {
   //Type appropriate comment here, and begin script below
   var refusalStatus = g_form.getValue('u_refusal_status');
    var refusalReason = g_form.getValue('u_refusal_closure_reason');
    var managementLevel = g_form.getValue('u_management_level');
    alert(managementLevel);
    if((managementLevel == 6 || 7 || 8 || 9 || 10 || 11) && (refusalStatus == 'invalid') && (refusalReason == 'Continues to disagree')){
        var answer = confirm("if refusal reason is chosen as Invalid then a Warning Letter will be sent to the employee. Do you want to proceed?");
        if (answer == true) {
        // I have selected dummy fields but you can select as per your needs
    } else {
        g_form.setValue('u_refusal_status', '');
        g_form.setValue('u_refusal_closure_reason', '');
        return false;
 
 
2nd Client script :
 
function onSubmit() {
   //Type appropriate comment here, and begin script below
   var refusalStatus = g_form.getValue('u_refusal_status');
    var refusalReason = g_form.getValue('u_refusal_closure_reason');
    var managementLevel = g_form.getValue('u_management_level');
    alert(managementLevel);
    if((managementLevel == 12 || 13) && (refusalStatus == 'invalid') && (refusalReason == 'Continues to disagree')){
        var answer = confirm("Selecting Refusal Closure Reason as 'Continues to disagree' will result in issuance of a redundancy letter");
        if (answer == true) {
        // I have selected dummy fields but you can select as per your needs
    } else {
        g_form.setValue('u_refusal_status', '');
        g_form.setValue('u_refusal_closure_reason', '');
        return false;
       
    }
}
}
4 REPLIES 4

Suyog Aptikar
Giga Guru

Can you try -> if (managementLevel == 12 || managementLevel ==13) and see if this helps

You need to do same for 1st client script.

What you have written is essentially passing for all criteria values.

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.



Best regards

Suyog

I agree with @Suyog Aptikar , you need to include the variable. Also the first script lacks the closing brackets but maybe that is just in your post.

Regards,
Niklas

or perhaps if (managementLevel <12) for the first script and if (managementLevel >=12) for the second. I would also recommend to combine the logic into one script. Makes it a lot easier to maintain.

 

Regards,
Niklas

Vanderlei
Mega Sage

Hi @Pratiksha Lang1 how are you?
I made this code.

 

 

function onSubmit() {
//Type appropriate comment here, and begin script below
var refusalStatus = g_form.getValue('u_refusal_status'),
refusalReason = g_form.getValue('u_refusal_closure_reason'),
managementLevel = g_form.getValue('u_management_level'),
managementLevelCondition1 = [6,7,8,9,10,11],
managementLevelCondition2 = [12,13],
textMessage;

if((refusalStatus == 'invalid') && (refusalReason == 'Continues to disagree')){
    //Get the Text Message
    if(managementLevelCondition1.indexOf(managementLevel) != "-1"){
        textMessage = "if refusal reason is chosen as Invalid then a Warning Letter will be sent to the employee. Do you want to proceed?"
    }else if(managementLevelCondition2.indexOf(managementLevel) != "-1"){
        textMessage = "Selecting Refusal Closure Reason as 'Continues to disagree' will result in issuance of a redundancy letter"
    }

    /* Show the modal if has Message */
    if(textMessage){
        var answer = confirm(textMessage)

        if(answer){
            // I have selected dummy fields but you can select as per your needs
        }
        else{
            g_form.setValue('u_refusal_status', '');
            g_form.setValue('u_refusal_closure_reason', '');
            return false
        }
    
    }

}

 

 

 

It basically unifies the two Client Script on Submit, reusing the variables and conditions.
As the conditions are very similar and the behavior of both scripts is the same, you can unify the conditions and just change the popup text

 

If my answer helped you, please mark my answer as helpful.

 

Vanderlei Catione Junior | LinkedIn

Senior ServicePortal Developer / TechLead at The Cloud People