-1

I have called Apex class 2 from Apex class1. During creating a new Account in Salesforce getting an error.Please anyone help on this issue.

Errore details

Error: Invalid Data. Review all error messages below to correct your data. Apex trigger AccountTrigger caused an unexpected exception, contact your administrator: AccountTrigger: execution of AfterInsert caused by: System.FinalException: Record is read-only: ()

Apex Class 1:

public class INTLAccountTriggerService{

public static void afterInsert(List<Account> triggerNew, Map<Id, Account> triggerNewMap)
{

    ZIP_alignment_account test = new ZIP_alignment_account();
    test.assignTerritory(triggerNew);
 }
}

Apex Class 2:

public class ZIP_alignment_account {

public void assignTerritory(List<Account> accList){
    
        for (Account a : accList){
        system.debug('Accountfromtrigger Id-->'+a.id);
        system.debug('Accountfromtrigger Name-->'+a.name);
    }
    List<ZTS_AccountTerritory__c> lstAT2Insert = new List<ZTS_AccountTerritory__c>();
    Set<String> zipcodes=new Set<String>(); 
    Set<String> marketSet = new Set<String>();
    Set<String> zipMarketKey =new Set<String>();
    List<Account> lstBricks = new List<Account>();
    
    for(Account acc:accList){
        if(!String.isBlank(acc.ZTS_EU_Zip_Postal_Code__c) && acc.ZTS_EU_Market__c != ZTS_EU_GlobalConstants.MARKET_VAL && acc.ZTS_EU_Market__c != ZTS_EU_GlobalConstants.UK_MARKET_VAL){
            zipcodes.add(acc.ZTS_EU_Zip_Postal_Code__c);
        }else if(acc.ZTS_EU_Market__c == ZTS_EU_GlobalConstants.MARKET_VAL){
            zipcodes.add(acc.ZTS_EU_State_County_Value__c);
        }else if(!String.isBlank(acc.ZTS_EU_Zip_Postal_Code__c) && acc.ZTS_EU_Market__c == ZTS_EU_GlobalConstants.UK_MARKET_VAL){
            zipcodes.add(acc.ZTS_EU_UK_PC4_from_Postalcode__c);
        }
        marketSet.add(acc.ZTS_EU_Market__c);
    }
    
    if(!zipcodes.isEmpty()){
        
        //Query ziptobrick 
        List<ZTS_GL_ZipToBrick__c> ziptoBrickList = [SELECT Name,ZTS_EU_ZipCode__c,ZTS_EU_BrickCode__c,ZTS_EU_Market__c,ZTS_EU_ZipPostalCode__c,ZTS_EU_Inactive__c,ZTS_EU_Query_Ext_ID__c,ZTS_EU_BrickCode__r.Name,ZTS_EU_BrickCode__r.ZTS_EU_Inactive__c,ZTS_EU_BrickCode__r.ZTS_EU_Territory__r.name 
                                                        FROM ZTS_GL_ZipToBrick__c 
                                                        WHERE ZTS_EU_Query_Ext_ID__c IN :zipcodes 
                                                        AND Name != '' 
                                                        AND ZTS_EU_BrickCode__r.ZTS_EU_Inactive__c = false 
                                                        AND ZTS_EU_Inactive__c = false 
                                                        AND ZTS_EU_Market__c IN : marketSet];
        //Code By Shefali
        for(ZTS_GL_ZipToBrick__c zpToBk :ziptoBrickList){ 
             zipMarketKey.add(zpToBk.ZTS_EU_Query_Ext_ID__c+';'+ zpToBk.ZTS_EU_Market__c);
        }
        
        List<ZTS_GL_ZipToBrick__c> zipList;
        Map<String,List<ZTS_GL_ZipToBrick__c>> zipToBrickMap = new Map<String,List<ZTS_GL_ZipToBrick__c>>();
        
        for(ZTS_GL_ZipToBrick__c zipToBrick : ziptoBrickList){       
            if(zipToBrickMap.containsKey(zipToBrick.ZTS_EU_Query_Ext_ID__c+';'+zipToBrick.ZTS_EU_Market__c)){
                zipToBrickMap.get(zipToBrick.ZTS_EU_Query_Ext_ID__c+';'+zipToBrick.ZTS_EU_Market__c).add(zipToBrick);                    
            }else{
                zipList = new List<ZTS_GL_ZipToBrick__c>();
                zipList.add(zipToBrick);
                zipToBrickMap.put(zipToBrick.ZTS_EU_Query_Ext_ID__c+';'+zipToBrick.ZTS_EU_Market__c,zipList);
            }
        }
    
        
        for(Account acc:accList){
           String zipcode = (acc.ZTS_EU_Market__c == ZTS_EU_GlobalConstants.MARKET_VAL)?(acc.ZTS_EU_State_County_Value__c != null ? acc.ZTS_EU_State_County_Value__c : ''):
                                                (acc.ZTS_EU_Market__c==ZTS_EU_GlobalConstants.UK_MARKET_VAL ? (acc.ZTS_EU_UK_PC4_from_Postalcode__c != '' ? acc.ZTS_EU_UK_PC4_from_Postalcode__c : ''):
                                                (acc.ZTS_EU_Zip_Postal_Code__c != null ? acc.ZTS_EU_Zip_Postal_Code__c:''));
                                                
            if(!String.isBlank(zipcode) && zipToBrickMap.containsKey(zipcode+';'+acc.ZTS_EU_Market__c)){
                List<ZTS_GL_ZipToBrick__c> zipBrickList = zipToBrickMap.get(zipcode+';'+acc.ZTS_EU_Market__c);

                for(ZTS_GL_ZipToBrick__c z : zipBrickList){
                  acc.ZTS_EU_BrickCode__c = z.ZTS_EU_BrickCode__r.Name;
                 lstBricks.add(acc);

     }
   }
          if(lstBricks.size()>0){
              
                insert lstBricks;
                System.debug('Insert Successful');
          }     
 }
}

} }

