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

Hi @Dipika4 ,

gs.log will not work in scoped application so use gs.info

Thanks,

Anand

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

Hi @Anand Kumar P ,

 

Thank you a lot. It worked.

 

Hi @Anand Kumar P 

 

In client script when I am trying to populate the field with the answer value, it's not populating although answer is passing sys_id and field to be populated is reference.


   function getLocationDetails(response){
    var answer = response.responseXML.documentElement.getAttribute("answer");
   
        alert("Passed answer is " + answer.toString()); // triggering alert
        g_form.setValue("new_location",answer.toString());     //not setting the value
   }

Hristo Ivanov
Kilo Sage

hey @Dipika4

 
Regarding this "gs.info("location "+newLoc.newline.toString()); //Even this log in not running.", it's not running cause you are trying to print before declaring the variable a couple of lines below.
 
It looks like you have the client callable checkmark set to true in the script include, so we can rule that out but you might want to add some info messages along the code to check what part of the script it gets to. From a first glance it looks like it should be working to be honest. 
 
Also, you can optimize your script by declaring new variables and assigning them with a value on the same line. 
Otherwise you might want to think about a scenario where you are also checking if the location that they are trying to add already exists