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
BrendBrend 

How to solve this trigger issue

Trigger is working for all profiles instead of only this profile id. Please how do I correct this

trigger accountdelet on Acount (before delete)
 {
String ProfileId = UserInfo.getProfileId(); 
for (Account a : Trigger.old)     
           
IF(ProfileId!='00721100046VB5L')
{
     a.addError('Not permitted');
    
            }
        }
Avinash BarolaAvinash Barola
Hi,
I think UserInfo.getProfileId() is giving 18 digit profile id, so for IF(ProfileId!='00721100046VB5L') this condition either you should use contains method or should query the profile id basis on profile name instead of hardcoded id.

Thanks
BrendBrend
Hi Avinash, thank you for responding. I am still a beginner in apex programming. I would have use profile name but I just want only certain users restricted. That is why i am using profile id. Your directives will be much appreciated. How best do you think I should write this instead of hardcoding
BrendBrend
Also why is it working for all profile and not just this profile id
Avinash BarolaAvinash Barola
You can user below code : 

trigger accountdelet on Acount (before delete)
 {
String ProfileId = UserInfo.getProfileId(); 
for (Account a : Trigger.old)     
           
IF(ProfileId != [select id from profile where id = '00721100046VB5L' LIMIT 1])
{
     a.addError('Not permitted');
    
            }
        }
I think this will work correctly.
BrendBrend
I am getting this error: Compile Error: Comparison arguments must be compatible types: String, List<Profile> at line 6 column 4
I even tried and converted the profile id to 18 digits and it is still givng same error
 
Avinash BarolaAvinash Barola
sorry for above code : 
use below one : 

trigger accountdelet on Account (before delete) {
String ProfileId = UserInfo.getProfileId(); 
for (Account a : Trigger.old)     
           
IF(ProfileId != [select id from profile where id = '00721100046VB5L' LIMIT 1].Id) {
     a.addError('Not permitted');
 }
 }
Ajay K DubediAjay K Dubedi
Hi Uzee,

Below code can fulfill your requirements. Hope this will work for you.
trigger accountdelet on Account (before delete)
{
String ProfileId = [SELECT id,Name FROM Profile WHERE Id =:UserInfo.getProfileId()].Id ;
for (Account a : Trigger.old)

IF(ProfileId =='00721100046VB5L')
{
a.addError('Not permitted');
}
}
Please mark this as best answer if this solves your problem.

Thank you
Ajay Dubedi