- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2020 08:40 AM
I have kind of a tricky situation. I have a Catalog Item that when submitted (from the Service Portal), automatically creates and pre-populates a MRVS. Then, via the workflow, a Task is assigned to a particular team to complete the filling out of the MRVS which has been pre-populated.
Before they close the Task, I want to make sure that they have populated a particular variable in all the records. I tried to make that variable mandatory, but that is not invoked unless they edit the record. If they choose NOT to edit it, it is not enforced, so they are able to close the Task without doing all the required work.
I first tried looping through all the records to see if I could find any that do not have a value in that particular field, but was unable to get that to work (question can be seen here: https://community.servicenow.com/community?id=community_question&sys_id=dafff530dbad1050190dfb243996...).
So, I thought maybe coming at this a different way. Instead of trying to loop through and check each record, maybe it would be better just to get aggregate record counts. I found this solution here by
If I were table to get those two counts, then I could just compare them, and require those numbers to be equal before allowing them to close the Task.
Thanks
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-10-2020 11:19 AM
I know I've used the array of one variable before, but I can't remember where - might have been server-side where everything MRVS is easier. I can't find the right syntax now, so new approach. First, this will get you the JSON with the variable name in place of the sys_id
var secApp = g_form.getValue('security_applications');
secApp = secApp.toString().replace(/variable_sys_id/g,'has_account');
//get total record count
var obj = JSON.parse(secApp);
Then just loop through the array of objects to count the populated values
var count = 0;
for(var i=0;i<obj.length; i++){
if(obj[i].has_account != ''){
count ++;
}
}
alert (count);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-13-2020 08:04 AM
Brad,
Thank you very much, that did the trick! This is what the code now looks like:
function onSubmit(){
var secApp = g_form.getValue('security_applications');
secApp = secApp.toString().replace(/773d2ddedb59d010bc827afc0f961963/g,'has_account');
//get total record count
var obj = JSON.parse(secApp);
var totRec = obj.length;
var totPop = 0;
for(var i=0;i<obj.length; i++){
if(obj[i].has_account != ''){
totPop ++;
}
}
//check to see if populated records equal total records
if(totPop == totRec){
return true;
//otherwise, prevent submission and return alert
}else{
alert('You have not populated all records! Please try again!');
return false;
}
}
If they try to Close, Save, or Update it without editing every record, they now get the alert letting them know they have to complete all records. The State actually changes on the screen to "Close Completed", but doesn't save that change (might be a little confusing for users).
Now, to toss a monkey wrench in it, they have asked me is it possible to just "Save" their progress (there is a lot to go through, so they may not do it all at once). So that means I really only need the above code to run when they are attempting to close the Task. It should allow them to Save or Update it, if the State is not being changed to one of the Closed states.
I don't think I can use a Catalog Client Script to do something like that, can I? Do I need to change it to a Client Script or something else?
If you think that is a new question that I should post in a new thread, please let me know and I will do that. I cannot thank you enough for all that you have done for thus far!
Thanks again.
Joe
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-13-2020 08:24 AM
This will only run the check if the task is not being closed
function onSubmit(){
if(g_form.getValue('state') != 3 && g_form.getValue('state') != 4 && g_form.getValue('state') != 7){//State is not Closed Complete, Closed Incomplete, or Closed Skipped
var secApp = g_form.getValue('security_applications');
secApp = secApp.toString().replace(/773d2ddedb59d010bc827afc0f961963/g,'has_account');
//get total record count
var obj = JSON.parse(secApp);
var totRec = obj.length;
var totPop = 0;
for(var i=0;i<totRec; i++){
if(obj[i].has_account != ''){
totPop ++;
}
}
//prevent submission and return alert if populated records don't equal total records
if(totPop != totRec){
alert('You have not populated all records! Please try again!');
return false;
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-13-2020 08:55 AM
Oh wow, that was simpler than I thought. I just needed to reverse the logic, as I only want it to run when they are trying to close it. So I changed the IF statement to this:
if((g_form.getValue('state') == 3) || (g_form.getValue('state') == 4) || (g_form.getValue('state') == 7)){
Then, to avoid the confusion, when it is not allowing them to close it because they haven't edited all rows, I am setting the "State" to "Work in Progress", i.e.
//check to see if populated records equal total records
if(totPop == totRec){
return true;
//otherwise, prevent submission and return alert
}else{
alert('You have not populated all records! Please try again!');
g_form.setValue('state',2); //set to "Work in Progress" state
return false;
}
And it works exactly as we want it!
Thanks a ton for all your help! It is greatly appreciated!