Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Need help to combine 2 arrays if one of them is empty or variable being used in array does not exist

GD11
Tera Expert

I have a variable set attached to 1 catalog item and there is flow designer that is integrated with othe 3rd party for 2 catalog items. i have variable which might have value for that variable set sometime but sometime it might be empty. so the code is failing to concatenate both arrays of both MRVS if 2nd one is empyt or is not present in catalog item because it is being shown based on need using UI policy. please advase what should we do to combine both arryas if MRVSJSON1 is empty or undefined in below code. right now flow designer is throwing inexpected error if we have mrvsjson1 as empty or not present in catalog item because of which payload is not getting sent over for 1st MRVS even if it has a value.

 

 

var mrvsJson = task.request_item.variables.ordervariable1;
var parsedData = JSON.parse(mrvsJson);
var arr = [];

for (var i = 0; i < parsedData.length; i++) {
    var obj = {};
    obj["Part"] = parsedData[i].part; // field name for part
    obj["Qty"] = parsedData[i].quantity; // field name for quantity
    arr.push(obj);
}
//gs.info('Value of MRVS ordervariable1: ' + JSON.stringify(arr));

// Get second MRVS (request_standalone_monitor)

var mrvsJson1 = task.request_item.variables.request_standalone_monitor;
var arr1 = [];
gs.info('value of the stand alone monitor 1 ' + mrvsJson1);
mrvsJson1 = String(mrvsJson1);
if(mrvsJson1 && mrvsJson1 != undefined){
gs.info('value of the stand alone monitor 2 ' + mrvsJson1);
//parsedData1 = JSON.stringify(parsedData1);

var parsedData1 = JSON.parse(mrvsJson1);
for (var j = 0; j < parsedData1.length; j++) {
    var obj1 = {};
    obj1["Part"] = parsedData1[j].part1; // field name for part
    obj1["Qty"] = parsedData1[j].qty; // field name for quantity
    arr1.push(obj1);
}
//gs.info('Value of MRVS request_standalone_monitor: ' + JSON.stringify(arr1));
arr1 = JSON.stringify(arr1);

} else {
    arr1 = JSON.stringify(arr);
}
// Combine both arrays
var combinedArr = arr.concat(arr1);

// Assign combined array to source_variables
source_variables['OrderVariable1'] = JSON.stringify(combinedArr);

gs.info('Final combined OrderVariable1: ' + source_variables['OrderVariable1']);
 
               
1 ACCEPTED SOLUTION

@GD11 

can you give me your business requirement and explain in detail what's required?

I assume this is what you want

-> 2nd MRVS or 1st MRVS if empty should not be appended

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

View solution in original post

12 REPLIES 12

Hi @Ankur Bawiskar  the business reuiqrement 

there are 2 mrvs , - for laptop and peripheral bundle, 2nd - addtional monitor. 1st one will never be emtpy as it is present on both catalog items. 2nd one is present only on 1 catalog item so there are chances that it not present and value also will not be there.

so we need to append both in ordervariable1 in both cases. the code should work in either of the case.

 

Thank you in advance for your help on this!

@GD11 

then here is the working logic, now you can incorporate it in your code

var mrvs1 = current.variables.mrvsVariableSet1NameHere;

var mrvs2 = current.variables.mrvsVariableSet2NameHere;

if(mrvs2 && mrvs2 != ''){
	// then only append 2nd mrvs
}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

Thanks only additional check of  mrvsJson1 != 'undefined' was required because it was coming as undefined wherever the variable itself was not present