How can I create variable conditions for an OnSubmit catalogue client script?

Annika Aaltonen
Kilo Guru

How can I create an OnSubmit catalogue client script (applies to a cat. item, UI type: Mobile/SP), but ONLY when let's say variable A has value X, and variable B has value Y on this cat. item? Technically I need a script that is first OnCondition and then OnSubmit, if that makes sense. Do I need to put my conditions in the script, in that case, please paste in here, how should it look like (I have absolutely minimal knowledge in javascript..) I'm using this OnSubmit catalogue client script I found here in the community:

function onSubmit(){
    //Set the mandatory checkbox variable names and total mandatory count here
    var mandatoryVars = ['var1','var2','var3'];
    var mandatoryCount = 1;
    
    var passed = forceMandatoryCheckboxes(mandatoryVars, mandatoryCount);
    if(!passed){
        //Abort the submit
        alert('You must select at least one checkbox.');
        return false;
    }
}

function forceMandatoryCheckboxes (mandatory, count) {
    // Variables used
	var answer = false;
    var varFound = false;
    var numTrue = 0;
	
    //Check each variable in the array
    for (i = 0; i < mandatory.length; i++) {
		//Check to see if variable is set to 'true'
		if (g_form.getBooleanValue(mandatory[i])) {
			numTrue ++;
			//Exit the loop if we have reached required number of 'true'
			if (numTrue >= count) {
				answer = true;
			}
		}
    }
    //Return true or false
    return answer;
}

 

This works just fine for the whole Catalogue item, but that's not what I need. Need to narrow it down to a scenario when  specific variables have a specific value/choice. I'd also need revert the effect of script when the conditions aren't true anymore. What's the best way to do this?

Thanks so much in advance!!

Annika

1 ACCEPTED SOLUTION

Hi Annika,

 

All you need to add one more condition in your 'make checkboxes mandatory' script. I have modified the script which you can refer below:

function onSubmit(){
//Here you are checking whether variable A has value X in it
if(g_form.getValue('variable_A') == 'X'){
    //Set the mandatory checkbox variable names and total mandatory count here
    var mandatoryVars = ['var1','var2','var3'];
    var mandatoryCount = 1;
    
    var passed = forceMandatoryCheckboxes(mandatoryVars, mandatoryCount);
    if(!passed){
        //Abort the submit
        alert('You must select at least one checkbox.');
        return false;
    }
 }
}

function forceMandatoryCheckboxes (mandatory, count) {
    // Variables used
	var answer = false;
    var varFound = false;
    var numTrue = 0;
	
    //Check each variable in the array
    for (i = 0; i < mandatory.length; i++) {
		//Check to see if variable is set to 'true'
		if (g_form.getBooleanValue(mandatory[i])) {
			numTrue ++;
			//Exit the loop if we have reached required number of 'true'
			if (numTrue >= count) {
				answer = true;
			}
		}
    }
    //Return true or false
    return answer;
}

 

Hope this helps. Please mark the answer Correct/Helpful based on the impact.

Regards,

Amlan

View solution in original post

6 REPLIES 6

Hi Annika,

 

All you need to add one more condition in your 'make checkboxes mandatory' script. I have modified the script which you can refer below:

function onSubmit(){
//Here you are checking whether variable A has value X in it
if(g_form.getValue('variable_A') == 'X'){
    //Set the mandatory checkbox variable names and total mandatory count here
    var mandatoryVars = ['var1','var2','var3'];
    var mandatoryCount = 1;
    
    var passed = forceMandatoryCheckboxes(mandatoryVars, mandatoryCount);
    if(!passed){
        //Abort the submit
        alert('You must select at least one checkbox.');
        return false;
    }
 }
}

function forceMandatoryCheckboxes (mandatory, count) {
    // Variables used
	var answer = false;
    var varFound = false;
    var numTrue = 0;
	
    //Check each variable in the array
    for (i = 0; i < mandatory.length; i++) {
		//Check to see if variable is set to 'true'
		if (g_form.getBooleanValue(mandatory[i])) {
			numTrue ++;
			//Exit the loop if we have reached required number of 'true'
			if (numTrue >= count) {
				answer = true;
			}
		}
    }
    //Return true or false
    return answer;
}

 

Hope this helps. Please mark the answer Correct/Helpful based on the impact.

Regards,

Amlan

Fantastic, thanks so much, Amlan!!

 

Best,

Annika