ServiceNow GlideAjax issue

deepamtrivedi
Kilo Contributor

Hi All,

I'm trying to update a field in the users profile when they login.

I thought I'd be able to do this by using script includes but it does not seem to work.

Just as a basic example I get the users details based on their user name and output the users last_name field, or "TEST" if

no user name is specified.

When I call the script include function using GlideAjax in the client script I get some odd errors.

On IE i get 'Promise' is undefined

And on Chrome I get cannot read property 'documentElement' of undefined.

I get these error regardless of where on the site I write the code, as i tried to add this to the login page but also tried to get the results on the Service catalog page.

please see below for my code.

Script Include:

var DeepamTest = Class.create();

DeepamTest.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    myFunction : function(UserName) {

                  var userName = this.getParameter('sysparm_user_id');

                  var gr = new GlideRecord('sys_user');

   

                  if(UserName != null && UserName != "") {

                          userName = UserName;

                  }

                  gr.addQuery('user_name', userName);

                  gr.query();

                  if(userName != null && userName != "" && gr.next()) {

                            //gr.city = "TEST";

                            //gr.update();

                            return gr.last_name.toString();

                  }

                  else {

                            return "TEST";

                  }

        }

});

In Client Script:

// the script include to call

var ga = new GlideAjax('DeepamTest');

// the name of the function

ga.addParam('sysparm_name', 'myFunction');

ga.addParam('sysparm_user_id', "SomeUserName");

// Our callback function, which will process the response.

ga.getXML(myCallBack);

function myCallBack(response) {

        //Dig out the 'answer' attribute, which is what our function returns.

        var result = response.responseXML.documentElement.getAttribute("answer");

        alert(result);

        console.log(JSON.stringify(response));

}

Result output:

{"type":"basic"}

1 ACCEPTED SOLUTION

Alikutty A
Tera Sage

Hi,



What are you trying to update on user profile upon login and where are you executing this script?



You can write a Script Action on the 'login' event and write this script for updating any value on user profile after they have logged in.



var gr = new GlideRecord('sys_user');


if(gr.get('user_name', event.parm1)){


    gr.field_name = 'value';


  gr.update();


}



Thanks


Please Hit like, Helpful or Correct depending on the impact of the response


View solution in original post

2 REPLIES 2

Alikutty A
Tera Sage

Hi,



What are you trying to update on user profile upon login and where are you executing this script?



You can write a Script Action on the 'login' event and write this script for updating any value on user profile after they have logged in.



var gr = new GlideRecord('sys_user');


if(gr.get('user_name', event.parm1)){


    gr.field_name = 'value';


  gr.update();


}



Thanks


Please Hit like, Helpful or Correct depending on the impact of the response


Thank you Alikutty,



I'm updating a custom field called: u_authorizedattachments



I hadn't realized there was a way to trigger server scripts on events, this was very useful. your suggestion worked perfectly.



Cheers,



Deepam