Need to populate the values based on the selected option from the one of the field

vinuth v
Tera Expert

Hi All,

 

I am working on one of the integration, based on the selected option on the form level I need to populate the values on another field.

I have created the REST message and using the api key under HTTP Headers. When I tried in the REST Message it's working fine.

I wrote the on change script on the Topic field.

 

vinuthv_0-1696513360260.png

When I select the "Cloud Provider" as Azure then "Topic" field contains some values based on the selected values from the "Cloud provider".

Cloud provider field type as Look up select Box and referring to u_ccoe_help_topic_map_list table.

vinuthv_1-1696513483005.png

If I select the "Topic" as "Extend Expiry of Sandbox subscription" then two more fields will visible  "Subscription Name" and "AssetID" I need to populate the values over here when I select this option "Extend Expiry of Sandbox subscription".

Responce : [{"item1":"DCO-AzurePOC-6,"item2":"2001"},{"item1":"DCO-AzurePOC-5","item2":"2083"}]

 

On the Subscription Name field I need to display item1 and on the AssetId field I need to display item2.

 

I tried with the client script like below and onChange of the Topic field

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
   

    if (newValue == 'Extend Expiry of Sandbox subscription') {

        alert('som new value:' + newValue);

        getSubIDSLIST();

    }

    function getSubIDSLIST() {
        alert("1st Test");
        var ga = new GlideAjax('SubscriptionIDs');
        ga.addParam('sysparm_name', 'getSubIDSLIST');
        ga.getXML(ajaxResponse);

    }

    function ajaxResponse(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        alert(answer);
        var x = answer;
        var y = JSON.parse(x);

        for (var i = 0; i < y.data.length; i++) {
            var arr = y.data[i];
            alert(arr);
            for (var s = 0; s < arr.length; s++) {
                alert(arr[s + 1]);
                g_form.addOption("subscription_name", arr[0], arr[0]);
                g_form.addOption("assetid", arr[1], arr[1]);

            }
        }

    }
}
 
 
 
Script Include:
var SubscriptionIDs = Class.create();
SubscriptionIDs.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getSubIDSLIST: function() {
        try {
            //gs.log("Enters try");
            var r = new sn_ws.RESTMessageV2('Azure Cloud','Default GET');
            //gs.log("Enters REST");
            //r.setStringParameterNoEscape('Token', 'token');
           // gs.log("Enters Method");
            var response = r.execute();
            //gs.log("response");
            var responseBody = response.getBody();
            var httpStatus = response.getStatusCode();
            gs.log("AWS SubID's step"+ responseBody+"++"+httpStatus);
            return responseBody;
           
         } catch (ex) {
             var message = ex.getMessage();
             gs.error(message);
         }


    },
   
    type: 'SubscriptionIDs'
});
 
 
The above script is not working and alert and logs are m=not coming.
 
 
Please any one suggest me,
Thanks in advance,
Vinuth
2 REPLIES 2

Anand Kumar P
Giga Patron
Giga Patron

Hi @vinuth v ,

If the log in client script  alert('som new value:' + newValue); is not getting the please check below newValue  exactly matches 'Extend Expiry of Sandbox subscription'. Make sure there are no leading/trailing spaces or any other discrepancies in the selected option's value.

    if (newValue == 'Extend Expiry of Sandbox subscription') {

If this solution is helpful, please consider marking it as "Solution Proposed."

  Thanks,
Anand    

Hi @Anand Kumar P 

 

I tried with the below script and Alert message is coming on the form level, but on the "Subscription Name" object is coming.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
   

    //if (newValue == 'Extend Expiry of Sandbox subscription') {
       
if (g_form.getValue('u_topic') == '4a0a957187e9711053d7419e8bbb35d2') {
        alert('som new value:' + g_form.getValue('u_topic'));

        getSubIDSLIST();

}

    function getSubIDSLIST() {
        alert("1st Test");
        var ga = new GlideAjax('SubscriptionIDs');
        ga.addParam('sysparm_name', 'getSubIDSLIST');
        ga.getXML(ajaxResponse);

    }

    function ajaxResponse(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        alert(answer);
        var x = answer;
        var y = JSON.parse(x);

        //for (var i = 0; i < y.data.length; i++) {
            for (var i = 0; i < y.length; i++) {
           // var arr = y.data[i];
            // var arr = y[i];
            // alert(arr);
            // for (var s = 0; s < arr.length; s++) {
            //     alert(arr[s + 1]);
            //     g_form.addOption("subscription_name", arr[0], arr[0]);
            //     g_form.addOption("assetid", arr[1], arr[1]);
            g_form.addOption("subscription_name", y[i], y[i]);

           // }
       


  }
    }
 }
 
 
This is the response  
[{"item1":"DCO-AzurePOC-6","item2":"2001582"},{"item1":"DCO-AzurePOC-5","item2":"2083112"}]
I need to populate on the "Subscription Name" and "AssetID" fields.