Action item script not working as expected
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2022 11:30 AM
I have created a ui parameter called affected_user in a function for the mobile app and I have set the default value to “javascript:gs.getUserID();”
when I tap the function in the app, the default value displays the logged in user as an affected user(as expected).
when I actually hit submit and create the incident, the affected user changes to the person who wrote the script (my admin account), because the input.affected_user returns “javascript:gs.getUserID();”
however if I actually select a user rather than leaving the default value, then the input.affected_user returns the sys id of the logged in user and everything works fine.
(function WriteBackAction(input) {
var gr = new GlideRecord(‘incident’);
gr.initialize();
gr.setValue(‘caller_id’, input.affected_user);
gr.setValue(‘short_description’, input.issue_description);
gr.insert();
})(input);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-02-2024 02:43 PM
did you solve this?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2024 09:48 PM
Hi Jose,
I addressed this issue in my previous response. Please see the detailed answer at the following link: View the Solution
Let me know if you need any further clarification.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2024 10:21 AM - edited 08-20-2024 10:16 PM
Step-by-Step Solution for Mapping Logged-In User in a Function Instance
Problem: When using a UI parameter with the default value "javascript:gs.getUserID();", the correct user is displayed in the mobile app UI, but on submission, the affected user is set to the person who wrote the script (admin account).
Solution:
Create a Function Instance:
- In ServiceNow, navigate to where you manage function instances and create a new function instance.
- Set the function type to Action and the context to Global.
Create and Configure the Action Item:
- In the function instance, add a new action item.
- Select the HR Core Case table (or the relevant table you are working with) since I am creating an HR case via a quick action.
- Set the action type to New.
Map Static Fields:
- Map the fields that will have static values, such as:
- State
- Source Type
- HR Service
- Assignment Group
- Map the fields that will have static values, such as:
Create a Parameter Screen for Dynamic Values:
- Create a parameter screen to collect dynamic inputs like:
- Description
- Priority
- Create a parameter screen to collect dynamic inputs like:
Map User-Related Fields to the Logged-In User:
- Add a variable to the parameter screen for mapping user-related fields. Here’s how:
- Name the variable as user (or any other appropriate name).
- Set the variable type to User. This will automatically retrieve the logged-in user's UserID.
- Add a variable to the parameter screen for mapping user-related fields. Here’s how:
Configure Action Item to Use the User Variable:
- In the action item configuration, map the following fields to variable.user:
- Opened by
- Opened for
- Subject person
- In the action item configuration, map the following fields to variable.user:
Verify the Setup:
- Now, In your case when the function is triggered in the mobile app, the affected_user field will be correctly set to the logged-in user, even when the user doesn't manually select themselves.
Explanation:
- By using the User variable type and mapping it directly to the user-related fields, the system correctly identifies and uses the logged-in user instead of defaulting to the script creator's account.
Mark my answer helpful & accepted if it helps you resolve your query.
Thanks,
Siddhesh Jadhav