<?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 Re: Add/ Remove a specific role from user profile when user is Added/removed from a particular field in Developer forum</title>
    <link>https://www.servicenow.com/community/developer-forum/add-remove-a-specific-role-from-user-profile-when-user-is-added/m-p/2656589#M1030818</link>
    <description>&lt;P&gt;&lt;a href="https://www.servicenow.com/community/user/viewprofilepage/user-id/597036"&gt;@Puneet4418&lt;/a&gt;&amp;nbsp;In your business rule, can you update as follows.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;if(current.u_owner!=previous.u_owner){
    var userRole = new GlideAggregate('sys_user_has_role');
    userRole.addQuery('user',current.u_owner);//replace with your 
    userRole.addQuery('role','b2d8f7130a0a0baa5bf52498ecaadeb4');//Add your role sys_id here
    userRole.addAggregate('COUNT');
    userRole.query();
    if(userRole.next()){
    var roleCount=0;
    roleCount= userRole.getAggregate('COUNT');
    gs.info(roleCount);
        if(roleCount==0){//Only add role if the user doesn't have it
            var glideRole = new GlideRecord('sys_user_has_role');
            glideRole.initialize();
            glideRole.user=current.u_owner;
            glideRole.role='b2d8f7130a0a0baa5bf52498ecaadeb4'; //replace with your role sys_id
            glideRole.state='active';
            glideRole.insert();

            if(previous.u_owner!=''){//Code to remove the role from previous owner.
                var removeRole = new GlideRecord('sys_user_has_role');
                removeRole.addQuery('user',previous.u_owner);
                removeRole.addQuery('role','b2d8f7130a0a0baa5bf52498ecaadeb4');//Replace with you role sys_id
               gs.info('Previous owner '+previous.u_owner);
                if(removeRole.next()){
                 gs.info('Deleting the record '+ removeRole.getValue('sys_id'));
                    removeRole.deleteRecord();
                }
            }

        }
    }
}
&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you check if both the info logs are printed and what values do they print.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 29 Aug 2023 15:25:42 GMT</pubDate>
    <dc:creator>Sandeep Rajput</dc:creator>
    <dc:date>2023-08-29T15:25:42Z</dc:date>
    <item>
      <title>Add/ Remove a specific role from user profile when user is Added/removed from a particular field.</title>
      <link>https://www.servicenow.com/community/developer-forum/add-remove-a-specific-role-from-user-profile-when-user-is-added/m-p/2653754#M1029895</link>
      <description>&lt;P&gt;Hello Developers,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can someone help me with below requirement.&lt;/P&gt;&lt;P&gt;There is a field "Owner" in a custom application form. whenever any user added to that field, a specific role should be automatically added to the user's profile and vice versa when removed from the field. How can we achieve this?&lt;/P&gt;</description>
      <pubDate>Fri, 25 Aug 2023 16:18:54 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/add-remove-a-specific-role-from-user-profile-when-user-is-added/m-p/2653754#M1029895</guid>
      <dc:creator>Puneet4418</dc:creator>
      <dc:date>2023-08-25T16:18:54Z</dc:date>
    </item>
    <item>
      <title>Re: Add/ Remove a specific role from user profile when user is Added/removed from a particular field</title>
      <link>https://www.servicenow.com/community/developer-forum/add-remove-a-specific-role-from-user-profile-when-user-is-added/m-p/2653825#M1029930</link>
      <description>&lt;P&gt;&lt;a href="https://www.servicenow.com/community/user/viewprofilepage/user-id/597036"&gt;@Puneet4418&lt;/a&gt;&amp;nbsp;Create an insert/update business rule on your custom table with condition owner changes. Inside the business rule script, add the following code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;if(current.u_owner!=previous.u_owner){
    var userRole = new GlideAggregate('sys_user_has_role');
    userRole.addQuery('user',current.u_owner);//replace with your 
    userRole.addQuery('role','b2d8f7130a0a0baa5bf52498ecaadeb4');//Add your role sys_id here
    userRole.addAggregate('COUNT');
    userRole.query();
    if(userRole.next()){
    var roleCount=0;
    roleCount= userRole.getAggregate('COUNT');
    gs.print(roleCount);
        if(roleCount==0){//Only add role if the user doesn't have it
            var glideRole = new GlideRecord('sys_user_has_role');
            glideRole.initialize();
            glideRole.user=current.u_owner;
            glideRole.role='b2d8f7130a0a0baa5bf52498ecaadeb4'; //replace with your role sys_id
            glideRole.state='active';
            glideRole.insert();

            if(previous.u_owner!=''){//Code to remove the role from previous owner.
                var removeRole = new GlideRecord('sys_user_has_role');
                removeRole.addQuery('user',previous.u_owner);
                removeRole.addQuery('role','b2d8f7130a0a0baa5bf52498ecaadeb4');//Replace with you role sys_id
                if(removeRole.next()){
                    removeRole.deleteRecord();
                }
            }

        }
    }
}

