- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-29-2024 10:19 PM
How can I create a custom field on the Incident form that automatically populates with the current user's name?
Solved! Go to Solution.
- 1,599 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-29-2024 10:34 PM - edited ‎09-29-2024 10:36 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-29-2024 10:53 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-29-2024 10:59 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-29-2024 11:34 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-29-2024 11:47 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-30-2024 12:43 AM
Hi @mahesh009 ,
Refer these items to get your work done.., These can help you to find better Solution.
===================================***************=========================================
"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
=====================================***********==========================================
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-29-2024 10:34 PM - edited ‎09-29-2024 10:36 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-29-2024 10:41 PM
Hi @mahesh009
To create a custom field on the Incident form in ServiceNow that automatically populates with the current user's name, follow these steps:
Add a New Custom Field:
Navigate to Incident > All.
Open any existing Incident or click on New to create a new one.
Right-click the header of the form and select Configure > Form Layout.
In the Form Layout section, under the Create New Field tab:
Type: Select String (or Reference to sys_user if you want it to store the actual user reference).
Label: Enter the desired label, e.g., "Current User".
Name: The field will automatically be given a name like u_current_user.
Click Add to include this field on the form.
Could you please clarify if you want to populate the logged-in user value in the custom field or the 'first name' and 'last name' of the current user?
Please mark this response as Helpful or Accepted if this solution works for you.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-29-2024 10:53 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-29-2024 10:59 PM
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