The CreatorCon Call for Content is officially open! Get started here.

Auto-populate the existing software value

Abbas_Ahamed
Tera Contributor

Hi @Ankur Bawiskar ,

 

I've created two catalog items: one for new software requests and another for existing software. When a request is made for software that's already approved, a link to the approved software appears at the top of the new software request. Clicking the link should take me to the existing software catalog request. The script works as expected and I've shared it for reference. What I need now is, when clicking the link, to navigate from the new software request to the approved software request, and have the software name automatically populated in the software title field of the existing software request. How can I achieve this?

 

Catalog Client Script: Existing Softare Catalog(Onchange)

 

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || newValue === '') {

        return;

    }

 

    var absAjax = new GlideAjax('CheckExistingBARecord');

    absAjax.addParam('sysparm_name', 'CheckExistingBARecord'); // Match method name exactly

    absAjax.addParam('sysparm_appName', newValue);

    absAjax.getXML(function(response) {

        var result = response.responseXML.documentElement.getAttribute("answer");

        result = JSON.parse(result); // Parse the JSON string

 

        if (result.exists === true) {

 

            var links = result.nameArr.map(function(newValue) {

                var softwareTitle = encodeURIComponent(newValue);

                var dynamicUrl = '/com.glideapp.servicecatalog_cat_item_view.do?v=1&sysparm_id=f9e209223b18ae10dc52da34c3e45a6a&sysparm_name=' + softwareTitle;

                return '<a href="' + dynamicUrl + '" target="_blank">' + ' ' + newValue + '</a>';

 

            });

 

            var msg = " Existing Software's with similar name: " + result.nameArr.join(', ') + ".";

            g_form.showFieldMsg('software_title', msg, 'error');

 

            if (result.nameArr.length > 0) {

                g_form.setValue('select_software', result.nameArr[0]);

            }

 

            if (g_form.getTableName() !== 'sc_task') {

                g_form.addErrorMessage('Navigate to' + '' + links);

 

 

            } else {

                g_form.hideFieldMsg('software_title', true);

            }

        }

    });

}

 

Script Include:

 

var CheckExistingBARecord = Class.create();
CheckExistingBARecord.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

    CheckExistingBARecord: function() {
        var appName = this.getParameter('sysparm_appName');

        var result = {
            exists: false,
            nameArr: [],
            url: "",
            operational_status: []
        };

        var business_app = new GlideRecord('cmdb_ci_business_app');
        business_app.addQuery('name', 'CONTAINS', appName);
        business_app.setLimit(3);
        business_app.query();

        while (business_app.next()) {
            result.nameArr.push(business_app.getValue('name'));
            result.exists = true;
            result.url = gs.getProperty('glide.servlet.uri');
            result.operational_status.push(business_app.getValue('operational_status'));
        }
        return JSON.stringify(result);
    },

    type: 'CheckExistingBARecord'
});
 
 
Screen Shot:
 
The new software link is functioning and directs users to the existing software.
Abbas_Ahamed_0-1757920901884.png

 

Existing software: I need PingID to be automatically populated when I click the link.

Abbas_Ahamed_1-1757920936161.png

 

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Abbas_Ahamed 

I shared working solution some months ago here

need help in auto populating catalog item 1 variable values in to catalog item 2 variables 

pass the variable value in the URL and then write onLoad catalog client script on another catalog item to grab, extract and auto populate

1) update your existing onChange to include software sysId, use this updated list

                var dynamicUrl = '/com.glideapp.servicecatalog_cat_item_view.do?v=1&sysparm_id=f9e209223b18ae10dc52da34c3e45a6a&sysparm_name=' + softwareTitle + '&sysparm_softwaresysId=' + newValue;

2) then onLoad catalog client script on other catalog item to grab

function onLoad() {

    try {
        var url = top.location.href;
        if (window == null) {
            // for portal
            var software = new URLSearchParams(url).get("sysparm_softwaresysId");
            g_form.setValue('softwareVariable', software);
        }
    } catch (ex) {
        // for native
        var gUrl = new GlideURL();
        gUrl.setFromCurrent();
        var software1 = gUrl.getParam("sysparm_softwaresysId");
        g_form.setValue('softwareVariable', software1);
    }
}

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

12 REPLIES 12

@Abbas_Ahamed 

it means the onchange client script is not running on the reference variable?

I thought so and hence included sysId with newValue

ensure you pass 1 more value from JSON i.e. sysId and then include that in URL and that should work then.

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

@Ankur Bawiskar ,

 

Thank you for providing the solution. I'll check it, and if it works, I'll update it accordingly.

 

Thanks,

Abbas

@Ankur Bawiskar ,

 

I made a few small changes to the script, and now it's working as expected. Thank you for your suggestions.

 

Onchange Link Fix script:

 

Abbas_Ahamed_0-1757947542237.png

 

Existing catalog request includes an alert message:

Abbas_Ahamed_1-1757947604643.pngAbbas_Ahamed_2-1757947627349.png

Thanks,

Abbas