- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-28-2024 08:54 AM
Hi All,
We have a script to update the hardware asset state and substate based on specific fields on the catalog tasks.
The scenario is that when a user is getting offboarded, the associated assets will also be updated in the hardware table from "In use" to "In stock" or whatever is selected in the catalog task fields. If a user have 4 assets associated ( 2 computers and 2 communication device) then total 4 tasks are getting triggered. Accordingly the number of assets of the offboarded user is triggering that many number of tasks which is working. For each tasks, assigned to will pick one asset serial number and select the state to In stock or something else and substate to Available which will update the state and substate in the hardware table when the respective tasks will be closed.
I am trying to update the hardware table based on the field selections for all the respective tasks with respective assets selected on it . It is working for one task when I am testing this in the background script.
I would like to know how the code will loop all the tasks and update the state in hardware table based on the details selected in each generated tasks.
Please check the below code and help me how to achieve this for multiple tasks.
The below code I was trying to update 2 tasks with 2 computer assets of the user with same short description. Accordingly will do for the communication device tasks. The below code is only updating one task and not the other.
var getsc = new GlideAggregate('sc_task');
getsc.addQuery('request_item', '709a525583c74a105e6498747daad3c7');
getsc.addQuery('short_description' , "Offboarding - Computer");
getsc.addAggregate('COUNT');
getsc.query();
if (getsc.next()) {
gs.print('Tasks: ' + getsc.getAggregate('COUNT'));
var gl= new GlideRecord('sc_task')
gl.addQuery('request_item', '709a525583c74a105e6498747daad3c7');
gl.addQuery('short_description' , "Offboarding - Local IT - Computer");
gl.query();
if (gl.next()){
for(var i=0; i<getsc.getAggregate('COUNT'); i++ ){
if (gl.u_asset_details != ""){
var asset = gl.u_asset_details;
var gr = new GlideRecord('alm_hardware');
gr.addQuery('sys_id', asset);
gr.query();
if (gr.next()) {
// for(var i=0; i<getsc.getAggregate('COUNT'); i++ ){
// gr.assigned_to = '';
gr.install_status = gl.u_asset_status;
gr.substatus = gl.u_asset_substate;
gr.update();
}
}
}
}
}
Please provide a solution to this.
Thanks in Advance.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2024 07:39 AM
Hi Koyel,
One issue is definitely on these lines:
if (gl.next()){
for(var i=0; i<getsc.getAggregate('COUNT'); i++ ){
Simply replace if with while and remove the following for loop statement (together with the closing brace at the bottom).
Let me know if it helps!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2024 07:54 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2024 07:39 AM
Hi Koyel,
One issue is definitely on these lines:
if (gl.next()){
for(var i=0; i<getsc.getAggregate('COUNT'); i++ ){
Simply replace if with while and remove the following for loop statement (together with the closing brace at the bottom).
Let me know if it helps!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2024 07:54 AM
Try using while instead of if loop
Thanks
Sanjana
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2024 09:20 AM
Hi @Koyel Guha
i provided you the revised version of your script that should handle multiple tasks and update the hardware table accordingly:
var requestId = '709a525583c74a105e6498747daad3c7'; // Replace with the actual request item ID
var taskShortDescription = "Offboarding - Local IT - Computer"; // Replace with the actual task description
var getsc = new GlideAggregate('sc_task');
getsc.addQuery('request_item', requestId);
getsc.addQuery('short_description', taskShortDescription);
getsc.addAggregate('COUNT');
getsc.query();
if (getsc.next()) {
gs.print('Tasks: ' + getsc.getAggregate('COUNT'));
var scTask = new GlideRecord('sc_task');
scTask.addQuery('request_item', requestId);
scTask.addQuery('short_description', taskShortDescription);
scTask.query();
while (scTask.next()) {
if (scTask.u_asset_details) {
var asset = scTask.u_asset_details;
var hardware = new GlideRecord('alm_hardware');
if (hardware.get(asset)) {
hardware.install_status = scTask.u_asset_status;
hardware.substatus = scTask.u_asset_substate;
hardware.update();
}
}
}
}
1. GlideAggregate: This is used to count the number of tasks you have.
2. GlideRecord (sc_task): This is used to fetch and loop through each sc_task that matches your criteria.
3. While loop: This ensures you are looping through all the tasks.
4. Asset Update: For each task, it checks if there is an associated asset (u_asset_details) and updates the corresponding record in the alm_hardware table.