tried to pull the stockroom name in a catalog form not working

chercm
Mega Sage

how to update the stockroom name based on the assignment group ? 

 

chercm_0-1706863191273.png

 

 

 

i tried to use this but stockroom field of the form is still blank 

 

i created the business rule on it . 

 

(function executeRule(current, previous /*null when async*/) {
// Check if Assignment Group is set
if (current.assignment_group.getDisplayValue() != '') {
// Query the Stockroom based on Assignment Group
var stockroomGR = new GlideRecord('alm_stockroom');
stockroomGR.addQuery('sys_id', current.assignment_group);
stockroomGR.query();
 
// If a stockroom is found, set the Stockroom field on the catalog form
if (stockroomGR.next()) {
current.stockroom = stockroomGR.getValue('name');
}
}
})(current, previous);
 
chercm_1-1706863374336.png

 

2 ACCEPTED SOLUTIONS

Hi @chercm,

 

Got it. I've spotted a few minor issues based on what you're trying to achieve.

I've tested this on a PDI and can confirm it works as required.

Update the following:

 

Business Rule. When to run: 'before'

 

Business Rule Script: (Ready for you to copy and paste)

(function executeRule(current, previous /*null when async*/ ) {
// Check if Assignment Group is set
if (current.assignment_group.getDisplayValue() != '') {
// Query the Stockroom based on Assignment Group

var stockroomGR = new GlideRecord('alm_stockroom');
stockroomGR.addQuery('assignment_group', current.assignment_group);
stockroomGR.query();

// If a stockroom is found, set the Stockroom field on the catalog form
if (stockroomGR.next()) {
current.u_stockroom = stockroomGR.sys_id;

}
}
})(current, previous);
 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Helpful.

 

Thanks, Robbie

View solution in original post

Hi @chercm ,

 

The reason you need to pass the sys_id when setting the stockroom is because the field is of type reference, meaning that it is actually pointing to another table. In order to set this value, you need to use the sys_id. 

You will however see the name displayed on the form. 

 

Have you seen my latest post? I've checked the script on a PDI and it works as required. Please can you copy and paste my script.

Please also ensure you have the Business Rule to operate 'on before' and check the 'Update' checkbox.

Out of interest, when do you set the assignment group? Do you have an Assignment Rule in operation? Have you implemented the script and changed the Assignment group value manually on the form to ensure the script is at least running?

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Helpful.

 

Thanks, Robbie

View solution in original post

20 REPLIES 20

@Harish KM  here is the catalog task table

 

chercm_0-1706879634082.png

chercm_1-1706879693578.png

 

 

Hi @chercm got it the below code should work, Your BR should be before insert checked,

 

if (current.assignment_group != '') {
// Query the Stockroom based on Assignment Group
var stockroomGR = new GlideRecord('alm_stockroom');
stockroomGR.addQuery('assignment_group', current.assignment_group);
stockroomGR.query();

// If a stockroom is found, set the Stockroom field on the catalog form
if (stockroomGR.next()) {
current.u_stockroom = stockroomGR.sys_id;
}
}

Regards
Harish

@Harish KM  does not work with the code provided. i have checked the box for before insert. 

Hi @chercm could you please run this code in background script and share the logs?

 

var stockroomGR = new GlideRecord('alm_stockroom');
stockroomGR.addQuery('assignment_group', 'group sysid'); // pass group sysid here
stockroomGR.query();

// If a stockroom is found, set the Stockroom field on the catalog form
if (stockroomGR.next()) {

gs.info("stock room is-->"+stockroomGR.sys_id);

gs.info("stock room display value-->"+stockroomGR.getDisplayValue());
}
}

Regards
Harish

Hi @chercm,

 

Got it. I've spotted a few minor issues based on what you're trying to achieve.

I've tested this on a PDI and can confirm it works as required.

Update the following:

 

Business Rule. When to run: 'before'

 

Business Rule Script: (Ready for you to copy and paste)

(function executeRule(current, previous /*null when async*/ ) {
// Check if Assignment Group is set
if (current.assignment_group.getDisplayValue() != '') {
// Query the Stockroom based on Assignment Group

var stockroomGR = new GlideRecord('alm_stockroom');
stockroomGR.addQuery('assignment_group', current.assignment_group);
stockroomGR.query();

// If a stockroom is found, set the Stockroom field on the catalog form
if (stockroomGR.next()) {
current.u_stockroom = stockroomGR.sys_id;

}
}
})(current, previous);
 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Helpful.

 

Thanks, Robbie