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

The next step is to add some logging, something like this:

workflow.info('Run Script starting');
var gr = new GlideRecord('cmdb_ci_computer');
gr.addQuery('sysid',current.variables.which_asset_needs_to_be_reassigned);
gr.query();
if (gr.next()) {
    workflow.info('Record found ' + gr.name + ' Currently assigned to ' + gr.assigned_to + ' To be assigned to ' + current.variables.new_user_this_asset_need_to_be_assigned_to);
    gr.assigned_to = current.variables.new_user_this_asset_need_to_be_assigned_to;
    workflow.info('New Assigned to = ' + gr.assigned_to;
    gr.update();
} else {
    workflow.info('Computer record not found');

You can view these on the Log tab when viewing the Context from the Related Link on the RITM.

 

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.

@waqask this works but i used a seperate timer for the date check

I'm struggling to see how this AI copy and paste provides any value or insight over the troubleshooting process I was walking you through in my three responses.  It's not even any different than the original script I provided earlier! 

@Brad Bowman  i added the info to my script yesterday and saw that it was pulling in from the wrong table i think so your response definetly helped!

im not saying i copied and pasted the script but it got it to work thats the most important