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.

If checkbox is checked auto populate reference filed

Bidduam
Tera Guru

For a catalog item I'm creating I need to populate a reference field if checkbox is checked, with the current user.

 

Checkbox variable field:

checkbox_1

 

Reference field on sys_user table:

name_field

 

If checkbox_1 = true then name_field = current users name

 

I figure this would require a Catalog Client Script?

Not sure how to write it - if anyone is able to assist please?

1 ACCEPTED SOLUTION

Ok thank you @Anirudh Pathak I've tried it and hasn't worked unfortunately

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

    //Type appropriate comment here, and begin script below
    var check = g_form.getValue('checkbox_1');
    if (check == true) {
        g_form.setValue('name_field', g_user.userID);
    } else {
        g_form.clearValue('name_field');
    }
}

 

 

View solution in original post

9 REPLIES 9

Hi @Bidduam ,

Can you update your code to the below one -

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

    //Type appropriate comment here, and begin script below
   
    if (newValue == 'true') {
        g_form.setValue('name_field', g_user.userID);
    } else {
        g_form.clearValue('name_field');
    }
}

@Anirudh Pathak that worked perfectly thank you 😀

Hi @Bidduam ,

You can mark my answer as accepted and helpful if it resolves your issue. 

Thank you!

Vrushali  Kolte
Mega Sage

Hello @Bidduam ,

 

You need to create on change client script for checkbox variable and write the script as follows :

Note: Please replace the variable name as per your case.

VrushaliKolte_0-1715145978355.png

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }

    if(newValue == 'true' || newValue == true){
		var user = g_user.userID; //get current user ID
		alert('user '+ user);
		g_form.setValue('assigned_to',user); // replace assigned_to with actual variable name 
	}
   //Type appropriate comment here, and begin script below
   
}

 Please mark my answer as Accepted ✔️ and Helpful 👍 based on the impact.

Ranjit Nimbalka
Mega Sage

Hi @Bidduam ,

 

Write a on change client script on checkbox field and write below code in a script.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }

   if(newValue=='true')

   {
	g_form.setValue('field_value',g_user.userID);
   }
   else
   {
	g_form.clearValue('field_value');
   }
   
}

  

RanjitNimbalka_0-1715146502883.png

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Regards,
Ranjit