How to update the select box variable based on the list collector variable values in servicenow.

Balaram7
Kilo Sage

Hi all,

 

iam stuck with the below requirement can anyone please help me out.

There is a custom table named "u_cost_manage" in that table there are three custom fields.

Supplier (reference) field, branch string field, and partner choice box (True/false)

Below is the requirement in a catalog item,

We have a list collector variable named "Branch" list collector, if the branch records are selected then if one of the records has partner as true, then we need to mark the Trusted partner variable select box (True/false) as true else it should be false.

 

Below is the client script i have tried.

 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || !newValue){
    return ;
   }
 var ga = new GlideAjax('CheckTrustedpartner');
 ga.addParam('sysparam_name','check');
 ga.addParam('sysparam_ids',newValue);
 ga.getXML(function(response){
    var answer = response.responseXML.documentElement.getAttribute("answer");
    g_form.setValue('trusted_partner',answer == 'true');
 });


}
 
 
Please help me on this, how to get the trusted partner variable marked as true when one of the list collector record contains the partner field value marked as true.
 
 
Thank you,
Balaram.
3 REPLIES 3

Tanushree Maiti
Tera Sage

Hi @Balaram7 

 

Try this one-

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
 
var selList = g_form.getValue('your_variable');
 var selArray = selList.split(',');
for (var i = 0; i < selArray.length; i++) {
        
// Do what you want to the retrieved record
 
var ga = new GlideAjax('CheckTrustedpartner');
        ga.addParam('sysparam_name','check');
       ga.addParam('sysparam_ids',selArray[i]);
       ga.getXML(function(response){
       var answer = response.responseXML.documentElement.getAttribute("answer");
   if (answer){
       g_form.setValue('trusted_partner',true);
   break;
   }
       }
 
        }
}
Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin:

yashkamde
Mega Sage

Hello @Balaram7 ,

 

I will recommend try using g_form.getValue instead of newValue on a List Collector, it returns a string of Sys IDs separated by commas ("sys_id1,sys_id2"). This is exactly what the IN operator in the Script Include needs.

 

If still doesn't work where are you stuck or what's the issue coming send screenshot..

If my response helped mark as helpful and accept the solution.

Aditya_hublikar
Mega Sage

Hello @Balaram7 ,

 

List collector give value like val1,val2,val3 in comma separated string , and you have to check whether each branch is trusted or not  so you can first convert string value into array format using stringVariable.split(',') method and then send each value in your glideAjax using for loop  then you can get your required response true or false , once get true then break the for loop and update that trusted partner checkbox as true .

 

You can refer code suggested by @Tanushree Maiti   but just  break the loop once response get true .

 

If this helps you then mark it as helpful and accept as solution.

Regards,

Aditya