Concatenation of Name field of the user belonging to specific department without affecting users

ark257
Tera Contributor

I have written a business rule on sys_user table

 

before query

 

condition: Department.name=='XYZ'

 

var firstName = g_form.getValue('first_name');
var lastName = g_form.getValue('last_name');
var Name = firstName + ' ' + lastName;
g_form.setValue('Name', Name);

But the condition is returning null value when business rule is ran

5 REPLIES 5

M Iftikhar
Tera Sage

Hi @ark257,

The issue you’re running into is because g_form is only available on the client side (forms, UI Policies, client scripts, etc.). A Business Rule runs on the server side, so g_form.getValue() will always return null there. 

If your goal is to set the name field on the sys_user table based on first_name and last_name, you should switch to a before insert/update Business Rule and use the current object instead of g_form.

For example:
 

// Business Rule on sys_user
// When: before insert OR before update
if (current.department.name == 'XYZ') {
var firstName = current.first_name.toString();
var lastName = current.last_name.toString();
current.name = firstName + ' ' + lastName;
}

   

A couple of things to keep in mind: 

  • current.<field> is the right way to access field values in server-side scripts. 
  • The name field on sys_user is normally system-controlled, so confirm it’s okay to override in your instance. 

Docs that might help: 

Thanks & Regards,   

Muhammad Iftikhar   

If my response helped, please mark it as the accepted solution so others can benefit as well. 

Thanks & Regards,
Muhammad Iftikhar

If my response helped, please mark it as the accepted solution so others can benefit as well.

Hi @M Iftikhar I tried runing the script but it stii not changing the name format. FYI i am trying to concatenate as lastname + firstname

Ankur Bawiskar
Tera Patron
Tera Patron

@ark257 

Your question is not clear.

what's your business requirement?

Seems you want Name field to be populated with Concatenation value o First Name + Last Name

If yes then do this

1) before insert/update business rule

current.name = current.first_name + ' ' + current.last_name;

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Hi @Ankur Bawiskar  I tried runing the script but it stii not changing the name format. FYI i am trying to concatenate as lastname + firstname