Copy functionality for RITM on portal

BabaS6658192191
Tera Contributor

I have a requirement on portal, where we need to copy the RITM for a specific catalog item's RITM. And when they click on the RITM it should open the new catalog item to auto populate the variable values from the original RITM.

 

I have updated the widget to show the copy button and then it will redirect to empty catalog item, but not able to find a solution to auto populate the variable values from the original RITM to the new catalog form opened. 

 

I want this to open in a new catalog form, so the users can edit and submit the accordingly to create request from portal.

 

 

1 ACCEPTED SOLUTION

Anvin
Tera Expert

Hello,

 

If you have to redirect to the item page, you have to construct an URL,

 

1. encode the RITM sys_id in the URL and in the catalog item

2. have an onLoad client script to get the parameter from URL.

3. the parameter will hold the old RITM sys_id, query the RITM and get the values using GlideAjax.

4. Construct a JSON from server side and pass it to client and set the values in the fields.

View solution in original post

6 REPLIES 6

First you have to construct the URL with the RITM sys_id encoded. 

 

<Your URL>+'old_ritm=' + current.sys_id

Here, you can define any parameter, I have given it as 'old_ritm' and if you are using any variables query the RITM, instead of current you can mention that. Now you have the URL and when user click on the URL, they will redirect to the portal page where they see the item with empty variables.

 

Now a client script needs to be created on the item. it should be onLoad of the item and client script should look for the parameter in the URL,

For native view, you can use the following,

var url = new GlideURL(window.location.href);
var myParam = url.getParam('old_ritm');

 

For portal, 

 

function getParameterByName(name) {
var url = window.location.href;
name = name.replace(/[\[\]]/g, '\\$&'); // Escape any special characters
var regex = new RegExp('[?&]' + old_ritm + '(=([^&#]*)|&|#|$)');
var results = regex.exec(url);
if (!results) return null; // If parameter not found
if (!results[2]) return ''; // If parameter is empty
return decodeURIComponent(results[2].replace(/\+/g, ' ')); // Decode and return the parameter value
}

 

If the scripts return values then it means the URL has the parameter. Since you given the sys_id in the URL parameter, you have the old RITM sys_id with you. You can use the usual glideajax script to get the values from the old RITM.

I have written this onLoad client script - 

function onLoad() {
    function getParameterByName(name) {
        var url = window.location.href;
        name = name.replace(/[\[\]]/g, '\\$&');
        var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)');
        var results = regex.exec(url);
        if (!results) return null;
        if (!results[2]) return '';
        return decodeURIComponent(results[2].replace(/\+/g, ' '));
    }

    var ritmId = getParameterByName('copy_from');
    if (ritmId) {
        var ga = new GlideAjax('CatalogPrefillUtil');
        ga.addParam('sysparm_name', 'getPrefillData');
        ga.addParam('sysparm_ritm_id', ritmId);
        ga.getXMLAnswer(function(response) {
            try {
                console.log('📦 Raw response from GlideAjax:', response);
                var values = JSON.parse(response);

                // Set variables here - use exact *Name* of variables (not label!)
                if (values.monthly_data_allowance)
                    g_form.setValue('monthly_data_allowance', values.monthly_data_allowance);

                if (values.storage)
                    g_form.setValue('storage', values.storage);

                // Add more variable mappings here if needed
            } catch (e) {
                console.error(' Error parsing or applying variable values:', e);
            }
        });
    }
} and below script include - 
var CatalogPrefillUtil = Class.create();
CatalogPrefillUtil.prototype = {
    initialize: function() {},

    getPrefillData: function() {
        var ritmId = gs.getParameter('sysparm_ritm_id');
        var result = {};

        var ritm = new GlideRecord('sc_req_item');
        if (ritm.get(ritmId)) {
            var vars = ritm.variables;

            result.monthly_data_allowance = vars.monthly_data_allowance + '';
            result.storage = vars.storage + '';
        }

        return JSON.stringify(result);
    },

    type: 'CatalogPrefillUtil'
}; But the values are not being copied and getting java console error.
Can you help?