Action item script not working as expected

Developer28
Tera Expert

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);

3 REPLIES 3

Jose Santos Alv
Tera Contributor

did you solve this?

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.

Siddhesh Jadhav
Kilo Sage

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:

  1. 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.
  2. 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.
  3. Map Static Fields:

    • Map the fields that will have static values, such as:
      • State
      • Source Type
      • HR Service
      • Assignment Group
  4. Create a Parameter Screen for Dynamic Values:

    • Create a parameter screen to collect dynamic inputs like:
      • Description
      • Priority
  5. 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.
  6. 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
  7. 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