Populate Variables from Existing RITM in a Catalog Item

tanuja_g
Tera Contributor

Hi All,

I have a catalog item (e.g., ABC) with the following requirement:

  • There is a variable called Request Type with two options:
    1. New: Displays a set of variables for submission.
    2. Old: Displays a reference variable where users can select an existing RITM.(displaying all RITMs submitted for this catalog item)

If the user selects Old and chooses an existing RITM, the variables of the selected ABC RITM should be auto-populated into the current catalog item's variables.

Note: The current variables are part of variable sets, and there are also Multi-Row Variable Sets (MRVS) in the catalog item.

How can I achieve this functionality? Any help or guidance would be appreciated.

Thanks in advance!

3 REPLIES 3

Community Alums
Not applicable

You can try writing a script include that would look up the previous RITM and return the field values that you need to populate the current catalog item request. You can return the set of values as an array and then populate the form.

tanuja_g
Tera Contributor

Could you please guide me on how to structure the script and any best practices for achieving this functionality? I would appreciate your expertise and insights on this.

Looking forward to your support.

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || !newValue) {
        return;
    }
    var ga = new GlideAjax('Fetchritm');
    ga.addParam('sysparm_name', 'fetchRITMVariables');
    ga.addParam('sysparm_ritm', newValue); //RITM sys_id
    ga.getXMLAnswer(function(response) {
        var result = JSON.parse(response);
        if (result.success) {
            for (var variableName in result.variables) {
                g_form.setValue(variableName, result.variables[variableName]);
            }
        } else {
        
        }
    });
}
var Fetchritm = Class.create();
SimpleRITMFetcher.prototype = {
    initialize: function() {},

    fetchRITMVariables: function() {
        var response = { success: false, variables: {} };

        try {
            var ritmSysId = this.getParameter('sysparm_ritm');
            var ritm = new GlideRecord('sc_req_item');
            if (ritm.get(ritmSysId)) {
                var vars = ritm.variables.getElements();
                vars.forEach(function(variable) {
                    response.variables[variable.getName()] = variable.getDisplayValue();
                });
                response.success = true;
            } else {
                response.message = 'not found.';
            }
        } catch (err) {
            response.message = 'Error: ' 
        }

        return new JSON().encode(response);
    },

    type: 'Fetchritm'
};

 

 Edit it Accordingly 

 

If my answer helped you in any way, then mark it as helpful or correct. This will help others finding a solution.

Kindly mark it correct and helpful if it is applicable.

Thanks,

Raj