Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-2024-08-06· OpenAIAi2 WildChat-4.8M (AI2)

In servicenow, I have the requirement if current logged in user's employee type is…

  1. User

    In servicenow, I have the requirement if current logged in user's employee type is permanent then only the catalog item requested for field populate the name of that user while raising the request

  2. Assistant

    To achieve the requirement in ServiceNow where the "Requested For" field in a catalog item is automatically populated with the name of the currently logged-in user, but only if the user's employee type is "permanent," you can use the following approach:

    Steps to Implement this Solution:

    1. Create a Catalog Client Script

      • Navigate to the catalog item where you want to implement this functionality.
      • Under the "Catalog Item" form, scroll down to find the "Catalog Client Scripts" related list.
      • Click on "New" to create a new Catalog Client Script.
    2. Configure the Catalog Client Script

      • Name: Give your script a meaningful name, like "Set Requested For if Permanent".

      • Type: Choose "onLoad" as the type. This ensures the script runs when the catalog item loads.

      • Active: Make sure this is checked.

      • Applies to: Select "Catalog Item" so it only applies to this item.

      • In the Script field, enter the following script:

        (function() {
            // Get the current logged-in user's sys_id
            var userSysId = g_user.userID;
        
            // Query the user's record to check the employee type
            var userGR = new GlideRecord('sys_user');
            if (userGR.get(userSysId)) {
                var employeeType = userGR.getValue('employee_type');
                
                // Check if the employee type is permanent
                if (employeeType === 'permanent') {
                    // If so, set the 'Requested For' field to the user's name
                    g_form.setValue('requested_for', g_user.userID);
                }
            }
        })();
        

    Explanation of the Script:

    • GlideRecord and Conditions: The script uses GlideRecord to query the sys_user table and fetch the employee type of the currently logged-in user.

    • Validation: It checks if the employee type field in this record is "permanent".

    • Set Requested For: If the condition is met, the g_form.setValue('requested_for', g_user.userID); sets the "Requested For" field to the logged-in user's ID.

    Important Considerations:

    • Employee Type Field: Make sure that the field employee_type exists in your sys_user table and contains values such as "permanent". Adjust the field and value if they differ in your instance.
    • Namespace (g_): The script uses the ServiceNow client-side API, hence the g_ functions are used, which are available in client-side scripting.
    • Security: Ensure that you have appropriate permissions to use GlideRecord in client scripts, as it's typically a security concern; you might want to rethink this if there's security bypassing involved.

    Testing:

    • Test the Script: After activating and saving your client script, preview the catalog item as a user whose employee type is "permanent" to ensure it auto-populates correctly.
    • Different Employee Types: Also, test with users of different employee types to confirm that the field does not auto-populate for them.

    This approach should accurately perform the task as per the requirement specified. If you encounter issues, you may want to look into debugging client scripts or checking that the field names and user attributes are correctly specified.