Getting a value of a description

abdul_qulatein
Giga Expert

I am trying to get a value from a description field then querying the location table against the value, but it doesn't seem to work. Have I missed anything in the script below?

(function executeRule(current, previous /*null when async*/) {

  //gs.log("ContextID: "+contextID, "AV Created Incident");

  gs.log("AV Location Script");

  var contextID = "";

  var AVdescription = current.description;

  if(AVdescription.indexOf("Context:") > -1){

  contextID = AVdescription.match("Context:(.*?)&");

  contextID = contextID[1];

  gs.log(contextID);

  //var search_phrase = "****************************************************";

  //var search_description = current.description.substring(0, current.description.indexOf(search_phrase));

  //s.setStringParameter('description', search_description);

  var locationRecord = new GlideRecord('cmn_location');

  locationRecord.addQuery('u_context_id', contextID);

  locationRecord.query();

  if (locationRecord.next()) {

  current.location = locationRecord.sys_id;

  current.company = locationRecord.company;

  gs.log(locationRecord);

  } else {

  sComments = '<br>WARNING - THIS ALERT DOES NOT HAVE A CORRESPONDING SITE IN SERVICENOW - PLEASE INFORM THE LOCAL SERVICENOW ADMIN<br><br>'+ sComments;

  }

  }

  })(current, previous);

1 ACCEPTED SOLUTION

Never mind got it working. Thank you very much for your help.



Here is the final script:



(function executeRule(current, previous /*null when async*/) {


  gs.log("AV Location Script - Start");


  var desc = current.description;


  var pos = desc.search('Context:');


  var contextID = desc.substring(pos+8);


  contextID = contextID.trim();



  var locationRec = new GlideRecord('cmn_location');


  locationRec.addQuery('u_context_id', contextID);


  locationRec.query();


  if (locationRec.next()) {


  current.location = locationRec.sys_id;


  current.company = locationRec.company.sys_id;


  }


  else {


  current.work_notes = '<br>WARNING - THIS ALERT DOES NOT HAVE A CORRESPONDING SITE IN SERVICENOW - PLEASE INFORM THE LOCAL SERVICENOW ADMIN.';


  }


  //gs.log("AV Location Script - End");


})(current, previous);


View solution in original post

13 REPLIES 13

Manik
ServiceNow Employee
ServiceNow Employee

Hi Abdul,



Can you please try adding gs.info log and try checking what value is actually being printed for contextID variable., I would also suggest you to add log for description value. Please share the values which you are getting in logs.



Thanks,


Manik



PS - Please mark helpful, like or comment if applicable


I am not getting any value returned in the logs.


Manik
ServiceNow Employee
ServiceNow Employee

I am expecting that you won't be getting value for contextID but what is the value that is getting printed for Description field?


I want to know that value or may be you can share the value of Description field from the record.



Thanks,


Manik


This is what's on the description field.


Category: System Compromised


Context: 7a7307fa-07e7-ed7b-e736-99bfcf182f3f