Pass INC. fields to new REQ/RITM.

MayrelCerero
Tera Expert

Hello! I created a UI Action on the INC. called 'Create New Hardware Request', which brings users to the New Hardware Request Order Guide. Within that order guide, we have three catalog items that share the same fields that I want to pass from the INC. I also created an onLoad catalog client script on the Order Guide. I want to carry over the INC. number to the new requests 'business justification' field, the INC. short description to the request summary field, and the 'on behalf of' to the new request's 'who is this request for' field. I'm thinking the issue is that two of the three fields on the new request are part of a variable set. 

 

Below is the script I created for the UI Action and Catalog Client Script: 

UI Action: 

current.update();

function getMyOrderGuideURL(record) {
    var guideId = '7b93aed61bdc19101fc82020f54bcb00'; // order guide sys_id

    var gu = new GlideURL('com.glideapp.servicecatalog_cat_item_guide_view.do');
    gu.set('v', '1');
    gu.set('sysparm_initial', 'true');
    gu.set('sysparm_guide', guideId);

    // Tracks parent inc.
    gu.set('sysparm_parent_table', record.getTableName());
    gu.set('sysparm_parent_sys_id', record.getUniqueValue());

    // Pass inc. fields as variables
    gu.set('variables.summary_description', record.short_description);
    gu.set('variables.business_justification', record.number);

    if (record.u_on_behalf_of) {
        gu.set('variables.who_is_this_request_for', record.u_on_behalf_of);
    }

    return gu.toString();
}

var url = getMyOrderGuideURL(current);
action.setRedirectURL(url);

Catalog Client Script:

function onLoad() {
    // Delay to ensure orderGuide is available
    setTimeout(function() {
        var summary = g_form.getValue('summary_description') || '';
        var justification = g_form.getValue('business_justification') || '';
        var onBehalfOf = g_form.getValue('who_is_this_request_for') || '';

        // if (typeof orderGuide === 'undefined' || typeof orderGuide.getItems !== 'function') {
        //     g_form.addInfoMessage('Order Guide not loaded. Skipping item pre-fill.');
        //     return;
        // }

        if (!window.orderGuide || typeof orderGuide.getItems !== 'function') {
            g_form.addInfoMessage('Order Guide object not found. Skipping item pre-fill.');
            return;
        }


        var items = orderGuide.getItems();

        for (var i = 0; i < items.length; i++) {
            var item = items[i];

            if (item && typeof item.setVariable === 'function') {
                if (item.getVariable('who_is_this_request_for') !== null)
                    item.setVariable('who_is_this_request_for', onBehalfOf);

                if (item.getVariable('summary_description') !== null)
                    item.setVariable('summary_description', summary);

                if (item.getVariable('business_justification') !== null)
                    item.setVariable('business_justification', justification);
            }
        }

        g_form.addInfoMessage('Fields copied to catalog items successfully.');
    }, 300); // 300ms delay
}

I would appreciate any suggestions! Thank you! 🙂 

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@MayrelCerero 

you can use Cascade variable feature.

are you using correct variable name etc?

did you add alert and see?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi Ankur, thank you for your response. Yes, I am using the correct variable names. But after may attempts and debugging, I THINK the issue here is when launching the Order Guide, which works for the guide view itself — but once I click "Choose Options" for a specific Catalog Item (e.g., "Computer Accessories Request"), the URL changes, and those extra parameters (u_on_behalf_of, short_description) are stripped out. So, that means by the time the Catalog Client Script runs inside the catalog item form, those values are no longer available via URLSearchParams.I'm thinking that my next step will be to use a record producer to get this to work.