- 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 09:47 AM
OK thanks for the clarification. I have made a few tweaks that will hopefully work:
1. I have had a sore forehead because of pointer issues in loops where the code looses access to the right value its working with. Using toString() or + "" forces the code to query the server for the actual value versus using whats in memory. Since you are referencing record.serial_number quite a bit in your code, I declare a serialNumber variable at the top and use it throughout the rest of the fuction.
2. I also changed your If statement to use gs.nil() function which checks for undefined and null for you.
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);
//This for loop is to iteriate through the Dell JSON Warranty block
for (var i = 0; i < warranty.length; i++) {
var existingWarranty = checkServiceLevel(serialNumber, warranty[i]);
if (existingWarranty == true) {
InsertWarranty(serialNumber, warranty[i]);
}
else {
voidWarranty(serialNumber, warranty[i]);
}
}
}
}
}
counter++;
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2018 10:17 AM
This has definitely solved one of the issues I was running into where it was losing its pointer value. Now it is iterating though each array properly. However, I am still have an issue where it is overwriting the first value in the Service Level field. If it has No Support it shouldn't have a date. The reason a date is getting plugged in is because of the fact that when it sees ProSupport it plugs that date but then the ProSupport text is overwritten.
Here is the audit trail where it can be seen that it is being overwritten.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2018 10:40 AM
What do you mean by this:
I am still have an issue where it is overwriting the first value in the Service Level field.
I see the screenshot above but is this happening to every computer or just the "first" one? Do you have a whitelisted JSON sample you can post that you get from Dell?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2018 10:48 AM
I am interested in seeing what this data looks like:
var warranty = parsed.AssetWarrantyResponse[0].AssetEntitlementData;
You can log it by:
gs.log(new JSON().encode(warranty));
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2018 10:54 AM
This is what the data looks like.
In Dell's Sandbox it looks like this.
I am iterating through each of those AssetEntitlement elements looking for one that has a ServiceLevelDescription of ProSupport. If it contains this then update the record with the EndDate and the description of ProSupport. Where I think my script is having the issue is that it is evaluating both of the functions if it does have ProSupport.