Default value to auto-populate cost center of Requested for user

Amy13
Tera Contributor

In a variable (reference to the cost center table), I'm looking to set the default value to the cost center of the user in the Requested for variable. How do I do this? This is what I have currently, but it's not working.

 

find_real_file.pngfind_real_file.png

9 REPLIES 9

Hi,

are you in scoped app?

you can use this onChange client script on Requested for which will work on form load also

1) it would set the cost center of the requested for user

also when user changes it would set the value

function onChange(control, oldValue, newValue, isLoading) {

	if(newValue == ''){
		g_form.clearValue('cost_center');	// cost center variable name here
	}

	var ref = g_form.getReference('requested_for', callBackMethod);	// requestedFor variable name
}

function callBackMethod(ref){
	if(ref.cost_center)
		g_form.setValue('cost_center', ref.cost_center); // cost center variable name here
}

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Amy 

Another way

in the default value of variable add this

javascript: var costCenter = ''; var gr = new GlideRecord('sys_user'); gr.get(gs.getUserID()); costCenter = gr.getValue('cost_center'); costCenter;

Then use onChange client script on Requested For

function onChange(control, oldValue, newValue, isLoading) {

	if(isLoading)
		return;

	if(newValue == ''){
		g_form.clearValue('cost_center');	// cost center variable name here
	}

	var ref = g_form.getReference('requested_for', callBackMethod);	// requestedFor variable name
}

function callBackMethod(ref){
	if(ref.cost_center)
		g_form.setValue('cost_center', ref.cost_center); // cost center variable name here
}

Let me know if I have answered your question.

If so, please mark appropriate response as correct & helpful so that this thread can be closed and others can be benefited by this.

Regards
Ankur

 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

I also Have the exact requirement but instead of cost center , i need cost center code find_real_file.png@Ankur Bawiskar  can you u help me with the code

Please post a new question as this is quite older one and tag me there.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Have you tried what I mentioned? Simulated myself, works fine.

If it concerns onChange: with Quebec don't go for a scripted solution anymore. See the article I mentioned. ServiceNow introduced a really nice no-code solution for this.

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020, 2021 ServiceNow Community MVP
2020, 2021 ServiceNow Developer MVP

---

LinkedIn
Community article, blog, video list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn