Updating table fields using workflow run script

keesh
Mega Contributor

Hello,

I'm working on a catalog item request and needed help with the following requirement:

  • Update Stockroom field to 'Tower Loaner' based on current value of Computer variable.
  • The logic is that if the computer is in stock, then it gets returned to the stockroom named 'Tower Loaner'.
  • Note: The Computer variable on my request is a reference field associated to the [cmdb_ci_computer] table.

All the other fields in my workflow run script are updating as required with the exception of the Stockroom field (gr.asset.stockroom). The Stockroom field is associated to the [alm_asset] table, so I tried dot-walking but had no luck on making this work. Any feedback would be much appreciated.

runScript();

function runScript() {
var gr = new GlideRecord('cmdb_ci_computer');
  if (gr.get(current.variables.computer)) {
    gr.hardware_status = 'in_stock';
    gr.hardware_substatus = 'available';
    gr.assigned_to = '';
    gr.asset.stockroom = '43bcbca81340df00cd9f74c66144b0f2'; //Tower Loaner Stockroom
    gr.update();
  }
}

CI Record:

find_real_file.png

Stockroom Record:

find_real_file.png

1 ACCEPTED SOLUTION

SanjivMeher
Kilo Patron
Kilo Patron

You can't update a field in a reference record from source record. Use below code.

 



runScript();

function runScript() {
var gr = new GlideRecord('cmdb_ci_computer');
  if (gr.get(current.variables.computer)) {
    gr.hardware_status = 'in_stock'; 
    gr.hardware_substatus = 'available';
    gr.assigned_to = '';
    //gr.asset.stockroom = '43bcbca81340df00cd9f74c66144b0f2'; //Tower Loaner Stockroom
    gr.update();
  }

var gr1 = new GlideRecord('alm_asset');
gr1.addQuery('ci',current.variables.computer);
gr1.query();

if (gr1.next()){
gr1.stockroom = '43bcbca81340df00cd9f74c66144b0f2'; //Tower Loaner Stockroom
gr1.update();
}
}

Please mark this response as correct or helpful if it assisted you with your question.

View solution in original post

3 REPLIES 3

SanjivMeher
Kilo Patron
Kilo Patron

You can't update a field in a reference record from source record. Use below code.

 



runScript();

function runScript() {
var gr = new GlideRecord('cmdb_ci_computer');
  if (gr.get(current.variables.computer)) {
    gr.hardware_status = 'in_stock'; 
    gr.hardware_substatus = 'available';
    gr.assigned_to = '';
    //gr.asset.stockroom = '43bcbca81340df00cd9f74c66144b0f2'; //Tower Loaner Stockroom
    gr.update();
  }

var gr1 = new GlideRecord('alm_asset');
gr1.addQuery('ci',current.variables.computer);
gr1.query();

if (gr1.next()){
gr1.stockroom = '43bcbca81340df00cd9f74c66144b0f2'; //Tower Loaner Stockroom
gr1.update();
}
}

Please mark this response as correct or helpful if it assisted you with your question.

The script worked perfectly. Thank you so much sanjivmeher. I appreciate your help and the prompt response.

rlatorre
Kilo Sage

You'll need to lookup the asset records separately to update it.