function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
WachivaWachiva 

Exclude User roles from trigger

Hi

I have written my first trigger and and having some trouble extending it... at present it works fine allocation the correct user to a record based on postal code using a custom object.

My need is to exclude records bing created by certain user roles being affected by this trigger and having experimented with how to do this i have become stuck, any help would be greatly appreciated.

At pressent the trigger is as below

trigger Assignaccounts on Account (After insert, Before Update) {

List <account> accountstoupdate = new list<account>();
for (account newaccount : Trigger.new)

IF (newAccount.Key_Account__c != True)
if (newAccount != null)
if (newaccount.BillingPostalCode != null) 
If (account.BillingPostalCode != NULL)

{List<postcode__c> Postcodes = [ SELECT User__c FROM postcode__c WHERE name LIKE :newaccount.BillingPostalCode.SubString(0,2) + '%' limit 1];
if (postcodes.size()>0)
{newaccount.OwnerId = postcodes[0].User__c;

accountstoupdate.add(newaccount);
}}} 

Thankyou

Andrew
Best Answer chosen by Wachiva
Suneel#8Suneel#8
Andrew,

UserInfo.getUserRoleId() will fetch you current logged in user role id. And with below SOQL you will be able to identify if the current user is part of exempted roles by checking the size of collection-IsItExemptedRole.size()>0.If size is >0 then current user role is the exempted role

List<UserRole> IsItExemptedRole=[select id,name from UserRole where name in :exemptedRoles and id=:UserInfo.getUserRoleId()];

if(IsItExemptedRole.size()>0){
}

Kindly mark this question as Solved if your issue is fixed

All Answers

WachivaWachiva
Trigger is before insert not after
Suneel#8Suneel#8
Andrew,

UserInfo.getUserRoleId() will fetch you current logged in user role id. And with below SOQL you will be able to identify if the current user is part of exempted roles by checking the size of collection-IsItExemptedRole.size()>0.If size is >0 then current user role is the exempted role

List<UserRole> IsItExemptedRole=[select id,name from UserRole where name in :exemptedRoles and id=:UserInfo.getUserRoleId()];

if(IsItExemptedRole.size()>0){
}

Kindly mark this question as Solved if your issue is fixed
This was selected as the best answer
WachivaWachiva
Hi Suneel#8

Thank you for yor solution however I am struggling to understand how to fit this solution into the existing code.

Kind Regards

Andrew
WachivaWachiva
Hi 

I think i have come up with a solution that works could you let me know if i have made an error and i will mark your solution and resolved thank you for your help

trigger Assignaccounts on Account (before insert, Before Update) {

List <account> accountstoupdate = new list<account>();
for (account newaccount : Trigger.new)

IF (newAccount.Key_Account__c != True)
if (newAccount != null)
if (newaccount.BillingPostalCode != null) 
If (account.BillingPostalCode != NULL)

{UserInfo.getUserRoleId(); 

List<String> exemptedRoles = new List<String>();
exemptedRoles.add('Wedding & Social');

list<UserRole> IsItExemptedRole=[select id,name from UserRole where name in :exemptedRoles and id=:UserInfo.getUserRoleId()];

if(IsItExemptedRole.size()<=0){

{List<postcode__c> Postcodes = [ SELECT User__c FROM postcode__c WHERE name LIKE :newaccount.BillingPostalCode.SubString(0,2) + '%' limit 1];
if (postcodes.size()>0)
{newaccount.OwnerId = postcodes[0].User__c;

accountstoupdate.add(newaccount);}




}}}}
Suneel#8Suneel#8
Yes your code looks good