Use script to find most recently inserted record in a table

JJG
Kilo Guru

Hello,

I have a script include that looks up records in my 'hour tracker' table. It finds records that match the name of the employee parameter. Now I just need to find the most recently inserted record so that I can pull field values from it. How can this be done? 

This is what I have so far:

get_lastContract: function() {
     var grEnd = new GlideRecord('x_utsll_time_manag_hour_tracker');
     grEnd.addQuery('name_of_employee', this.getParameter('sysparm_nameEmployee'));
     grEnd.query();
     if (grEnd.next()){


     }

1 ACCEPTED SOLUTION

Mark Roethof
Tera Patron
Tera Patron

Just checked on my PDI, because it was a theoretical answer. Though checked if it also works for scoped: works fine.

(function() {

	var gr = new GlideRecord('x_utsll_time_manag_hour_tracker');
	gr.addQuery('name_of_employee', this.getParameter('sysparm_nameEmployee'));
	gr.orderByDesc('sys_created_on');
	gr.setLimit(1);
	gr._query();

	if(gr._next()) {
		gs.info(gr.getDisplayValue() + ', created: ' + gr.getValue('sys_created_on'));
	}

})();

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

View solution in original post

2 REPLIES 2

Mark Roethof
Tera Patron
Tera Patron

Hi JJG,

This code would give me the most recent created incident. So just adjust it to your table.

(function() {

	var gr = new GlideRecord('incident');
	gr.orderByDesc('sys_created_on');
	gr.setLimit(1);
	gr._query();

	if(gr._next()) {
		gs.info(gr.getDisplayValue() + ', created: ' + gr.getValue('sys_created_on'));
	}

})();

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Mark Roethof
Tera Patron
Tera Patron

Just checked on my PDI, because it was a theoretical answer. Though checked if it also works for scoped: works fine.

(function() {

	var gr = new GlideRecord('x_utsll_time_manag_hour_tracker');
	gr.addQuery('name_of_employee', this.getParameter('sysparm_nameEmployee'));
	gr.orderByDesc('sys_created_on');
	gr.setLimit(1);
	gr._query();

	if(gr._next()) {
		gs.info(gr.getDisplayValue() + ', created: ' + gr.getValue('sys_created_on'));
	}

})();

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn