How to get the sys_id of 100 records

preethigovi
Tera Contributor

Hi Team,

I need sys id of 100 records by passing short description. The short desc may contain ',(,& in it. How we can get sys id of this records,

 

eg: short description :

Discuss New Hire's Career Journey
4 ACCEPTED SOLUTIONS

Medi C
Giga Sage

@preethigovi,

Are you having a list of short descriptions that you need to look for the record sys_ids? If so, please try:

function getSysIds(shortDescriptions) {
	var result = [];
    var grIncident = new GlideRecord("incident"); //Replace with the table you want
    grIncident.addQuery("short_descriptionIN" + shortDescriptions);
    grIncident.query();

    while (grIncident.next()) {
        result.push(grIncident.sys_id);
    }
	return result;
}


//Usage: Example
var shortDescriptions = ["Discuss New Hire's Career Journey", "TEST SHort Description"];
var sysIds = getSysIds(shortDescriptions);
gs.info(sysIds);

//Results:
//Script: 690923053b6caed0e301de2a85e45a4b,690923053b6caed0e301de2a85e45a4b

If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.

View solution in original post

Rohit  Singh
Mega Sage

Hi @preethigovi ,

 

You can write a script.

 

var sysid = [];
var gr = new GlideRecord("table_name");
gr.addQuery("short_descriptionLIKE
Discuss New Hire's Career Journey");
gr.setLimit(100);  // Limiting 100 record to get the sys_id.
gr.query();
while(gr.next())
{
  sysid.push(gr.sys_id);

}

gs.print(sysid);

 

For optimum result you can go to the table list view and build the query using filter and copy the filter. Use the copied filter in line gr.addQuery("paste_copied_filter_query")

 

If my response helped, please hit the Thumb Icon and accept the solution so that it benefits future readers.

 

Regards,
Rohit

View solution in original post

Ankur Bawiskar
Tera Patron
Tera Patron

@preethigovi 

since you only want 100 records, please use setLimit()

var shortDescription = "Discuss New Hire's Career Journey";
var gr = new GlideRecord('incident');
gr.addQuery('short_description', 'LIKE', shortDescription);
gr.setLimit(100);
gr.query();
var sysIds = [];
while (gr.next()) {
    sysIds.push(gr.getValue('sys_id'));
}
gs.info('Sys IDs: ' + sysIds.join(', '));

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

Juhi Poddar
Kilo Patron

Hello @preethigovi 

To retrieve the sys_id of records where the short description contains special characters like ', (  &, try this script:

var sys_id_arr = [];
var gr = new GlideRecord('incident');
gr.addEncodedQuery("short_descriptionLIKE'^ORshort_descriptionLIKE,^ORshort_descriptionLIKE(^ORshort_descriptionLIKE&");
gr.setLimit(100);
gr.query();
while(gr.next()){
	sys_id_arr.push(gr.getUniqueValue());
}
gs.print("Sys IDs: " + sys_id_arr.join(', '));

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps future readers to locate the solution easily and supports the community!"

 

Thank You
Juhi Poddar

 

View solution in original post

14 REPLIES 14

Medi C
Giga Sage

@preethigovi,

Are you having a list of short descriptions that you need to look for the record sys_ids? If so, please try:

function getSysIds(shortDescriptions) {
	var result = [];
    var grIncident = new GlideRecord("incident"); //Replace with the table you want
    grIncident.addQuery("short_descriptionIN" + shortDescriptions);
    grIncident.query();

    while (grIncident.next()) {
        result.push(grIncident.sys_id);
    }
	return result;
}


//Usage: Example
var shortDescriptions = ["Discuss New Hire's Career Journey", "TEST SHort Description"];
var sysIds = getSysIds(shortDescriptions);
gs.info(sysIds);

//Results:
//Script: 690923053b6caed0e301de2a85e45a4b,690923053b6caed0e301de2a85e45a4b

If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.

Hello @preethigovi,

Thank you for marking my response as helpful, if that helped resolved your query, please accept the solution and close the thread, so that it benefits future readers. 


If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.

Hello @preethigovi,

 

I hope you are doing well! Was this helpful? If so, kindly accept the solution and close the thread so that it benefits future readers. 


If you found this helpful, please hit the thumbs-up button and mark as correct. That helps others find their solutions.

Rohit  Singh
Mega Sage

Hi @preethigovi ,

 

You can write a script.

 

var sysid = [];
var gr = new GlideRecord("table_name");
gr.addQuery("short_descriptionLIKE
Discuss New Hire's Career Journey");
gr.setLimit(100);  // Limiting 100 record to get the sys_id.
gr.query();
while(gr.next())
{
  sysid.push(gr.sys_id);

}

gs.print(sysid);

 

For optimum result you can go to the table list view and build the query using filter and copy the filter. Use the copied filter in line gr.addQuery("paste_copied_filter_query")

 

If my response helped, please hit the Thumb Icon and accept the solution so that it benefits future readers.

 

Regards,
Rohit