How set default value of a variable using javascript

Alon Grod
Tera Expert

Hi,

 

I have the variable 'computer name' of type string in my 'create incident' record producer. I have the field 'u_computer_name' of type string on sys_user table. How can I set the default value of this variable inside the record producer to be like computer_name of the current login user when loading the record producer?

 

Screen Shot 2023-02-06 at 14.49.34.png

 

2 ACCEPTED SOLUTIONS

Basheer
Mega Sage

Hi @Alon Grod ,

You need to write an Client Script + Script Include to achieve this

Do as something like this onLoad

Client Script

function onLoad() {
    var ga = new GlideAjax('loggedUserData');
      ga.addParam('sysparm_name', 'name');
      ga.getXML(callback);
}
function callback(response) {
      var answer = response.responseXML.documentElement.getAttribute("answer");
//put this answer in your variable  
}

In Script Include

var loggedUserData= Class.create();
loggedUserData.prototype = Object.extendsObject(AbstractAjaxProcessor, {
name: function () {
return gs.getUser().u_computer_name;             
      }
});

 

Please hit like button if my suggestion has helped you in any way.
Please mark correct if my response has solved your query.

Cheers,
Mohammed Basheer Ahmed.

View solution in original post

Ankur Bawiskar
Tera Patron
Tera Patron

@Alon Grod 

this should work fine

javascript: gs.getUser().getRecord().getValue('u_computer_name');

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

View solution in original post

13 REPLIES 13

Alon Grod
Tera Expert

@Community Alums 

Community Alums
Not applicable

Hi @Alon Grod ,

 Try this sample example and replace with your field name and table :https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0724438

 

@Community Alums hi is not helpful, is there any chance helping with the script