Set value to logged in user

Nicole_k
Kilo Expert

Hi All,

I am not a Java dev and I just need some help on how to set a value to be the current logged in user if a checkbox is true

Variable is requestor

Default value is javascript:gs.getUserID()

 

If checkbox "newuser" is false then I want to set requestor to the logged in user, else set the request to blank.

By Default the newuser feild is false. The requestor feild loads correct with the logged in users name.

If i set new user to true it makes request blank which is great.

If i then set new user to false again the requestor stays blank. I want the requestor to be the logged in user. 

 

Here is what I have so far, Can anyone help?

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
g_form.setValue("requestor",************);
}

if (newValue != '') {
g_form.setValue("requestor","");

}

}

1 ACCEPTED SOLUTION

Abhinay Erra
Giga Sage

Here you go

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
g_form.setValue("requestor",g_user.userID);
return;
}

if (newValue == 'true') {
g_form.setValue("requestor","");

}
else{
	g_form.setValue("requestor",g_user.userID);
}

}

View solution in original post

7 REPLIES 7

Thank you this worked perfectly

Hey Nicole,

 

Is the code I shared not working with removing g_form.setValue from IsLoading block? The reason I ask as you already have default value set via variable default value javascript:gs.getUserID() so the setValue line in the script is no longer required.

 

Thanks,

Pradeep Sharma

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

I have further modified your code. Here you go.

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	
	if (newValue == 'true') {
		g_form.setValue("requestor",g_user.userID);
		
	}
	
	else
		
	{
		
		g_form.setValue("requestor", '');
	}
	
	//Type appropriate comment here, and begin script below
	
}