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 anbody help me to code this

I created a checkbox field call active on opportunity . If a user other than system administrator tries to update the field to inactive when the stage field value is not closed/won show an error 'You do not have the access to perform this operation. Kindly contact your system administrator'
Best Answer chosen by Vidya H 4
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Vidya,

Can you try the below code modified a bit.
 
trigger OppyValidation 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.Call_Active__c ==false && oldoppy.Call_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.Call_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');
        }
    }
    }
}

If this solution helps, Please mark it as best answer.


Thanks,

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Vidya,

Can you confirm if you are okay with validation rule or you need ony with trigger logic.

Thanks,
 
Vidya H 4Vidya H 4
Need to write Trigger
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Vidya,

Can you try the below validation rule.
 
AND( ISCHANGED( Call_Active__c ), NOT(Call_Active__c),not( ISPICKVAL( StageName ,'Closed Won')), $Profile.Name !='System Administrator' )

If this solution helps, Please mark it as best answer.

Thanks,
 
Vidya H 4Vidya H 4
@Sai Praveen, 
need to do with trigger.
 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Vidya,

Can you try the below trigger code.
 
trigger OppyValidation on Opportunity (before insert,before update) {
    
    Id profileId= userinfo.getProfileId();
String profileName=[Select Id,Name from Profile where Id=:profileId].Name;

    for(Opportunity opp:Trigger.new){
        opportunity oldoppy= Trigger.oldmap.get(opp.id);
        
        if(opp.Call_Active__c ==false && oldoppy.Call_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 this solution helps, Please mark it as best answer.


Thanks,
 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Vidya,

Did the above soloution worked? If so please mark it as best answer.

Thanks,
 
Vidya H 4Vidya H 4
@Sai Praveen  while saving opportunity record im getting error like this
OppValidation: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.OppValidation: line 7, column 1
Vidya H 4Vidya H 4
@Sai Praveen
please help to solve that error
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Vidya,

Can you try the below code modified a bit.
 
trigger OppyValidation 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.Call_Active__c ==false && oldoppy.Call_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.Call_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');
        }
    }
    }
}

If this solution helps, Please mark it as best answer.


Thanks,
This was selected as the best answer
Vidya H 4Vidya H 4
@Sai Praveen 
its working fine 
thank you