Cost Center not populating.

Shidhi
Tera Contributor

The cost Center is populating and getting cleared immediately. Again populating only on selecting another field.

 

client script:

 

function onLoad() {
   
var user_id = g_user.userID;
var ga = new GlideAjax('userDetailsUtil');//name of script include
ga.addParam('sysparm_name', 'getEmployeeDetails');//name of function on script include
ga.addParam('sysparm_user', user_id);//name of field on form triggering call
ga.getXML(EmployeeDetailsLookup); // Always try to use asynchronous call
}
 
// Callback function to process the response returned from the server
function EmployeeDetailsLookup(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var result = JSON.parse(answer);
g_form.setValue('requested_for',result.name);
g_form.setValue('cost_center',result.cc);
 
}
 
 
Script Include:
 

var userDetailsUtil = Class.create();
userDetailsUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getEmployeeDetails: function() {
var userName = this.getParameter('sysparm_user');
var user = new GlideRecord('sys_user');
var result = { //Create an object to store the User data
name: "",
cc: "",

};
if (user.get(userName)) {
result.name = user.name.toString(); // toString is different from getDisplayValue
result.cc = user.cost_center.toString(); // toString is different from getDisplayValue

}
return JSON.stringify(result);

},
type: 'userDetailsUtil'
});

 

cost center populated and got cleared off:

Shidhi_0-1716313131033.png

cost center is populating only once the cursor moves to another field:

Shidhi_1-1716313277389.png

Can anyone help me to populate cost center.

 

Thank you!

 
 
 
 
 
 
 
   

 

1 ACCEPTED SOLUTION

johnfeist
Mega Sage
Mega Sage

Hi Shidhi,

If you are seeing the cost center being populated and then disappearing, you are probably dealing with a case of dueling client scripts.  I would suggest that you check through all your client scripts for others that touch the cost center field.  It is possible to order the client scripts (you may need to add that existing field to the client script form) in case you have two On Loads that touch cost center to make sure that the one in question runs last.  Please keep in mind that On Change scripts also run as part of loading a record.

Hope that helps.

:{)

Helpful and Correct tags are appreciated and help others to find information faster

View solution in original post

3 REPLIES 3

johnfeist
Mega Sage
Mega Sage

Hi Shidhi,

If you are seeing the cost center being populated and then disappearing, you are probably dealing with a case of dueling client scripts.  I would suggest that you check through all your client scripts for others that touch the cost center field.  It is possible to order the client scripts (you may need to add that existing field to the client script form) in case you have two On Loads that touch cost center to make sure that the one in question runs last.  Please keep in mind that On Change scripts also run as part of loading a record.

Hope that helps.

:{)

Helpful and Correct tags are appreciated and help others to find information faster

Sandeep Rajput
Tera Patron
Tera Patron

@Shidhi Check if there is any onChange script defined on the cost center table which is clearing the value.

AshishKM
Kilo Patron
Kilo Patron

Hi @Shidhi , 

 

As you are using onLoad() script. It means  "Requester For" also pre populated as logged in User.

You can utilize the auto-populate feature for cost center becuase "Requested For" is reference type and User record has the cost center as reference field. 

 

AshishKM_0-1716321551420.png

 

-Thanks,

AshishKM

 

 


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution