Autopopulate variable fields when button is clicked on SOW

Rairai31
Giga Guru

Hi,

I have a requirement to autopopulate variable fields when 'Create request' button is clicked on SOW. Once button is clicked, a catalog item will open on a new tab. The expectation is based from incident details, it will populate the variable item on the catalog (e.g. Caller - Requested for, Short Description - Short description of your request....)

 

Here's the script for my UI Action:

function createRequest() {
    var confirmation = confirm("Confirm Action: Convert Incident to Service Request.");
 
    if (!confirmation) {
        return;
    }
 
    var requested_for = g_form.getValue('caller_id');
    var shortDesc = encodeURIComponent(g_form.getValue('short_description'));
    var desc = encodeURIComponent(g_form.getValue('description'));
    var incidentSysId = g_form.getUniqueValue();
 
    var redirectUrl = '/employeehub?id=sc_cat_item&sys_id=45c51348dbe76d10867d9517f39619e9' +
                      '&sysparm_caller=' + requested_for +
                      '&sysparm_shortDesc=' + shortDesc +
                      '&sysparm_desc=' + desc +
                      '&sysparm_inc=' + incidentSysId;
 
    g_navigation.open(redirectUrl, "_blank");
}

 

 

Here's my catalog client script to populate the variable items:

 

function getParameterValue(name) {
    var queryString = top.location.href.split('?')[1];
    if (!queryString) return "";
    
    var params = new URLSearchParams(queryString);
    var value = params.get(name);
    return value ? decodeURIComponent(value) : "";
}
 
function onLoad() {
    g_form.setValue("requested_for", getParameterValue("sysparm_caller"));
    g_form.setValue("short_description_of_your_request", getParameterValue("sysparm_shortDesc"));
    g_form.setValue("additional_information", getParameterValue("sysparm_desc"));
	g_form.setValue("incident_number", getParameterValue("sysparm_inc"));
}

 

 

Scripts are working when clicking the button on native view but not on SOW. Sharing as well the script on declarative action assignment.

 

function onClick() {

    var incidentSysId = g_form.getUniqueValue();
    var caller = g_form.getValue('caller_id');
    var shortDes = g_form.getValue('short_description');
    var desc = g_form.getValue('description');

    var params = {};
    params.sysparm_parent_table = "incident";
    params.sysparm_parent_sys_id = incidentSysId;
    params.sysparm_caller = caller;
    params.sysparm_shortDesc = shortDes;
    params.sysparm_desc = desc;
	params.sysparm_inc = incidentSysId;

    // Open the catalog item
    g_service_catalog.openCatalogItem('sc_cat_item', '45c51348dbe76d10867d9517f39619e9', '-1', params);
}

 

Does autopopulation of variable item is supported on SOW? Thank you.

2 ACCEPTED SOLUTIONS

Ankur Bawiskar
Tera Patron
Tera Patron

@Rairai31 

the way how parameters are passed and grabbed is different for native and SOW/Workspace.

check my blog on how to handle this

1) check if it's native or sow and then handle the logic to grab/fetch the values.

Invoke record producer within Configurable workspace, send parameter & fetch the value 

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

View solution in original post

updated client script 

 

function getParameterValue(name) {

    var url = decodeURIComponent(top.location.href); //Get the URL and decode it
    if (url.indexOf('extra-params') > -1) { //workspace
        var workspaceParams = url.split('extra-params/')[1]; //Split off the url on Extra params
        var allParams = workspaceParams.split('/'); //The params are split on slashes '/'

        //Search for the parameter requested
        for (var i = 0; i < allParams.length; i++) {
            if (allParams[i] == name) {
                return allParams[i + 1];
            }
        }
    } else { //native
var url = new URL(self.location.href);
        var value = url.searchParams.get(name);
        return value ? decodeURIComponent(value) : "";
    }
}
 
function onLoad() {
    g_form.setValue("requested_for", getParameterValue("sysparm_caller"));
    g_form.setValue("short_description_of_your_request", getParameterValue("sysparm_shortDesc"));
    g_form.setValue("additional_information", getParameterValue("sysparm_desc"));
	g_form.setValue("incident_number", getParameterValue("sysparm_inc"));
}

View solution in original post

13 REPLIES 13

Ct111
Tera Sage

For Auto Population have you set UI Type to All in your script?

 

Someone attempted similar thing in this thread for autopopulation , worth giving a try atleast.

Thank you, @Ct111 for your response. Yes, the UI type is set to All. But still it does not populate the variable items. Thank you

Pranesh072
Mega Sage
Mega Sage

try this in your client script 

 

function getParameterValue(name) {
        var url = new URL(self.location.href);
        var value = url.searchParams.get(name);
        return value ? decodeURIComponent(value) : "";
    }

    function onLoad() {
        g_form.setValue("requested_for", getParameterValue("sysparm_caller"));
        g_form.setValue("short_description_of_your_request", getParameterValue("sysparm_shortDesc"));
        g_form.setValue("additional_information", getParameterValue("sysparm_desc"));
        g_form.setValue("incident_number", getParameterValue("sysparm_inc"));
    }

Thank you very much @Pranesh072. I have tried your script but still when clicking the button on SOW, it is not populating the variable items.

 

I added an alert on sysparm caller. It is returning value on default view but not on SOW. Thank you