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

Anand Kumar P
Giga Patron
Giga Patron

Hi @Dipika4 ,

If you want to create record onSubmit then go with record producer inplace of catalog item with above script.
Take below script as refrence

function onSubmit() {
var ga = new GlideAjax('UserOnboardingNonTGELocation');//name of script include
ga.addParam('sysparm_name', 'createLocation');//name of function on script include
ga.addParam('sysparm_user_location', g_form.getValue("new_location"));//name of field on the form I want to pass value for
ga.addParam('sysparm_user_line1', g_form.getValue("new_line1"),);//name of field on the form I want to pass value for
//add all fields like above
    ga.getXML(getLocationDetails);
  function getLocationDetails(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        alert(answer.toString());
}
}
 function in my script include

createLocation: function(){
var loc_name = this.getParameter('sysparm_user_location');
var line_name = this.getParameter('sysparm_user_line1');

var gr = new GlideRecord('cmn_location');
gr.initialize();
gr.first_name = loc_name;
gr.u_tda_location_code = line_name;
gr.insert();
 var locationId = gr.sys_id.toString();
        return locationId;

Thanks,

Anand

Hi @Anand Kumar P ,

 

I tried implementing script mentioned by you, but the thing is client script is not hitting the script include. In first line of script include I ran gs.log but even that it didn't logged anything.

 

createLocation: function(){
    gs.log("script Include");
var line_name = this.getParameter('sysparm_my_line');
var city_name = this.getParameter('sysparm_my_city');
var state_name = this.getParameter('sysparm_my_state');
var country_name = this.getParameter('sysparm_my_country');
var code_name = this.getParameter('sysparm_my_code');
var tda_name = this.getParameter('sysparm_my_tda');
gs.log("Location received is "+ line_name+ ", "+city_name +", "+ state_name+ ", "+country_name + ", " +code_name +", "+tda_name);
 
 
Thanks
 

Hi @Dipika4 ,

Make sure client callable checkbox in script include to true and application scope--All in script include.

Thanks,

Anand

Hi @Anand Kumar P ,

 

Yeah it is checked and scope is all.

Dipika4_0-1701760411203.png