Create location in cmn_location table, from Catalog item form.

Dipika4
Tera Contributor

Create location in cmn_location table, Whenever user will select others in the location field, new field will display to provide new location details like street, city, state, country, postal code. For this requirement I am using onSubmit catalog client script and script include but this is not working.

 

Client Script:

function onSubmit() {
   var obj = {
            "location": g_form.getValue("new_location"),
            "line": g_form.getValue("new_line1"),
            "tdaLocationCode": g_form.getValue("new_TDALocationCode"),
            "city": g_form.getValue("new_city"),
            "stateProvince": g_form.getValue("new_stateProvince"),
            "country": g_form.getValue("new_country"),
            "postalCode": g_form.getValue("new_postalCode")
        }
    var ga = new GlideAjax("UserOnboardingNonTGELocation");
    ga.addParam('sysparm_name', 'createLocation');
    ga.addParam('sysparm_location', JSON.stringify(obj));
    ga.getXML(getLocationDetails);

    function getLocationDetails(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        alert(answer.toString());
}
}
 
Script Include:
var UserOnboardingNonTGELocation = Class.create();
UserOnboardingNonTGELocation.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
createLocation: function(){
    var param = this.getParameter('sysparm_location');
    var newLoc = JSON.parse(param);
    gs.info("location "+newLoc.newline.toString()); //Even this log in not running.
    gs.info("location1 "+ newLoc);
       
        var newline = "";
        newline = newLoc.line.toString();;
        var newtdaLocation = "";
        newtdaLocation = newLoc.tdaLocationCode.toString();
        var newcity = "";
        newcity = newLoc.city.toString();
        var newstate = "";
        newstate = newLoc.stateProvince.toString();
        var newcountry = "";
        newcountry = newLoc.country.toString();
        var newcode = "";
        newcode = newLoc.postalCode.toString();
        var newlocation = "";
        newlocation = newline +", "+ newcity+ ", "+newstate;

var location_new = "";
        var grl = new GlideRecord('cmn_location');
        grl.initialize();
        grl.street = newline;
        grl.u_tda_location_code = newtdaLocation;
        grl.city = newcity;
        grl.state = newstate;
        grl.country = newcountry;
        grl.zip = newcode;
        grl.name = newline + ", " + newcity + ", " + newstate;
        location_new = grl.sys_id;
        grl.insert();
        return location_new;
   
},
    type: 'UserOnboardingNonTGELocation'
});

 

Kindly guide if anyone implemented something similar to this.

1 ACCEPTED SOLUTION

Hi @Dipika4 ,

AnandKumarP_0-1701760928892.pngAnandKumarP_1-1701761223357.png

 

AnandKumarP_2-1701761252608.png

For me its working fine in my pdi you can check from your end.
Scoped Application Script Logging. gs.print() and gs.log() are older and not available in scoped applications, whereas gs.debug(), gs.info(), gs.warn(), gs.error() work in both scoped applications and global.

Please mark it helpful and solution proposed.

Thanks,

Anand

View solution in original post

10 REPLIES 10

AnveshKumar M
Tera Sage
Tera Sage

Hi @Dipika4 

 

This request will not reach your Script Include as the form will submit immediately after the GlideAjax call.

 

Try the following catalog client script, whether the request reached the Client Callable Script Include or not.

 

function onSubmit() {
   var actionName = g_form.getActionName();
   var obj = {
            "location": g_form.getValue("new_location"),
            "line": g_form.getValue("new_line1"),
            "tdaLocationCode": g_form.getValue("new_TDALocationCode"),
            "city": g_form.getValue("new_city"),
            "stateProvince": g_form.getValue("new_stateProvince"),
            "country": g_form.getValue("new_country"),
            "postalCode": g_form.getValue("new_postalCode")
        }
    var ga = new GlideAjax("UserOnboardingNonTGELocation");
    ga.addParam('sysparm_name', 'createLocation');
    ga.addParam('sysparm_location', JSON.stringify(obj));
    ga.getXML(getLocationDetails);
    return false;

    function getLocationDetails(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        g_form.submit(actionName);
    }
}

 

Please mark my answer helpful and accept as a solution if it helped 👍✔️ 

Thanks,
Anvesh