Best way to loop through Catalog Item variables to find "true" values

MBarrott
Mega Sage

I have a Catalog Item which has a large number of checkboxes, each corresponding to a particular access that needs to be granted. 

 

What I would like to do is loop through all these variables, identify which are "true" and then pass the variable name to a new variable, essentially building a string containing all access needed. These particular vars have specific naming structures (all uppercase, starting with name structures such as "AR_" and "AP_").

 

I couldn't see any optimal Actions or Flow Logic to achieve this. Is looping through the fd_data and setting a flow variable the optimal solution?

2 ACCEPTED SOLUTIONS

Ankur Bawiskar
Tera Patron
Tera Patron

@MBarrott 

if you are using flow then no straight way.

You can use inline script and get all the variables and iterate

Something like this in flow on your catalog item but please enhance

var ritmSysId = fd_data.trigger.request_item.sys_id;

var gr = new GlideRecord("sc_req_item");
gr.addQuery("sys_id", ritmSysId);
gr.query();
if (gr.next()) {
    var variables = gr.variables.getElements();
    for (var i = 0; i < variables.length; i++) {
        var question = variables[i].getQuestion();
        var label = question.getLabel();
        var value = question.getDisplayValue();
        if (!label.startsWith('AR_') && !label.startsWith('AP_')) {
            // your logic to append
        }
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

MBarrott
Mega Sage

Final solution:

 

Big thanks for Ankur for the initial scripting. After some modifications I was able to achieve the desired result. 

 

Script below if people would like to use it. Some of the variables are not used but could be useful to others for further modifying:

// get request item sys_id and declare var(s)
var ritmSysId = fd_data.trigger.request_item.sys_id;
var resultString = '';

// instantiate GlideRecord class on sc_req_item and query against respective sys_id
var gr = new GlideRecord("sc_req_item");
gr.addQuery("sys_id", ritmSysId);
gr.query();

// check if record is found
if (gr.next()) 
{
	// get all elements from respective record
    var variables = gr.variables.getElements();
	
	// loop through all respective elements
    for (var i = 0; i < variables.length; i++) 
    {
        var question = variables[i].getQuestion(); // gets respective element
        var label = question.getLabel(); // gets the user-friendly label
        var value = question.getDisplayValue(); // gets the inputted/displayed value of the var
        var resName = question.getName(); // gets the name of the var
		
		if(value == "true")
		{
			if (resName.startsWith('AB_') || resName.startsWith('AR_') || resName.startsWith('..........') 
			{
				resultString += resName + '\n';
			}
		}
    }
}

// Return the result string
return resultString;

View solution in original post

6 REPLIES 6

Mark Manders
Mega Patron

That's the only way to go, because you need to check on each one individually. And the Flow variable will have the list you need at the end.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Ankur Bawiskar
Tera Patron
Tera Patron

@MBarrott 

if you are using flow then no straight way.

You can use inline script and get all the variables and iterate

Something like this in flow on your catalog item but please enhance

var ritmSysId = fd_data.trigger.request_item.sys_id;

var gr = new GlideRecord("sc_req_item");
gr.addQuery("sys_id", ritmSysId);
gr.query();
if (gr.next()) {
    var variables = gr.variables.getElements();
    for (var i = 0; i < variables.length; i++) {
        var question = variables[i].getQuestion();
        var label = question.getLabel();
        var value = question.getDisplayValue();
        if (!label.startsWith('AR_') && !label.startsWith('AP_')) {
            // your logic to append
        }
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@MBarrott 

Thank you for marking my response as helpful.

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

MBarrott
Mega Sage

Final solution:

 

Big thanks for Ankur for the initial scripting. After some modifications I was able to achieve the desired result. 

 

Script below if people would like to use it. Some of the variables are not used but could be useful to others for further modifying:

// get request item sys_id and declare var(s)
var ritmSysId = fd_data.trigger.request_item.sys_id;
var resultString = '';

// instantiate GlideRecord class on sc_req_item and query against respective sys_id
var gr = new GlideRecord("sc_req_item");
gr.addQuery("sys_id", ritmSysId);
gr.query();

// check if record is found
if (gr.next()) 
{
	// get all elements from respective record
    var variables = gr.variables.getElements();
	
	// loop through all respective elements
    for (var i = 0; i < variables.length; i++) 
    {
        var question = variables[i].getQuestion(); // gets respective element
        var label = question.getLabel(); // gets the user-friendly label
        var value = question.getDisplayValue(); // gets the inputted/displayed value of the var
        var resName = question.getName(); // gets the name of the var
		
		if(value == "true")
		{
			if (resName.startsWith('AB_') || resName.startsWith('AR_') || resName.startsWith('..........') 
			{
				resultString += resName + '\n';
			}
		}
    }
}

// Return the result string
return resultString;