- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-09-2019 12:18 AM
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
Solved! Go to Solution.
- Labels:
-
Service Catalog
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-09-2019 02:52 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-09-2019 12:27 AM
Something like this would help?
function onSubmit(){
var fir = g_form.getValue('FirstVariable');
var sec = g_form.getValue('SecondVariable');
var thir = g_form.getValue('ThirdVariable');
if(fir == ''||sec == '' || thir == ''){
alert('cannot happen');
return false;
}
else if(fir == 'something' || sec == 'something'){
g_form.setValue('FirstVariable','');
}
Please mark my response as correct and helpful if it helped solved your question.
-Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-09-2019 12:56 AM
Hi Annika,
Use the below code and pass\updated the variable names and validate variable values.
function onSubmit(){
var aVlue = g_form.getValue('A');
var bValue= g_form.getValue('B');
var thir = g_form.getValue('ThirdVariable');
if(aValue == 'X' && bValue == 'Y'){
return true;
}
else {
return true;
}
}
Note : Here return true means allows the form for submission else means not.
Regards,
Kotaiah Sadari
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-09-2019 01:10 AM
Hi Annika,
I assume the variable A and B in your scenario are of type Checkbox/Select Box/Reference. If so, please refer the below code for you requirement.
function onSubmit() {
var valueA = g_form.getValue('A');
var valueB = g_form.getValue('B');
//Check whether variable A has X and variable B has Y value
if(valueA == 'X' && valueB == 'Y'){
alert('You must select the X in the question of A and Y in the question of B');
return false;
}
}
If the variable types are different (for example Lookup Select Box) where you need to validate whether value X is selected among other selected values, then the approach would be different. Please let me know in that case for modified code.
Hope this helps. Please mark the answer Correct/Helpful based on the impact.
Regards,
Amlan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-09-2019 02:35 AM
Hi everyone,
Thanks for your input, but I still didn't manage to get it to work. Let's make it really simple: my only condition is "the value of variable A must be X" ( I managed to simplify the criteria). This variable is a Select Box. Just to clarify, I don't want to prevent submission of the form in case the value of variable A isn't X! Variable A is something that only becomes visible via UI policy after the customer makes specific selections.
When variable A becomes visible, and the customer chooses value X in it, a group of SelectBoxes become visible. I need all of these to be checked as a signature from the customer, so I used the cat. client script I posted in my starting post (checkboxes can't be made mandatory otherwise).
Now, I want "the value of variable A must be X" to be a condition for applying the "make checkboxes mandatory" -script above, not for submitting the form in general. Otherwise, the "make checkboxes mandatory" -script is always applied, which doesn't work for me, since the customer could make such selection where the checkboxes are never even displayed.
Sorry for not being specific in the start! Appreciate your help 🙂
Annika