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

Ok and what is it you are trying to retrieve from it? I mean what value?



Thanks,


Manik


I want to use the context id from the description field to query the location table which has the same contextID populated then assign the sys_id of the location to the current incident record.



So use this '7a7307fa-07e7-ed7b-e736-99bfcf182f3f' to query the location table there should only be 1 location associated with this contextID then to append the location sys_id onto the current incident record.



Hope this make sense!


Thanks Manik that works.



Just one last tweak: how can I remove the space before the contextID?



I am getting this: ' 7a7307fa-07e7-ed7b-e736-99bfcf182f3f' insteade of '7a7307fa-07e7-ed7b-e736-99bfcf182f3f'


you can use trim function for removing extra spaces...



http://wiki.servicenow.com/index.php?title=Eliminate_Leading_and_Trailing_Spaces_in_Fields#gsc.tab=0


Mark Correct if it solved your issue or hit Like and Helpful if you find my response worthy.


Thanks,
Deepa


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);