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
Taha Khan 7Taha Khan 7 

convert validation rule to trigger

Hi All,
I am very new to development and aI have to change one validation rule on Opportunity into trigger using custom settings.
For example:Validation rule on an opportunity has some profiles, some roles and stage 
something like below-
AND(
ISPICKVAL(StageName ,"Pending"), 
ISPICKVAL(StageName ,"Closed") , 
NOT($Permission.Bypass_Rule_Exclusion), 
$Profile.Name <> " Contractor", 
$UserRole.Name <> "Analyst")

and I need to change this validation rule into trigger.
I can move this hardcoded values to custom settings,but I am not sure how to write a trigger .
Could someone please help me.
Best Answer chosen by Taha Khan 7
Xeon ArianXeon Arian
Hi Taha,

Asssuming you want the validation to fire on both Update and Insert you can use the below code. I have modified the code to meet your requirements. Please select this as best answer if the solution works for you.
trigger Opptrigger on Opportunity (after insert, after update) {

List<User> cUser = [SELECT UserRole.Name,Profile.Name from User];
List<PermissionSet> PermList = [SELECT Label FROM PermissionSet];
 Set<Id> setofOppId = new Set<Id>();
        
for(Opportunity objOpp: trigger.new){
               
 setofOppId.add(objOpp.Id);
       
 }

List<Opportunity> OppList = [Select Stagename from Opportunity where id IN :setofOppId]

for(Opportunity Data : OppList){
 for(User Udata : cUser){
for(PermissionSet Pdata : PermList){
                   if((StageName == 'Pending' || StageName == 'Closed') && Udata. UserRole.Name != 'Contractor' && Udata.Profile.Name != 'Analyst'  && PermList.Label != 'Bypass Rule Exclusion' )
{
                      Data.addError('Add your error message here');
}
}
}
}
}

 

All Answers

Xeon ArianXeon Arian
Hi Taha,

Asssuming you want the validation to fire on both Update and Insert you can use the below code. I have modified the code to meet your requirements. Please select this as best answer if the solution works for you.
trigger Opptrigger on Opportunity (after insert, after update) {

List<User> cUser = [SELECT UserRole.Name,Profile.Name from User];
List<PermissionSet> PermList = [SELECT Label FROM PermissionSet];
 Set<Id> setofOppId = new Set<Id>();
        
for(Opportunity objOpp: trigger.new){
               
 setofOppId.add(objOpp.Id);
       
 }

List<Opportunity> OppList = [Select Stagename from Opportunity where id IN :setofOppId]

for(Opportunity Data : OppList){
 for(User Udata : cUser){
for(PermissionSet Pdata : PermList){
                   if((StageName == 'Pending' || StageName == 'Closed') && Udata. UserRole.Name != 'Contractor' && Udata.Profile.Name != 'Analyst'  && PermList.Label != 'Bypass Rule Exclusion' )
{
                      Data.addError('Add your error message here');
}
}
}
}
}

 
This was selected as the best answer
Taha Khan 7Taha Khan 7
Awesome Xeon Arian,
Thanks for all your help.
Could you please let me know how to write test class for this trigger?
This is my first test class and Trigger.
Appreciate all your help