- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2018 08:48 AM
Hello All,
I have a for loop that is overwriting values. I have multiple arrays that are being evaluated, if there is a specific element in the array run a function and if there if this element is not in the array run another function. The issue that I am having is that it will run both of the functions at the same time. How can I get the loop to evaluate only one of the functions at a time?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2018 11:19 AM
OK I believe I know what's going on and its a sequencing issue. In the screenshot you provided, I don't see one with ProSupport so not 100% sure. But want I believe is happening is this:
- Your API Query is coming back with 5 AssetEntitlement results
- ProSupport could be in item 2 but the code continues to process items 3-5 which is overwriting the ProSupport after that has been updated.
- If ProSupport is item 2, then you really don't need to process items 3-5 correct?
So we really need to process all of them to determine the type of update to perform instead of multiple updates. If ProSupport is found, stop the loop and update the CI.
The code below should solve your problem. Notice I removed one of the parameters to the voidWarranty since it wasn't being used.
Hopefully this solves your issue and if so please mark any post helpful and/or the correct answer to your question.
var companySysIds = ['955fe9d04fa882005fed650f0310c75e', //Dell Computer Corporation
'041f69d04fa882005fed650f0310c7f1', //Dell Inc.
'8fcad87b4f172a4036cf76601310c7d3']; //Dell__
var answer, test;
dellIntegration();
function dellIntegration() {
var record = new GlideRecord('cmdb_ci_computer');
record.addQuery('manufacturer','IN',companySysIds);
record.addNotNullQuery('serial_number');
record.query();
var counter = 0;
var apiLimit = 6;
//gs.print("There are " + record.getRowCount() + " records to check for Dell's Warranty Status");
while (record.next()) {
if (counter < apiLimit) {
var serialNumber = record.serial_number.toString();
var needsUpdate = checkWarranty(serialNumber);
if (needsUpdate == true) {
gs.print("Querying Dell's Warranty API: " + " Serial Number: " + serialNumber + " Manufacturer: " + record.manufacturer.getDisplayValue());
//Set up the API request by calling the Outbound API Rest Message record
var request = new sn_ws.RESTMessageV2('Dell Warranty Sandbox', 'Get');
request.setStringParameter('serviceTag',serialNumber);
var response = request.execute();
var responseBody = response.getBody();
//gs.print("Dell Warranty API Response: " + responseBody);
//JSONParser Script Include is available with the JSON Web Service Plugin
var parser = new JSONParser();
var parsed = parser.parse(responseBody);
var result = parsed.AssetWarrantyResponse;
if (result != null) {
var warranty = parsed.AssetWarrantyResponse[0].AssetEntitlementData;
if (!gs.nil(warranty)) {
//gs.print(warranty);
// Set flag to capture if ProSupport, set default to false
var existingWarranty = false;
//This for loop is to iteriate through the Dell JSON Warranty block
for (var i = 0; i < warranty.length; i++) {
if (checkServiceLevel(serialNumber, warranty[i])) {
existingWarranty = true;
var warrantyDetails = warranty[i];
break; // Stop loop since we don't need to continue
}
}
if (existingWarranty == true) {
InsertWarranty(serialNumber, warrantyDetails);
} else {
voidWarranty(serialNumber);
}
}
}
}
counter++;
}
}
}
function checkWarranty(api_serial_number){
var gr = new GlideRecord('cmdb_ci_computer');
gr.addQuery('serial_number', api_serial_number);
gr.query();
while (gr.next()){
if (gr.u_service_level == '') {
return true;
}
else {
return false;
}
}
}
function checkServiceLevel(api_serial_number, api_warranty){
if (api_warranty.ServiceLevelDescription == "ProSupport") {
return true;
}
else {
return false;
}
}
function voidWarranty(api_serial_number) {
var gr = new GlideRecord('cmdb_ci_computer');
gr.addQuery('serial_number',api_serial_number);
gr.query();
while(gr.next()) {
gr.u_service_level = "No Support"
gr.short_description = "VoidWarranty Function Executed"
gr.update();
}
}
function InsertWarranty(api_serial_number, api_warranty) {
var gr = new GlideRecord('cmdb_ci_computer');
gr.addQuery('serial_number',api_serial_number);
gr.query();
while(gr.next()) {
gr.warranty_expiration = api_warranty.EndDate;
gr.u_service_level = api_warranty.ServiceLevelDescription;
gr.short_description = "InsertWarranty Function Executed";
gr.update();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2018 08:52 AM
Can you share your code? Its hard to provide recommendations without seeing what you have tried.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2018 08:56 AM
Here is the script, what is happening is that I am able to parse the JSON string and grab the array that I want. I want to be able to evaluate the array and input the warranty information if a certain warranty type appears. If that warranty type appears input "No Support". The issue is that it will do what I'd like for the first few then it will start to overwrite the values that I have. Essentially running both functions.
var companySysIds = ['955fe9d04fa882005fed650f0310c75e', //Dell Computer Corporation
'041f69d04fa882005fed650f0310c7f1', //Dell Inc.
'8fcad87b4f172a4036cf76601310c7d3']; //Dell__
var answer, test;
dellIntegration();
function dellIntegration() {
var record = new GlideRecord('cmdb_ci_computer');
record.addQuery('manufacturer','IN',companySysIds);
record.addNotNullQuery('serial_number');
record.query();
var counter = 0;
var apiLimit = 6;
//gs.print("There are " + record.getRowCount() + " records to check for Dell's Warranty Status");
while (record.next()) {
if (counter < apiLimit) {
var needsUpdate = checkWarranty(record.serial_number);
if (needsUpdate == true) {
gs.print("Querying Dell's Warranty API: " + " Serial Number: " + record.serial_number + " Manufacturer: " + record.manufacturer.getDisplayValue());
//Set up the API request by calling the Outbound API Rest Message record
var request = new sn_ws.RESTMessageV2('Dell Warranty Sandbox', 'Get');
request.setStringParameter('serviceTag',record.serial_number);
var response = request.execute();
var responseBody = response.getBody();
//gs.print("Dell Warranty API Response: " + responseBody);
//JSONParser Script Include is available with the JSON Web Service Plugin
var parser = new JSONParser();
var parsed = parser.parse(responseBody);
var result = parsed.AssetWarrantyResponse;
if (result != null) {
var warranty = parsed.AssetWarrantyResponse[0].AssetEntitlementData;
if (warranty != undefined && warranty != null) {
gs.print(warranty);
//This for loop is to iteriate through the Dell JSON Warranty block
for (var i = 0; i < warranty.length; i++) {
var existingWarranty = checkServiceLevel(record.serial_number, warranty[i]);
if (existingWarranty == true) {
InsertWarranty(record.serial_number, warranty[i]);
}
else {
voidWarranty(record.serial_number, warranty[i]);
}
}
}
}
}
counter++;
}
}
}
function checkWarranty(api_serial_number){
var gr = new GlideRecord('cmdb_ci_computer');
gr.addQuery('serial_number', api_serial_number);
gr.query();
while (gr.next()){
if (gr.u_service_level == '') {
return true;
}
else {
return false;
}
}
}
function checkServiceLevel(api_serial_number, api_warranty){
if (api_warranty.ServiceLevelDescription == "ProSupport") {
return true;
}
else {
return false;
}
}
function voidWarranty(api_serial_number, api_warranty) {
var gr = new GlideRecord('cmdb_ci_computer');
gr.addQuery('serial_number',api_serial_number);
gr.query();
while(gr.next()) {
gr.u_service_level = "No Support"
gr.short_description = "VoidWarranty Function Executed"
gr.update();
}
}
function InsertWarranty(api_serial_number, api_warranty) {
var gr = new GlideRecord('cmdb_ci_computer');
gr.addQuery('serial_number',api_serial_number);
gr.query();
while(gr.next()) {
gr.warranty_expiration = api_warranty.EndDate;
gr.u_service_level = api_warranty.ServiceLevelDescription;
gr.short_description = "InsertWarranty Function Executed";
gr.update();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2018 09:19 AM
Ben, thanks. Is this the area of code you are referring to?
if (result != null) {
var warranty = parsed.AssetWarrantyResponse[0].AssetEntitlementData;
if (warranty != undefined && warranty != null) {
gs.print(warranty);
//This for loop is to iteriate through the Dell JSON Warranty block
for (var i = 0; i < warranty.length; i++) {
var existingWarranty = checkServiceLevel(record.serial_number, warranty[i]);
if (existingWarranty == true) {
InsertWarranty(record.serial_number, warranty[i]);
}
else {
voidWarranty(record.serial_number, warranty[i]);
}
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2018 09:27 AM
You are correct, what is happening is this. If the JSON string doesn't have a certain element update the record with "No Support" and if it does have that element update that record with that warranty while also adding the expiration date. The screen shots below show the issue I'm having and how it's overwriting.
This is how it should be with no warranty.
This is how it is appearing with warranty.
My theory is that it is evaluating the array at each element but isn't stopping when it does find a match. So it will eventually overwrite the value when it moves to the next index.