Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Issue in populating the Requested for field in REQ

deepika adimu
Tera Contributor

Hi all,

 

I have one use case, i.e., "Requested For" is the same as "Opened By" when multiple RITMs are created under a single request via add to cart.

 

I don't know how to validate the RITM which is submitted via Add to Cart functionality and anyone help me to find the solution 

 

Existing Script:

 

(function executeRule(current, previous /*null when async*/) {
 var grRITM = new GlideRecord('sc_req_item'); 
grRITM.addQuery('request', 'current.sys_id'); 
grRITM.addNotNullQuery('order_guide'); 
grRITM.query(); 
var count = grRITM.getRowCount(); 
if (count > 1) {
 while (grRITM.next()) {
 if (!JSUtil.nil(grRITM.variables.requested_for)) { 
current.requested_for = grRITM.variables.requested_for; 
} else if (!JSUtil.nil(grRITM.variables.u_nf_requested_for)) { 
current.requested_for = grRITM.variables.u_nf_requested_for; 
}
 }
 }
 })(current, previous);

 

2 REPLIES 2

Rajesh Chopade1
Mega Sage

Hi @deepika adimu 

 

Try bellow updated code once:

(function executeRule(current, previous /*null when async*/) {
    var grRequest = new GlideRecord('sc_request');
    if (grRequest.get(current.request)) {  // 'current.request' is the sys_id of the Request
        var openedBy = grRequest.opened_by;  // Get the 'Opened By' user on the Request
        
        // Now, check if RITMs under the same Request are valid
        var grRITM = new GlideRecord('sc_req_item'); 
        grRITM.addQuery('request', current.request); // Query RITMs associated with the current request
        grRITM.query();
        
        while (grRITM.next()) {
            // Check if 'Requested For' matches the 'Opened By' user
            var requestedFor = grRITM.variables.requested_for; // 'Requested For' on the RITM
            
            // Check if 'Requested For' is either null or doesn't match 'Opened By'
            if (!JSUtil.nil(requestedFor) && requestedFor != openedBy) {
                // You can add a log, or set an error on the record
                gs.error('Requested For does not match Opened By for RITM ' + grRITM.sys_id);
                
                // Optionally, set the 'Requested For' field on the RITM to be the same as 'Opened By'
                grRITM.variables.requested_for = openedBy;
                grRITM.update(); // Update the RITM record to ensure consistency
            }
        }
    }
})(current, previous);

 

I hope my answer helps you to resolve your issue, if yes please mark my answer helpful and correct.

thank you

Rajesh

deepika adimu
Tera Contributor

Hi @deepika adimu,

 

 

Try it :

/**
    "Requested For" is the same as "Opened By" when multiple RITMs are created under a single request via cart functionality
     */

    var grRITM = new GlideRecord('sc_req_item');
    grRITM.addQuery('request', current.sys_id);
    grRITM.addNotNullQuery('order_guide');
    grRITM.query();
    var count = grRITM.getRowCount();
    gs.print(count);
    if (count >= 1) {
        while (grRITM.next()) {
            if (!JSUtil.nil(grRITM.variables.requested_for)) {
                current.requested_for = grRITM.variables.requested_for;
            } else if (!JSUtil.nil(grRITM.variables.u_nf_requested_for)) {
                current.requested_for = grRITM.variables.u_nf_requested_for;
            }
        }
    } else {
        current.requested_for = current.opened_by;
    }