Run script to change assigned_to

jean-pauldehaas
Tera Guru

Hi,

 

i have a catalog item to reassign assets which has the following variables:

current_user_this_asset_is_assigned_to ,  which_asset_needs_to_be_reassigned , new_user_this_asset_need_to_be_assigned_to .

 

i need to create a run script in the workflow which reassigns the asset in (which_asset_needs_to_be_reassigned) to the user that is selected in (new_user_this_asset_need_to_be_assigned_to) on the cmdb_ci_computer table.

i also need to make sure the asset is reassigned on the date that is selected in the variable  when_does_this_change_need_to_be_performed (date picker)

 

i tried afew things but couldnt make it work, this is my script i started with:

 

var gr = new GlideRecord('cmdb_ci_computer');
gr.addQuery('sysid',current.assigned_to);//add the variable name
gr.query();
if(gr.next()){
gr.assigned_to = current.variables.new_user_this_asset_need_to_be_assigned_to;
gr.update();
}
1 ACCEPTED SOLUTION

waqask
Kilo Expert

 

It looks like you're on the right track with your script, but there are a few adjustments needed to correctly reassign the asset and ensure it’s updated with the correct date. Here’s a revised version of your script:

 

javascript
Copy code
// Create a GlideRecord object for the 'cmdb_ci_computer' table var gr = new GlideRecord('cmdb_ci_computer'); // Query the table for the asset that needs to be reassigned gr.addQuery('sys_id', current.variables.which_asset_needs_to_be_reassigned); gr.query(); // Check if a record was found if (gr.next()) { // Reassign the asset to the new user gr.assigned_to = current.variables.new_user_this_asset_need_to_be_assigned_to; // Set the reassignment date if it's provided if (current.variables.when_does_this_change_need_to_be_performed) { gr.assignment_date = new GlideDateTime(current.variables.when_does_this_change_need_to_be_performed); } // Update the record gr.update(); }
 

Key Points:

  1. Query the Asset Correctly: Ensure you use the correct field name and variable for querying the asset. In the addQuery method, sys_id should match the asset you want to reassign.

  2. Update the Correct Fields: The field you’re updating is assigned_to, which should be the reference to the new user. Ensure that assigned_to is the correct field in your cmdb_ci_computer table.

  3. Handle the Date Field: Ensure that the field where the date needs to be set (assignment_date in this case) is correct and exists in your table. Also, the GlideDateTime object is used to properly format the date.

  4. Variable Names: Make sure the variable names in current.variables match exactly with the names used in your catalog item.

You may need to adapt the script slightly depending on the exact field names and requirements of your instance.

View solution in original post

9 REPLIES 9

Brad Bowman
Kilo Patron
Kilo Patron

This would be closer - assuming the asset and ...assigned_to variables are a reference to the correct tables

var gr = new GlideRecord('cmdb_ci_computer');
gr.addQuery('sysid',current.variables.which_asset_needs_to_be_reassigned);
gr.query();
if (gr.next()) {
    gr.assigned_to = current.variables.new_user_this_asset_need_to_be_assigned_to;
    gr.update();
}

 If you don't want this to happen until the selected date you'll need a timer activity preceding the Run Script.

hi Brad,

 

new_user_this_asset_need_to_be_assigned_to references to sys_user table

current_user_this_asset_is_assigned_to references to sys_user table

which_asset_needs_to_be_reassigned references to cmdb_ci_computer table

 

i used your script but its not updating so i think some tables are inccorrect or something

 

 

 

Looks like it should work, as long as all of the really long variable names are exactly correct.  Are you creating a new request every time you change the workflow, and submitting the request logged in or impersonating an admin user?

yeah thats something i still need to changes those names.

but yes im creating a new request every time as an admin.

the WF does not throw an error and completes but nothing is changed on the assigned to field