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
Kilo Patron
Kilo Patron

Hi @chercm adjusted the code below

if (current.assignment_group !='') {
// Query the Stockroom based on Assignment Group
var stockroomGR = new GlideRecord('alm_stockroom');
stockroomGR.addQuery('assignment_group', current.assignment_group);// your checking grp name here not sys_id so change it to grp
stockroomGR.query();

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

Regards
Harish

@Harish KM @Robbie  i tried to change to assignment_group but still does not work 

 

here is the updated script

 

(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.getValue('name');
}
}
})(current, previous);

 

chercm_0-1706876492649.png

 

Hi @chercm is this BR after update? if yes please add current.update()

// Query the Stockroom based on Assignment Group
var stockroomGR = new GlideRecord('alm_stockroom');
stockroomGR.addQuery('assignment_group', current.assignment_group);// your checking grp name here not sys_id so change it to grp
stockroomGR.query();

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

current.update(); // need update
}
}

Regards
Harish

@Harish KM does not work with the update code 

 

(function executeRule(current, previous /*null when async*/) {
 
 
// 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.getValue('name');
current.update(); // need update
}
 
})(current, previous);