&lt;/LI-CODE&gt;
&lt;P&gt;Hope this helps.&lt;/P&gt;</description>
      <pubDate>Fri, 25 Aug 2023 17:39:19 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/add-remove-a-specific-role-from-user-profile-when-user-is-added/m-p/2653825#M1029930</guid>
      <dc:creator>Sandeep Rajput</dc:creator>
      <dc:date>2023-08-25T17:39:19Z</dc:date>
    </item>
    <item>
      <title>Re: Add/ Remove a specific role from user profile when user is Added/removed from a particular field</title>
      <link>https://www.servicenow.com/community/developer-forum/add-remove-a-specific-role-from-user-profile-when-user-is-added/m-p/2653854#M1029942</link>
      <description>&lt;P&gt;Hi Pankaj,&lt;/P&gt;
&lt;P&gt;Can you please share the need for such use case.&lt;/P&gt;</description>
      <pubDate>Fri, 25 Aug 2023 18:16:28 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/add-remove-a-specific-role-from-user-profile-when-user-is-added/m-p/2653854#M1029942</guid>
      <dc:creator>Jaspal Singh</dc:creator>
      <dc:date>2023-08-25T18:16:28Z</dc:date>
    </item>
    <item>
      <title>Re: Add/ Remove a specific role from user profile when user is Added/removed from a particular field</title>
      <link>https://www.servicenow.com/community/developer-forum/add-remove-a-specific-role-from-user-profile-when-user-is-added/m-p/2656475#M1030785</link>
      <description>&lt;P&gt;&lt;a href="https://www.servicenow.com/community/user/viewprofilepage/user-id/145583"&gt;@Sandeep Rajput&lt;/a&gt;&amp;nbsp;Thanks for your help.&lt;/P&gt;&lt;P&gt;The role addition part is working fine. However, when I am removing user from the owner field, it is not removing that particular role from user's profile.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Aug 2023 14:01:17 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/add-remove-a-specific-role-from-user-profile-when-user-is-added/m-p/2656475#M1030785</guid>
      <dc:creator>Puneet4418</dc:creator>
      <dc:date>2023-08-29T14:01:17Z</dc:date>
    </item>
    <item>
      <title>Re: Add/ Remove a specific role from user profile when user is Added/removed from a particular field</title>
      <link>https://www.servicenow.com/community/developer-forum/add-remove-a-specific-role-from-user-profile-when-user-is-added/m-p/2656478#M1030786</link>
      <description>&lt;P&gt;&lt;a href="https://www.servicenow.com/community/user/viewprofilepage/user-id/257473"&gt;@Jaspal Singh&lt;/a&gt;&amp;nbsp; Its our client's requirement.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Aug 2023 14:02:45 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/add-remove-a-specific-role-from-user-profile-when-user-is-added/m-p/2656478#M1030786</guid>
      <dc:creator>Puneet4418</dc:creator>
      <dc:date>2023-08-29T14:02:45Z</dc:date>
    </item>
    <item>
      <title>Re: Add/ Remove a specific role from user profile when user is Added/removed from a particular field</title>
      <link>https://www.servicenow.com/community/developer-forum/add-remove-a-specific-role-from-user-profile-when-user-is-added/m-p/2656589#M1030818</link>
      <description>&lt;P&gt;&lt;a href="https://www.servicenow.com/community/user/viewprofilepage/user-id/597036"&gt;@Puneet4418&lt;/a&gt;&amp;nbsp;In your business rule, can you update as follows.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;if(current.u_owner!=previous.u_owner){
    var userRole = new GlideAggregate('sys_user_has_role');
    userRole.addQuery('user',current.u_owner);//replace with your 
    userRole.addQuery('role','b2d8f7130a0a0baa5bf52498ecaadeb4');//Add your role sys_id here
    userRole.addAggregate('COUNT');
    userRole.query();
    if(userRole.next()){
    var roleCount=0;
    roleCount= userRole.getAggregate('COUNT');
    gs.info(roleCount);
        if(roleCount==0){//Only add role if the user doesn't have it
            var glideRole = new GlideRecord('sys_user_has_role');
            glideRole.initialize();
            glideRole.user=current.u_owner;
            glideRole.role='b2d8f7130a0a0baa5bf52498ecaadeb4'; //replace with your role sys_id
            glideRole.state='active';
            glideRole.insert();

            if(previous.u_owner!=''){//Code to remove the role from previous owner.
                var removeRole = new GlideRecord('sys_user_has_role');
                removeRole.addQuery('user',previous.u_owner);
                removeRole.addQuery('role','b2d8f7130a0a0baa5bf52498ecaadeb4');//Replace with you role sys_id
               gs.info('Previous owner '+previous.u_owner);
                if(removeRole.next()){
                 gs.info('Deleting the record '+ removeRole.getValue('sys_id'));
                    removeRole.deleteRecord();
                }
            }

        }
    }
}
&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you check if both the info logs are printed and what values do they print.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Aug 2023 15:25:42 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/add-remove-a-specific-role-from-user-profile-when-user-is-added/m-p/2656589#M1030818</guid>
      <dc:creator>Sandeep Rajput</dc:creator>
      <dc:date>2023-08-29T15:25:42Z</dc:date>
    </item>
    <item>
      <title>Re: Add/ Remove a specific role from user profile when user is Added/removed from a particular field</title>
      <link>https://www.servicenow.com/community/developer-forum/add-remove-a-specific-role-from-user-profile-when-user-is-added/m-p/2656787#M1030869</link>
      <description>&lt;P&gt;&lt;a href="https://www.servicenow.com/community/user/viewprofilepage/user-id/145583"&gt;@Sandeep Rajput&lt;/a&gt;&amp;nbsp;tried but no info logs are printed.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Aug 2023 18:14:07 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/add-remove-a-specific-role-from-user-profile-when-user-is-added/m-p/2656787#M1030869</guid>
      <dc:creator>Puneet4418</dc:creator>
      <dc:date>2023-08-29T18:14:07Z</dc:date>
    </item>
    <item>
      <title>Re: Add/ Remove a specific role from user profile when user is Added/removed from a particular field</title>
      <link>https://www.servicenow.com/community/developer-forum/add-remove-a-specific-role-from-user-profile-when-user-is-added/m-p/2656814#M1030874</link>
      <description>&lt;P&gt;&lt;a href="https://www.servicenow.com/community/user/viewprofilepage/user-id/597036"&gt;@Puneet4418&lt;/a&gt;&amp;nbsp;Can you share the screenshots of your business rule, please include all sections 'When to run, action and advanced in your screenshots&lt;/P&gt;</description>
      <pubDate>Tue, 29 Aug 2023 18:48:45 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/add-remove-a-specific-role-from-user-profile-when-user-is-added/m-p/2656814#M1030874</guid>
      <dc:creator>Sandeep Rajput</dc:creator>
      <dc:date>2023-08-29T18:48:45Z</dc:date>
    </item>
    <item>
      <title>Re: Add/ Remove a specific role from user profile when user is Added/removed from a particular field</title>
      <link>https://www.servicenow.com/community/developer-forum/add-remove-a-specific-role-from-user-profile-when-user-is-added/m-p/2657786#M1031172</link>
      <description>&lt;P&gt;&lt;a href="https://www.servicenow.com/community/user/viewprofilepage/user-id/145583"&gt;@Sandeep Rajput&lt;/a&gt;I found the issue. Now its working fine.&lt;/P&gt;</description>
      <pubDate>Wed, 30 Aug 2023 15:44:31 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/add-remove-a-specific-role-from-user-profile-when-user-is-added/m-p/2657786#M1031172</guid>
      <dc:creator>Puneet4418</dc:creator>
      <dc:date>2023-08-30T15:44:31Z</dc:date>
    </item>
    <item>
      <title>Re: Add/ Remove a specific role from user profile when user is Added/removed from a particular field</title>
      <link>https://www.servicenow.com/community/developer-forum/add-remove-a-specific-role-from-user-profile-when-user-is-added/m-p/2743781#M1057557</link>
      <description>&lt;P&gt;&lt;a href="https://www.servicenow.com/community/user/viewprofilepage/user-id/145583"&gt;@Sandeep Rajput&lt;/a&gt;&amp;nbsp;I have been looking at this as I have the same requirement. The problem i'm getting is that when I add a user to my custom field, the role is added to the profile. When you then add an additional user after the first user it then gives a unique key violation and removes the role from the user still in the custom field. Any idea why this is?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 26 Nov 2023 18:59:30 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/add-remove-a-specific-role-from-user-profile-when-user-is-added/m-p/2743781#M1057557</guid>
      <dc:creator>Luke James</dc:creator>
      <dc:date>2023-11-26T18:59:30Z</dc:date>
    </item>
    <item>
      <title>Re: Add/ Remove a specific role from user profile when user is Added/removed from a particular field</title>
      <link>https://www.servicenow.com/community/developer-forum/add-remove-a-specific-role-from-user-profile-when-user-is-added/m-p/2743912#M1057589</link>
      <description>&lt;P&gt;&lt;a href="https://www.servicenow.com/community/user/viewprofilepage/user-id/28405"&gt;@Luke James&lt;/a&gt;&amp;nbsp;Could you please post a separate question with screenshot of your business rule and form where you are facing this issue.&lt;/P&gt;</description>
      <pubDate>Mon, 27 Nov 2023 04:03:15 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/add-remove-a-specific-role-from-user-profile-when-user-is-added/m-p/2743912#M1057589</guid>
      <dc:creator>Sandeep Rajput</dc:creator>
      <dc:date>2023-11-27T04:03:15Z</dc:date>
    </item>
  </channel>
</rss>

