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,



You can try using this code and it would return Context ID.


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


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


gs.log(res);



Thanks,


Manik



PS - Please mark correct, like or helpful if applicable


Manik
ServiceNow Employee
ServiceNow Employee

Yes Abdul,   you can use trim to remove spaces.



Thanks,


Manik



PS - Please mark correct, like or helpful if applicable



Deepa Srivastav
Kilo Sage

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


Hi Deepa,



I am actually trying to use this right now... but i need to manipulate this script a little bit. Any idea how best to make this a catalog client script?



Any help would be greatly appreciated!


Thanks!