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

@Ankur Bawiskar 

 

1. New software catalog Request: When I enter a software name, if that name already exists, a link is provided that should navigate to the existing software.

 

2. Existing software catalog request: Once I navigate to the existing catalog request, the software title should automatically populate in the software title variable field.

 

3. The software title variable field is a reference field and should get its value from the Business Application table.

 

My issue is that while the link works and the software title is auto-populated in the field, it should not take the exact record from the Business Application table. In the script, can I pass the sys_id and remove the sysparam_name?

 

Thanks,

Abbas

@Abbas_Ahamed 

yes name not required to be sent as you are not utilizing that.

since it's reference variable you should pass sysId in URL and populate sysId only in reference variable

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,

 

I provided the sys_id, but it isn't functioning as I expected. I've attached the script for your reference.

 

Abbas_Ahamed_0-1757943963967.png

 

Thanks,

Abbas

 

@Abbas_Ahamed 

you are passing 2 sysIds? result.sys_d and newValue?

why?

function onLoad() {

    try {
        var url = top.location.href;
        alert(url);
        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);
    }
}

also can you share what came in alert in the onLoad catalog client script in URL?

 

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 ,

 

Alert Message:

Abbas_Ahamed_1-1757945222957.png