Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Script Include returning null

Vijay485
Tera Contributor

 I want a project account from employee table to catalog form

 whenever a user logs in his project account should be populated 

 

Client script 

 

 

function onLoad() {
//Type appropriate comment here, and begin script below
alert("hello script include");
var user = g_form.getValue('requested_by');
alert(user); // returning sys_id
var ga = new GlideAjax('ProjectAccount');
ga.addParam('sysparm_name','callback');
ga.addParam('sysparm_user','user');
ga.getXML(getDetails);
function getDetails(response){
var answer = response.responseXML.documentElement.getAttribute('answer');
alert(answer); // returning null
g_form.setValue('current.variables.project_account',answer);

}

 

Script include 

 

var ProjectAccount = Class.create();
ProjectAccount.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
 
    callback: function() {
        var user = this.getParameter('sysparm_user');
        var gr = new GlideRecord('x_1037697_corpor_0_employee');
        gr.addQuery('sys_id',gs.getUserID);
        gr.query();
        //gs.debug('E-voucher ='+gr.getRowCount);
        if (gr.next()) {
            var pa = gr.project_account;
//gs.info("pa acccount"+ pa);
gs.log("pa acccount"+ pa);
return pa;
        }
 
    },

 

 

5 REPLIES 5

Tony Chatfield1
Kilo Patron

Hi, have you tested your script-include\validated that it is returning a result to your ajax call?
It looks like you have a syntax errors with method calls not being completed with () eg

gr.addQuery('sys_id',gs.getUserID());
//gs.debug('E-voucher ='+gr.getRowCount());

Aqib4
Kilo Guru

It seems like there are a couple of issues in your code that need to be addressed:

1. In your client script, you're passing the string `'user'` to the GlideAjax parameter instead of the actual variable `user`. You should remove the quotes around `user` so that the actual value of the variable is sent.

2. In your script include, you're not sending the response back properly. You need to use `gs.print` or `this.writeOutput` to send the response back to the client.

Here's the corrected code:

**Client Script:**
function onLoad() {
var user = g_form.getValue('requested_by');
var ga = new GlideAjax('ProjectAccount');
ga.addParam('sysparm_name', 'callback');
ga.addParam('sysparm_user', user); // Pass the user variable, not the string 'user'
ga.getXML(getDetails);
function getDetails(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
g_form.setValue('current.variables.project_account', answer);
}
}
```

**Script Include:**

var ProjectAccount = Class.create();
ProjectAccount.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
callback: function() {
var user = this.getParameter('sysparm_user');
var gr = new GlideRecord('x_1037697_corpor_0_employee');
gr.addQuery('sys_id', user); // Use user to query the employee table
gr.query();
if (gr.next()) {
var pa = gr.getValue('project_account');
this.writeOutput(pa); // Send the project account back to the client
}
},
type: 'ProjectAccount'
});
```

With these corrections, your client script should properly send the `requested_by` value to the server, and the script include should return the project account value back to the client. Make sure that `project_account` is a valid field in the `x_1037697_corpor_0_employee` table and `requested_by` is a valid field in your form.

Hi @Vijay485, I hope this solution resolves your issue. Please give a thumbs-up if it does.

Hi @Vijay485 
Thanks for marking my answer as helpful. If it helped you in any way please accept the solution so that it will be beneficial to the future readers with the same query.

 

Regards: Raja

swathisarang98
Giga Sage

Hi @Vijay485 ,

 

There are few errors in the code,

1. remove single inverted comma from users in client script

    ga.addParam('sysparm_user',user);

2. you are passing user sys_id but you are not using it 

3. correct the addQuery

     gr.addQuery('sys_id', gs.getUserID());

4. Try adding toString()

    return pa.toString();

5. In client script current is not accessible 

    g_form.setValue('current.variables.project_account',answer); 

instead try g_form.setValue('variable backend name',answer);

 

 

if correcting these also not helping please let me know,

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang