+ Start a Discussion
Vinod TomarVinod Tomar 

Record Update Access Verification

Below is the function for edit access-


public static void verifyEditAuthority(Opportunity[] opportunityListNew,Opportunity[] opportunityListOld) {
        ClosedOpptyUserAccess__c oppEditAccess = ClosedOpptyUserAccess__c.getInstance();
        string oldStageName ='';       
        //Retreiving old stage value
        oldStageName = opportunityListOld[0].StageName;
        System.debug('oldStageName:  ' + oldStageName );
        for(Opportunity myOpp : opportunityListNew) {
            if (myOpp.StageName == 'Closed Lost' && !oppEditAccess .Can_Edit_Opportunity__c || !oppEditAccess .Can_Edit_Opportunity__c && oldStageName  == 'Closed Lost') {
                myOpp.addError('You do not have access to edit this opportunity because it is in a Closed Lost stage. Please contact the system administrator.');


In this code I am trying to stop editing the opportunity for some profiles. I am getting value of new stage through loop. but oldstage value for one record only. 


Now qurestion is would this work if we update multiple records?    


Thats because of this: oldStageName = opportunityListOld[0].StageName;


Try using map to overcome this problem. I am not very sure about this, but worth a try:


Create 2 map variables, mapNew and mapOld and put the old and new opportunities with ids. And check.