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

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

function onChange(control, oldValue, newValue, isLoading) {
 if (isLoading || newValue === '') {

g_form.setValue("requestor",g_user.userID); //assuming you want to run it during onLoad as well.


//return;
}

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

}

else

{

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



}

unfortunately not changing the value back still.

Updated code below. This should work.

 

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

if (newValue == 'true') {

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

else

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

//Type appropriate comment here, and begin script below

}

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);
}

}