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
Vidya H 4Vidya H 4 

hi, anybody can help me for this?

Please anbody help me to write handler class??

trigger OppValidation on Opportunity (before insert,before update) {
    
    Id profileId= userinfo.getProfileId();
    String profileName=[Select Id,Name from Profile where Id=:profileId].Name;
    if(Trigger.isupdate){
        for(Opportunity opp:Trigger.new){
            opportunity oldoppy= Trigger.oldmap.get(opp.id);
            
            if(opp.Active__c ==false && oldoppy.Active__c==True && opp.stagename!='Closed Won' && profileName!='System Administrator'){
                opp.adderror('You do not have the access to perform this operation. Kindly contact your system administrator');
            }
        }
        
    }
    
    if(Trigger.isinsert){
        for(Opportunity opp:Trigger.new){
            
            if(opp.Active__c ==false && opp.stagename!='Closed Won' && profileName!='System Administrator'){
                opp.adderror('You do not have the access to perform this operation. Kindly contact your system administrator');
            }
        }
    }
}
CharuDuttCharuDutt
Hii Vidya
Try Below Code
trigger OppValidation on Opportunity (before insert,before update) {
    if(Trigger.isInsert || Trigger.isUpdate){
        OppValidationHelper.method(trigger.new,trigger.newMap,trigger.OldMap,Trigger.isInsert,Trigger.isUpdate);
    }
}


Helper Class:
public class OppValidationHelper{
    public static void method(list<Opportunity> lstop,map<Id,Opportunity>newmap,map<Id,Opportunity>oldmap,boolean IsInsert,Boolean IsUpdate){
        Id profileId= userinfo.getProfileId();
        String profileName=[Select Id,Name from Profile where Id=:profileId].Name;
        for(Opportunity opp : lstop){
            if(IsInsert){
                if(opp.Active__c == false && opp.stagename != 'Closed Won' && profileName!='System Administrator' && oldmap == null){
                    opp.adderror('You do not have the access to perform this operation. Kindly contact your system administrator');
                }
            }
            if(Isupdate){
                 if(opp.Active__c == false && newmap.get(opp.id).Active__c != Oldmap.get(opp.id).Active__c && opp.stagename!='Closed Won' && profileName!='System Administrator'){
                opp.adderror('You do not have the access to perform this operation. Kindly contact your system administrator');
            }
            }
        }
    }
    
}
Please Mark It As Best Answer If It Helps
Thank You!