Populate RITM variable value on the basis of RITM IN alm_consumable table

AbdulrehmanT
Kilo Guru
There is a field in alm_consumable table named 'request line' which is a reference field from RITM table.
Now the demand is if someone choose the RITM in request line field that the Ritm field value Location and Assigned too should be auto populated                   AND      RITM variable value named 'Cost Center' which is reference variable..should also be populate of that RITM in Consumable field record.

Below is the script , its works for location but not for cost center 

var
GetRITMDetails = Class.create();
GetRITMDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getDetails: function() {
        var ritmId = this.getParameter('sysparm_ritm_id');
        var result = {
            location: '',
            cost_center: ''
        };

        if (!ritmId)
            return JSON.stringify(result);

        var ritmGR = new GlideRecord('sc_req_item');
        if (ritmGR.get(ritmId)) {
            // Get location directly
            result.location = ritmGR.location + '';

            // Get cost_center variable (reference)
            var mtom = new GlideRecord('sc_item_option_mtom');
            mtom.addQuery('request_item', ritmId);
            mtom.query();
            while (mtom.next()) {
                var variableName = mtom.sc_item_option.name + '';
                var variableValue = mtom.sc_item_option.value + '';

                if (variableName == 'cost_center') {
                    result.cost_center = variableValue; // This is the sys_id
                    break;
                }
            }
        }

        return JSON.stringify(result);
    }
});
1 ACCEPTED SOLUTION

i am using this now and its working but now i want to make the requested for of ritm equals to assigned to of consumable and its fetching the sys id but its not seeting the field value. can you help

var GetRITMDetails = Class.create();
GetRITMDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getDetails: function() {
        var ritmId = this.getParameter('sysparm_ritm_id');
        var result = {
            location: '',
            cost_center: '',
            requested_for: '',
   
        };

        if (!ritmId)
            return JSON.stringify(result);

        var ritm = new GlideRecord('sc_req_item');
        if (ritm.get(ritmId)) {
            result.location = ritm.location + '';
            result.requested_for = ritm.requested_for.getDisplayValue() + '';
            result.cost_center = ritm.variables.cost_center + '';
             
        }

        return JSON.stringify(result);
    }
});

View solution in original post

2 REPLIES 2

Rafael Batistot
Kilo Patron

Hi @AbdulrehmanT

 

  • Dereferencing the variable correctly:

    • You're accessing mtom.sc_item_option.value, but when the variable is a reference, you need to get the actual sys_id of the referenced record, not just the display value.

    • The sc_item_option is a reference field on sc_item_option_mtom. You must use .getRefRecord() to properly access the variable's value.

  • Ensure the variable name comparison is case-sensitive:

    • Double-check if the variable name in your condition ('cost_center') matches the exact name (not label) defined in the variable configuration.


 Modify your loop to use:

var itemOption = mtom.sc_item_option.getRefRecord();
if (!itemOption)
     continue;



 

If you found this response helpful, please mark it as Helpful. If it fully answered your question, consider marking it as Correct. Doing so helps other users find accurate and useful information more easily.

i am using this now and its working but now i want to make the requested for of ritm equals to assigned to of consumable and its fetching the sys id but its not seeting the field value. can you help

var GetRITMDetails = Class.create();
GetRITMDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getDetails: function() {
        var ritmId = this.getParameter('sysparm_ritm_id');
        var result = {
            location: '',
            cost_center: '',
            requested_for: '',
   
        };

        if (!ritmId)
            return JSON.stringify(result);

        var ritm = new GlideRecord('sc_req_item');
        if (ritm.get(ritmId)) {
            result.location = ritm.location + '';
            result.requested_for = ritm.requested_for.getDisplayValue() + '';
            result.cost_center = ritm.variables.cost_center + '';
             
        }

        return JSON.stringify(result);
    }
});