<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>question script in Community Central forum</title>
    <link>https://www.servicenow.com/community/community-central-forum/script/m-p/3229990#M2684</link>
    <description>&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;P&gt;(function run() {&lt;BR /&gt;// Step 1: Greet the user and ask for their email.&lt;BR /&gt;var email = prompt("Hello! I can help you assign roles. Please provide the email ID of the user.");&lt;/P&gt;&lt;P&gt;// Step 2: Check if the email is valid.&lt;BR /&gt;if (!email) {&lt;BR /&gt;return "You did not provide an email address. Please provide a valid email ID.";&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;// Step 3: Ask the user for the role they need to assign.&lt;BR /&gt;var role = prompt("What role would you like to assign to the user?");&lt;/P&gt;&lt;P&gt;// Step 4: Check if the role is valid.&lt;BR /&gt;if (!role) {&lt;BR /&gt;return "You did not provide a role. Please provide a valid role name.";&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;// Step 5: Search for the user by email in the sys_user table.&lt;BR /&gt;var userGr = new GlideRecord('sys_user');&lt;BR /&gt;userGr.addQuery('email', email);&lt;BR /&gt;userGr.query();&lt;/P&gt;&lt;P&gt;// Step 6: Check if the user exists.&lt;BR /&gt;if (!userGr.next()) {&lt;BR /&gt;return "Sorry, I couldn't find a user with that email address. Please check the email and try again.";&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;// Step 7: Check if the role exists in sys_user_role_list.&lt;BR /&gt;var roleGr = new GlideRecord('sys_user_role');&lt;BR /&gt;roleGr.addQuery('name', role);&lt;BR /&gt;roleGr.query();&lt;/P&gt;&lt;P&gt;if (!roleGr.next()) {&lt;BR /&gt;return "Sorry, I couldn't find the role '" + role + "'. Please provide a valid role.";&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;// Step 8: Assign the role to the user.&lt;BR /&gt;var userRoleGr = new GlideRecord('sys_user_roles');&lt;BR /&gt;userRoleGr.initialize();&lt;BR /&gt;userRoleGr.user = userGr.sys_id; // Assign the user sys_id&lt;BR /&gt;userRoleGr.role = roleGr.sys_id; // Assign the role sys_id&lt;BR /&gt;userRoleGr.insert(); // Insert the record into the sys_user_roles table&lt;/P&gt;&lt;P&gt;// Step 9: Return success message to the user.&lt;BR /&gt;return "The role '" + role + "' has been successfully assigned to " + userGr.name + " with email " + email + ".";&lt;BR /&gt;})();&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;But it is giving this message as visible in the screenshot. So what will be the correct script code.&lt;/P&gt;&lt;P&gt;It is for&amp;nbsp;is designed to assist in assigning roles to users. It greets the user, asks for their email ID and the role they need to provide, extracts the role from the sys_user_role_list, updates the user's role in the sys_user table, and informs the user once the process is completed. The intended audience for this agent is ServiceNow users who need to assign roles to other users.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
    <pubDate>Mon, 07 Apr 2025 11:50:28 GMT</pubDate>
    <dc:creator>AnanyaT</dc:creator>
    <dc:date>2025-04-07T11:50:28Z</dc:date>
    <item>
      <title>script</title>
      <link>https://www.servicenow.com/community/community-central-forum/script/m-p/3229990#M2684</link>
      <description>&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;P&gt;(function run() {&lt;BR /&gt;// Step 1: Greet the user and ask for their email.&lt;BR /&gt;var email = prompt("Hello! I can help you assign roles. Please provide the email ID of the user.");&lt;/P&gt;&lt;P&gt;// Step 2: Check if the email is valid.&lt;BR /&gt;if (!email) {&lt;BR /&gt;return "You did not provide an email address. Please provide a valid email ID.";&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;// Step 3: Ask the user for the role they need to assign.&lt;BR /&gt;var role = prompt("What role would you like to assign to the user?");&lt;/P&gt;&lt;P&gt;// Step 4: Check if the role is valid.&lt;BR /&gt;if (!role) {&lt;BR /&gt;return "You did not provide a role. Please provide a valid role name.";&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;// Step 5: Search for the user by email in the sys_user table.&lt;BR /&gt;var userGr = new GlideRecord('sys_user');&lt;BR /&gt;userGr.addQuery('email', email);&lt;BR /&gt;userGr.query();&lt;/P&gt;&lt;P&gt;// Step 6: Check if the user exists.&lt;BR /&gt;if (!userGr.next()) {&lt;BR /&gt;return "Sorry, I couldn't find a user with that email address. Please check the email and try again.";&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;// Step 7: Check if the role exists in sys_user_role_list.&lt;BR /&gt;var roleGr = new GlideRecord('sys_user_role');&lt;BR /&gt;roleGr.addQuery('name', role);&lt;BR /&gt;roleGr.query();&lt;/P&gt;&lt;P&gt;if (!roleGr.next()) {&lt;BR /&gt;return "Sorry, I couldn't find the role '" + role + "'. Please provide a valid role.";&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;// Step 8: Assign the role to the user.&lt;BR /&gt;var userRoleGr = new GlideRecord('sys_user_roles');&lt;BR /&gt;userRoleGr.initialize();&lt;BR /&gt;userRoleGr.user = userGr.sys_id; // Assign the user sys_id&lt;BR /&gt;userRoleGr.role = roleGr.sys_id; // Assign the role sys_id&lt;BR /&gt;userRoleGr.insert(); // Insert the record into the sys_user_roles table&lt;/P&gt;&lt;P&gt;// Step 9: Return success message to the user.&lt;BR /&gt;return "The role '" + role + "' has been successfully assigned to " + userGr.name + " with email " + email + ".";&lt;BR /&gt;})();&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;But it is giving this message as visible in the screenshot. So what will be the correct script code.&lt;/P&gt;&lt;P&gt;It is for&amp;nbsp;is designed to assist in assigning roles to users. It greets the user, asks for their email ID and the role they need to provide, extracts the role from the sys_user_role_list, updates the user's role in the sys_user table, and informs the user once the process is completed. The intended audience for this agent is ServiceNow users who need to assign roles to other users.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Mon, 07 Apr 2025 11:50:28 GMT</pubDate>
      <guid>https://www.servicenow.com/community/community-central-forum/script/m-p/3229990#M2684</guid>
      <dc:creator>AnanyaT</dc:creator>
      <dc:date>2025-04-07T11:50:28Z</dc:date>
    </item>
    <item>
      <title>Re: script</title>
      <link>https://www.servicenow.com/community/community-central-forum/script/m-p/3230149#M2687</link>
      <description>&lt;P&gt;Hello &lt;a href="https://www.servicenow.com/community/user/viewprofilepage/user-id/856505"&gt;@AnanyaT&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The table in which you can assign role to user is "sys_user_has_role" table and not "sys_user_roles"&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Correct this and let me know the updates.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&lt;STRONG&gt;Kindly mark my answer as helpful and accept solution if it helped you in anyway. This will help me be recognized for the efforts and also move this questions from unsolved to solved bucket.&amp;nbsp;&lt;/STRONG&gt;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&lt;STRONG&gt;&amp;nbsp;&lt;/STRONG&gt;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&lt;STRONG&gt;Regards,&lt;/STRONG&gt;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&lt;STRONG&gt;&amp;nbsp;&lt;/STRONG&gt;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&lt;STRONG&gt;Shivalika&amp;nbsp;&lt;/STRONG&gt;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&lt;STRONG&gt;&amp;nbsp;&lt;/STRONG&gt;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&lt;STRONG&gt;My LinkedIn - &lt;A href="https://www.linkedin.com/in/shivalika-gupta-540346194" target="_blank"&gt;https://www.linkedin.com/in/shivalika-gupta-540346194&lt;/A&gt;&lt;/STRONG&gt;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&lt;STRONG&gt;&amp;nbsp;&lt;/STRONG&gt;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;&lt;STRONG&gt;My youtube - &lt;A href="https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&amp;amp;si=0WynLcOw" target="_blank"&gt;https://youtube.com/playlist?list=PLsHuNzTdkE5Cn4PyS7HdV0Vg8JsfdgQlA&amp;amp;si=0WynLcOw&lt;/A&gt;&lt;/STRONG&gt;&lt;/EM&gt;&lt;EM&gt;&lt;STRONG&gt;NeEISQCY&lt;/STRONG&gt;&lt;/EM&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 07 Apr 2025 13:31:19 GMT</pubDate>
      <guid>https://www.servicenow.com/community/community-central-forum/script/m-p/3230149#M2687</guid>
      <dc:creator>Shivalika</dc:creator>
      <dc:date>2025-04-07T13:31:19Z</dc:date>
    </item>
    <item>
      <title>Re: script</title>
      <link>https://www.servicenow.com/community/community-central-forum/script/m-p/3230232#M2688</link>
      <description>&lt;P&gt;&lt;a href="https://www.servicenow.com/community/user/viewprofilepage/user-id/856505"&gt;@AnanyaT&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As shared by &lt;a href="https://www.servicenow.com/community/user/viewprofilepage/user-id/764476"&gt;@Shivalika&lt;/a&gt; please correct the table name and it should work&lt;/P&gt;
&lt;P&gt;this line&lt;/P&gt;
&lt;P&gt;var userRoleGr = new GlideRecord('&lt;STRONG&gt;sys_user_has_role&lt;/STRONG&gt;');&lt;/P&gt;
&lt;P&gt;If my response helped please mark it correct and close the thread so that it benefits future readers.&lt;/P&gt;</description>
      <pubDate>Mon, 07 Apr 2025 14:18:17 GMT</pubDate>
      <guid>https://www.servicenow.com/community/community-central-forum/script/m-p/3230232#M2688</guid>
      <dc:creator>Ankur Bawiskar</dc:creator>
      <dc:date>2025-04-07T14:18:17Z</dc:date>
    </item>
    <item>
      <title>Re: script</title>
      <link>https://www.servicenow.com/community/community-central-forum/script/m-p/3231009#M2693</link>
      <description>&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="AnanyaT_2-1744090187810.png" style="width: 400px;"&gt;&lt;img src="https://www.servicenow.com/community/image/serverpage/image-id/433167i66B13761E26BA194/image-size/medium/is-moderation-mode/true?v=v2&amp;amp;px=400" role="button" title="AnanyaT_2-1744090187810.png" alt="AnanyaT_2-1744090187810.png" /&gt;&lt;/span&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;giving this error&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Tue, 08 Apr 2025 05:30:42 GMT</pubDate>
      <guid>https://www.servicenow.com/community/community-central-forum/script/m-p/3231009#M2693</guid>
      <dc:creator>AnanyaT</dc:creator>
      <dc:date>2025-04-08T05:30:42Z</dc:date>
    </item>
  </channel>
</rss>

