how to fetch catalog item variable values from 'sc_cart_item' table

Lovely Butola
Tera Contributor

I have a requirement where HR team imports an excel sheet with a 'Department' column. They do this through a Catalog Item. On the catalog item they select a 'department' variable. Now the requirement is that excel sheet Column and the Variable selected should be SAME. If not, the RITM should not get created.

I tried to achieve this using Before insert Business Rule on sc_cart_item. But I'm unable to fetch variable value from this table.

var deptList = current.variables.department.getDisplayValue(); // not working

 

this is not helping.

Would be great if I could get some suggestions.

7 REPLIES 7

Lovely Butola
Tera Contributor

I could achieve the validation part using 'sc_req_item' Table. And upon failure of the validations I am aborting the creation of RITM. Now the issue is how can I delete REQUEST (REQ) that's getting created. Don't want any record getting created on failure.

@Lovely Butola 

how did you achieve this? please share the logic

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar 

 

Before Insert on sc_req_item table:

 

var deptList = current.variables.select_business.getDisplayValue(); // variable value
var arrDept = deptList.split(', ');

var attach = new GlideRecord('sys_attachment');
attach.orderByDesc('sys_created_on');
attach.setLimit(1);
attach.addQuery('table_name', 'sc_cart_item');
attach.addQuery('sys_created_by', gs.getUserName());
attach.addEncodedQuery('sys_created_onRELATIVEGT@minute@ago@1');
attach.addQuery('file_name', 'CONTAINS', '.xls');
attach.query();
if (attach.next()) {
var attachment = new GlideSysAttachment();var attachmentStream = attachment.getContentStream(attach.sys_id.toString());

var parser = new sn_impex.GlideExcelParser();
parser.parse(attachmentStream);

var headers = parser.getColumnHeaders();

var rowNumber = 0;

if (headers.indexOf('Department_L1') >= 0) {

var flag = false;

while (parser.next() && flag == false) {

     var currentCellValue = row['Department_L1'];
                if (JSUtil.nil(currentCellValue)) {
                    gs.flushMessages();
                    gs.addErrorMessage("The Department Cell is blank. Kindly review the template.");
                    flag = true;
                   current.setAbortAction(true);
 
                    break;
                } 
else if (arrDept.indexOf(currentCellValue) < 0) {
                    gs.flushMessages();
                    gs.addErrorMessage("The value selected should match the value in the Template. Kindly review the template.");
 
                   current.setAbortAction(true);
                    flag = true;
                    break;
                }
                //    }
            }
 
I have written many validations but this is the main one. Also at last have an ELSE condition. But this is the logic
 
Anyways i figured out how to delete the REQ record.
Thanks!