Values are not getting populated on the form

is_12
Tera Contributor

Hello,

For an address validation on the form, if the zipcode is present in the location it will validated else it should be calling 3rd party for the validation, for the else in the flow able to get the response but on the it is clearing the value (object is getting returned). 

is_12_0-1742337819997.png

Below is how I'm call the subflow in the script include :

getLocationDetails: function() {
              var zipcode = this.getParameter('sysparm_zipcode');
        //var countryCode = this.getParameter('syspram_countryCode');
        var city = this.getParameter('sysparm_city');
        var state = this.getParameter('sysparm_state');
        var country = this.getParameter('sysparm_country');
        var countryCode = '';
        
        var countryGR = new GlideRecord('core_country');
        countryGR.addQuery('name', country);
        countryGR.query();
        
        if (countryGR.next()) {
            countryCode = countryGR.getValue('iso3166_2'); // Assuming 'country_code' is the field name
        }
        var location = new GlideRecord('cmn_location');
        location.addQuery('zip', zipcode);
        location.query();
        gs.info('I**bleep**ainsidefunction7');
        gs.info('I**bleep**azipcode' + zipcode);
        if (location.next()) {
            gs.info('I**bleep**ainsidefunction8');
            return JSON.stringify({
                "country": location.getValue('country'),
                "state": location.getValue('state'),
                "city": location.getValue('city'),
                "countryCode": countryCode
            });

           
        } else if (!location.next()) {

            try {
                var inputs = {};
                inputs['state'] = state; // String
                inputs['country'] = country; // String
                inputs['countrycode'] = countryCode; // String
                inputs['city'] = city; // String
                inputs['zipcode'] = zipcode; // String

                // Start Asynchronously: Uncomment to run in background. Code snippet will not have access to outputs.
                // sn_fd.FlowAPI.getRunner().subflow('global.bt__call_the_mysite').inBackground().withInputs(inputs).run();

                // Execute Synchronously: Run in foreground. Code snippet has access to outputs.
                var result = sn_fd.FlowAPI.getRunner().subflow('x_bttm_bttsm_api.bt__call_the_mysite_v1').inForeground().withInputs(inputs).run();
                var outputs = result.getOutputs();
                var resppayload = outputs['resppayload'];
                // Current subflow has no outputs defined.      
            } catch (ex) {
                var message = ex.getMessage();
                gs.error(message);
            }

            return JSON.stringify({
                "country": resppayload.bestSuggested.country,
                "countryCode": resppayload.bestSuggested.countryCode, // Now correctly pulling the country code
                "state": resppayload.bestSuggested.county,
                "city": resppayload.bestSuggested.town
            });
And written an onchange catalog client script :
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    alert('script got executed');
    var zipcode = newValue;
    var city = g_form.getValue('city');
    var state = g_form.getValue('state_county_province');
    var country = g_form.getValue('country');

   

    // Call the GlideAjax script include
    var ga = new GlideAjax('BT_CSR_SSR_CatalogUtils');
    ga.addParam('sysparm_name', 'getLocationDetails');
    ga.addParam('sysparm_zipcode', zipcode);
    ga.addParam('sysparm_city',city);
    ga.addParam('sysparm_state',state);
    ga.addParam('sysparm_country',country);

    ga.getXMLAnswer(function(response) {
        alert(response);
        var locationDetails = JSON.parse(response);
        alert(locationDetails);
        if (locationDetails) {

            if (city != locationDetails.city) {
                g_form.setValue('city', locationDetails.city);
            }
            if (state != locationDetails.state) {
                g_form.setValue('state_county_province', locationDetails.state);
            }
            if (country != locationDetails.country) {
                g_form.setValue('country', locationDetails.country);

            }

            //g_form.setValue('country', locationDetails.country);
            //g_form.setValue('state_county_province', locationDetails.state);
            //g_form.setValue('city', locationDetails.city);
        }
    });
}
4 REPLIES 4

GopikaP
Mega Sage

Hi @is_12 , can you try to log outputs from the subflow itself?

Also try to log the JSON before returning in the else part. 

Subflows has output defined right? (There is a comment showing - '// Current subflow has no outputs defined. '). I am assuming you defined it later and edited the script in script include.

Also, return the JSON in the try block.

 

is_12
Tera Contributor

Can you help me with the script please

Sure, can you log the response coming back from subflow ? here - and let me know what you are getting in log? Also can you please share some screenshots of your subflow, and how outputs are mapped in subflow?

getLocationDetails: function() {
                var zipcode = this.getParameter('sysparm_zipcode');
                //var countryCode = this.getParameter('syspram_countryCode');
                var city = this.getParameter('sysparm_city');
                var state = this.getParameter('sysparm_state');
                var country = this.getParameter('sysparm_country');
                var countryCode = '';

                var countryGR = new GlideRecord('core_country');
                countryGR.addQuery('name', country);
                countryGR.query();

                if (countryGR.next()) {
                    countryCode = countryGR.getValue('iso3166_2'); // Assuming 'country_code' is the field name
                }
                var location = new GlideRecord('cmn_location');
                location.addQuery('zip', zipcode);
                location.query();
                gs.info('I**bleep**ainsidefunction7');
                gs.info('I**bleep**azipcode' + zipcode);
                if (location.next()) {
                    gs.info('I**bleep**ainsidefunction8');
                    return JSON.stringify({
                        "country": location.getValue('country'),
                        "state": location.getValue('state'),
                        "city": location.getValue('city'),
                        "countryCode": countryCode
                    });


                } else if (!location.next()) {

                    try {
                        var inputs = {};
                        inputs['state'] = state; // String
                        inputs['country'] = country; // String
                        inputs['countrycode'] = countryCode; // String
                        inputs['city'] = city; // String
                        inputs['zipcode'] = zipcode; // String

                        // Start Asynchronously: Uncomment to run in background. Code snippet will not have access to outputs.
                        // sn_fd.FlowAPI.getRunner().subflow('global.bt__call_the_mysite').inBackground().withInputs(inputs).run();

                        // Execute Synchronously: Run in foreground. Code snippet has access to outputs.
                        var result = sn_fd.FlowAPI.getRunner().subflow('x_bttm_bttsm_api.bt__call_the_mysite_v1').inForeground().withInputs(inputs).run();
                        var outputs = result.getOutputs();
                        var resppayload = outputs['resppayload'];
						gs.log("Response from subflow - "+resppayload);
						gs.log("country = "+resppayload.bestSuggested.country);
						return JSON.stringify({
                        "country": resppayload.bestSuggested.country,
                        "countryCode": resppayload.bestSuggested.countryCode, // Now correctly pulling the country code
                        "state": resppayload.bestSuggested.county,
                        "city": resppayload.bestSuggested.town
                    });
                        // Current subflow has no outputs defined.      
                    } catch (ex) {
                        var message = ex.getMessage();
                        gs.error(message);
                    }
                }
			},

 

Ankur Bawiskar
Tera Patron
Tera Patron

@is_12 

did subflow run fine and give correct output?

what debugging did you perform?

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