Thanks, Raghu

1
  • the assignment starting with acc.ZTS_EU_BrickCode__c is causing the error because in AfterInsert, u cannot update field values on same record that fired the trigger. u need to assign values in beforeInsert. But then its not clear what exactly you are trying to achieve. Why inserting the list lstBricks which is basically the same accounts that were fired in the insert trigger?
    – Shamina
    Jun 29, 2021 at 5:30

1 Answer 1

0

In after trigger the instance of trigger.new is in readonly mode soacc.ZTS_EU_BrickCode__c = z.ZTS_EU_BrickCode__r.Name is creating the issue. Here you are trying to update the trigge.new instance which is only possible in before trigger not in after trigger.

        for(Account acc:accList){
           account accObjTemp = new account();
           accObjTemp = acc;
           String zipcode = (acc.ZTS_EU_Market__c == ZTS_EU_GlobalConstants.MARKET_VAL)?(acc.ZTS_EU_State_County_Value__c != null ? acc.ZTS_EU_State_County_Value__c : ''):
                                                (acc.ZTS_EU_Market__c==ZTS_EU_GlobalConstants.UK_MARKET_VAL ? (acc.ZTS_EU_UK_PC4_from_Postalcode__c != '' ? acc.ZTS_EU_UK_PC4_from_Postalcode__c : ''):
                                                (acc.ZTS_EU_Zip_Postal_Code__c != null ? acc.ZTS_EU_Zip_Postal_Code__c:''));
                                                
            if(!String.isBlank(zipcode) && zipToBrickMap.containsKey(zipcode+';'+acc.ZTS_EU_Market__c)){
                List<ZTS_GL_ZipToBrick__c> zipBrickList = zipToBrickMap.get(zipcode+';'+acc.ZTS_EU_Market__c);

                for(ZTS_GL_ZipToBrick__c z : zipBrickList){
                  accObjTemp.ZTS_EU_BrickCode__c = z.ZTS_EU_BrickCode__r.Name;
                 lstBricks.add(accObjTemp);

     }
   }
          if(lstBricks.size()>0){
              
                insert lstBricks;
                System.debug('Insert Successful');
          }     
 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.