You need to sign in to do that
Don't have an account?
Wachiva
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
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
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
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
Thank you for yor solution however I am struggling to understand how to fit this solution into the existing code.
Kind Regards
Andrew
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);}
}}}}