incident record from a sys_id?

showsid02
Tera Contributor

How to get incident record from a sys_id in script??

6 REPLIES 6

Sagar Pagar
Tera Patron

Hi @showsid02,

 

Try this sample scripts to get incident record using scripts.

 

var incidentRecord = new GlideRecord('incident');
incidentRecord.addEncodedQuery("sys_id=add_sys_id_here");
incidentRecord.query();
if (incidentRecord.next()) {

	gs.info(incidentRecord.getValue('number')); // get or print other fields as per need

}

 

 

If my response helps to solve your issue. Kindly mark it as helpful & correct. It will be helpful for future readers! 👍🏻
Thanks,
Sagar Pagar

The world works with ServiceNow

Or even easier, with a get:

var incidentRecord = new GlideRecord('incident');
if (incidentRecord.get('<<sys_id>>')){
    gs.info(incidentRecord.getDisplayValue());
}

 


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

GTSNOW
Giga Guru

Hi,

 

You can fetch record from any table with following script.

var grI = new GlideRecord('incident');
grI.get('91d704f7473b61106809afb8036d43fc');//SYSID of Incident
gs.print(grI.number);

 

Try this in Background script. It will work.

 

If you found my response helpful or relevant to your query, please consider clicking the "Helpful" button or giving it a "Like". This not only helps others identify useful replies but also encourages contributors like me to continue assisting the community. Thank you for your support! 

Hi @GTSNOW,

 

You should never use a get without an if.
It makes sense to not do it in this case, because you probably already know this sys_id exists, however it can be taken as an example in an automated script, where it may not be the case there always is a valid sys_id which will lead to unintended results.


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.