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

@Rairai31  try this 

 

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 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"));
}

 

declarative action

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', params);
}

 

 

@Pranesh072 This is great! The onLoad did work. It has populated the variable items on SOW but when I tried on native view, I got a Javascript browser error. I have deactivated the declarative action since I enabled the Workspace form button on the UI action. @Ankur Bawiskar 

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"));
}

@Rairai31 

I already informed you need to separate it out.

1) get URL

2) check if it's SOW -> then my logic to grab

3) else -> your logic to grab

something like this

function onLoad() {

    var url = top.location.href;
    var urlParameterValue = '';
    if (url.indexOf('/sow') > -1) { // check if it's configurable workspace
        var url_cwf = decodeURIComponent(url);
        var workspaceParams = url_cwf.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] == 'sysparm_sysId') { // give here the name of parameter to fetch
                urlParameterValue = allParams[i + 1];
            }
        }
        alert(urlParameterValue);
    } else {
        // native your logic
    }
}

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

@Rairai31 

Hope you are doing good.

Did my reply answer your question?

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