incedent

mahesh009
Mega Guru

How can I create a custom field on the Incident form that automatically populates with the current user's name?

6 ACCEPTED SOLUTIONS

Manikanta Kota
Mega Guru

Hi @mahesh009 

1.Navigate to the Incident form.
2.Right-click on the form header and select Configure > Form Layout.
3.In the Form Layout, under the Available section, click on Create New Field ,field type  'String' .
4.in Client scripts create new onload script 

ex:

(function executeRule(current, previous /*null when async*/) {

var userName = gs.getUser().getDisplayName();

g_form.setValue('u_current_user', userName);
})(current, previous);

 

Should my answer prove to be helpful, kindly mark it as such by clicking "Accept as Solution" and "Helpful."

 

Regards,

Manikanta. Kota

View solution in original post

Anantha27
Mega Guru

Hi @mahesh009 

When we open the incident table, we can configure the form layout and create new custom field to the incident form.

And we can write a client script on the incident table onchange() to auto populate the field on the new field.

Thank you.

View solution in original post

Ramesh_143
Giga Guru

Hi @mahesh009 , 

To automatically populate the "assigned_to" field on the Incident form with the current user's name, you can either use a Business Rule or a Client Script.

Business Rule:

  • Create a new Business Rule on the Incident table.
  • Set the "When to run" condition to "Before Insert".
  • In the script, assign the current user's name to the "assigned_to" field using current.assigned_to = gs.getUser().name;.

Client Script:

  • Create a new Client Script on the Incident table.
  • Set the "Type" to "OnLoad".
  • In the script, set the value of the "assigned_to" field to the current user's name using g_form.setValue('assigned_to', g_user.name);.

Choose the method that best suits your requirements based on whether you need the field to be populated before or after the incident is saved. 
Regards,

Ramesh

View solution in original post

Sai Krishna6147
Mega Guru

Hi @mahesh009 

we can choose the method that best suits your requirements based on whether you need the field to be populated before or after the incident is saved. 

Business Rule:

  • Create the custom field
  • To populate the field Automatically, we have create business rule
  • in adavanced tab ,add a script

 

if (current.u_current_user.nil()) { // Check if the field is empty
    current.u_current_user = gs.getUser().getName(); // Get current user's name
}

 

Client Script:

  • Create a new Client Script on the Incident table.
  • Set the "Type" to "OnLoad".
  • In the script, set the value of the "assigned_to" field to the current user's name using 

 

g_form.setValue('assigned_to', g_user.name);

.

 

Should my answer prove to be helpful, kindly mark it as such by clicking "Accept as Solution" and "Helpful."

 

Regards,

Venkata Sai Krishna

View solution in original post

VishaalRanS
Tera Guru

Hi @mahesh009 

 

You Can Add Client Script for Auto-Population:

 

To automatically populate this field with the current user's name, you need to create a client script:

  • Navigate to System Definition > Client Scripts.
  • Click on New to create a new client script.
  • Fill out the form as follows:
    • Name: Give it a descriptive name (e.g., "Auto-populate Current User").
    • Table: Select Incident.
    • Type: Choose onLoad.

In the script section, add the following code:

 

function onLoad() {                                                      // Get current user's name

var userName = g_user.getDisplayName();             // Set value to custom field

 g_form.setValue('u_current_user', userName);

}

 

Thanks, and Regards

Vishaal

Please mark this response as correct or helpful if it assisted you with your question.

View solution in original post

Aditya02
Tera Guru

Hi @mahesh009 ,

 

Refer these items to get your work done.., These can help you to find better Solution.

 

https://www.servicenow.com/community/developer-forum/autopopulate-user-details-onchange-of-user-name...

 

https://www.servicenow.com/community/developer-forum/how-can-i-populate-a-custom-field-created-by-te...

 

 

===================================***************=========================================

"If you found my answer helpful, please give it a like and mark it as the accepted solution. It helps others find the solution more easily and supports the community!"

 

Thanks & Regards,

Aditya

=====================================***********==========================================

 

 

View solution in original post

7 REPLIES 7

Sai Krishna6147
Mega Guru

Hi @mahesh009 

we can choose the method that best suits your requirements based on whether you need the field to be populated before or after the incident is saved. 

Business Rule:

  • Create the custom field
  • To populate the field Automatically, we have create business rule
  • in adavanced tab ,add a script

 

if (current.u_current_user.nil()) { // Check if the field is empty
    current.u_current_user = gs.getUser().getName(); // Get current user's name
}

 

Client Script:

  • Create a new Client Script on the Incident table.
  • Set the "Type" to "OnLoad".
  • In the script, set the value of the "assigned_to" field to the current user's name using 

 

g_form.setValue('assigned_to', g_user.name);

.

 

Should my answer prove to be helpful, kindly mark it as such by clicking "Accept as Solution" and "Helpful."

 

Regards,

Venkata Sai Krishna

VishaalRanS
Tera Guru

Hi @mahesh009 

 

You Can Add Client Script for Auto-Population:

 

To automatically populate this field with the current user's name, you need to create a client script:

  • Navigate to System Definition > Client Scripts.
  • Click on New to create a new client script.
  • Fill out the form as follows:
    • Name: Give it a descriptive name (e.g., "Auto-populate Current User").
    • Table: Select Incident.
    • Type: Choose onLoad.

In the script section, add the following code:

 

function onLoad() {                                                      // Get current user's name

var userName = g_user.getDisplayName();             // Set value to custom field

 g_form.setValue('u_current_user', userName);

}

 

Thanks, and Regards

Vishaal

Please mark this response as correct or helpful if it assisted you with your question.

Aditya02
Tera Guru

Hi @mahesh009 ,

 

Refer these items to get your work done.., These can help you to find better Solution.

 

https://www.servicenow.com/community/developer-forum/autopopulate-user-details-onchange-of-user-name...

 

https://www.servicenow.com/community/developer-forum/how-can-i-populate-a-custom-field-created-by-te...

 

 

===================================***************=========================================

"If you found my answer helpful, please give it a like and mark it as the accepted solution. It helps others find the solution more easily and supports the community!"

 

Thanks & Regards,

Aditya

=====================================***********==========================================