I want to autopopulate values of rejected RITM

Taaha M
Tera Contributor

In my catalog form I have a field which is named as 'Previous RITM Rejected' , in this field I am listing the RITM rejected which was opened by me.
Now ,when I select a RITM in this field, the other fields on current Request form should autopopulate with its values from the rejected RITM selected.

How to do this? Should I write catalog script or have to do in workflow.

1 ACCEPTED SOLUTION

Runjay Patel
Giga Sage

Hi @Taaha M ,

 

You can write onchange catalog client script on Previous Item Rejected filed.

Client script

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }

    // GlideAjax to fetch data from the selected RITM
    var ga = new GlideAjax('RITMDataFetcher'); // Script Include to fetch data
    ga.addParam('sysparm_name', 'getRITMDetails');
    ga.addParam('sysparm_ritm', newValue); // sys_id of the selected RITM
    ga.getXML(setData);

function setData(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
    answer = answer.evalJSON(); //Transform the JSON string to an object
   

        // Populate fields on the catalog form
        g_form.setValue('field_name_1', answer .field1); // Replace field_name_1 with actual field names
        g_form.setValue('field_name_2', answer .field2); // Populate additional fields as needed
        // Add more g_form.setValue() calls for other fields
    });
}

 

Server Side

var RITMDataFetcher= Class.create();
RITMDataFetcher.prototype = Object.extendsObject(AbstractAjaxProcessor, {
 
        getRITMDetails: function(ritmSysId) {
        var obj = {};
var sysId = this.getParameter('sysparm_ritm');
        var gr = new GlideRecord('sc_req_item'); // Table for RITM
        if (gr.get(ritmSysId)) {
            obj.field1= gr.getDisplayValue('field1');
            obj.field2= almAssetGr.getDisplayValue('field2');
            
            var json = new JSON();
            var data = json.encode(obj); //JSON formatted string

            
        }
      return data;
    },

    type: 'RITMDataFetcher'
});

 

Check this blog for more info regarding ajax call: https://servicenowwithrunjay.com/return-multiple-values-to-glideajax-from-script-include/

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

View solution in original post

4 REPLIES 4

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @Taaha M 

 

I understand that it's a user experience issue, but practically, it will increase the technical debt. First, you would need to filter all rejected items based on the catalog item selected. If no items have been rejected, the user would have to update all the details. Additionally, an item might have been rejected 6 months ago, and in that time, variable values could have changed, or new variables might have been added. These are process issues that will contribute to increasing the technical debt.

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

I understand, but that is my requirement.
I am not sure how to begin on this one

The steps can be

 

- New variable , refer to OldRITM where ritm. item == Catalog item and state = rejected.

- Then server side script to bring the rejected item value to current one.

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Runjay Patel
Giga Sage

Hi @Taaha M ,

 

You can write onchange catalog client script on Previous Item Rejected filed.

Client script

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }

    // GlideAjax to fetch data from the selected RITM
    var ga = new GlideAjax('RITMDataFetcher'); // Script Include to fetch data
    ga.addParam('sysparm_name', 'getRITMDetails');
    ga.addParam('sysparm_ritm', newValue); // sys_id of the selected RITM
    ga.getXML(setData);

function setData(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
    answer = answer.evalJSON(); //Transform the JSON string to an object
   

        // Populate fields on the catalog form
        g_form.setValue('field_name_1', answer .field1); // Replace field_name_1 with actual field names
        g_form.setValue('field_name_2', answer .field2); // Populate additional fields as needed
        // Add more g_form.setValue() calls for other fields
    });
}

 

Server Side

var RITMDataFetcher= Class.create();
RITMDataFetcher.prototype = Object.extendsObject(AbstractAjaxProcessor, {
 
        getRITMDetails: function(ritmSysId) {
        var obj = {};
var sysId = this.getParameter('sysparm_ritm');
        var gr = new GlideRecord('sc_req_item'); // Table for RITM
        if (gr.get(ritmSysId)) {
            obj.field1= gr.getDisplayValue('field1');
            obj.field2= almAssetGr.getDisplayValue('field2');
            
            var json = new JSON();
            var data = json.encode(obj); //JSON formatted string

            
        }
      return data;
    },

    type: 'RITMDataFetcher'
});

 

Check this blog for more info regarding ajax call: https://servicenowwithrunjay.com/return-multiple-values-to-glideajax-from-script-include/

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------