- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-12-2024 06:54 AM
Hello,
I've successfully obtained a list of Configuration Items (CIs) using a script include. However, I now aim to automatically populate a variable with the CI that is the latest created. How can I achieve this?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-14-2024 07:02 PM
Hi @Akshay03, you just need to amend your script include to order the returned results by the Created On [sys_created_on] field so the CI that was created last is the first record in the array result. You then populate your variable using the first item in your array:
A very simple example of a script include function that would do this is shown below:
getNewestConfigurationItems: function() {
var ci = new GlideRecord('cmdb_ci');
ci.orderByDesc('sys_created_on');
ci.setLimit(10);
ci.query();
var result = [];
while (ci.next()) {
result.push(ci);
}
return result;
},
Please mark as correct if this answered your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-14-2024 07:02 PM
Hi @Akshay03, you just need to amend your script include to order the returned results by the Created On [sys_created_on] field so the CI that was created last is the first record in the array result. You then populate your variable using the first item in your array:
A very simple example of a script include function that would do this is shown below:
getNewestConfigurationItems: function() {
var ci = new GlideRecord('cmdb_ci');
ci.orderByDesc('sys_created_on');
ci.setLimit(10);
ci.query();
var result = [];
while (ci.next()) {
result.push(ci);
}
return result;
},
Please mark as correct if this answered your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-14-2024 08:31 PM
Hi @Akshay03
Let's use the API below to order the query set in descending order of a specific field.
orderByDesc(String, fieldName)
Cheers,
Tai Vu