• Chinna99
  • NEWBIE
  • 0 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 1
    Replies
HI.. I need quick help ....

Error Message :

Review all error messages below to correct your data.
Apex trigger OpportunityTrigger caused an unexpected exception, contact your administrator: OpportunityTrigger: execution of AfterInsert caused by: System.FinalException: Record is read-only: Class.OpportunityTriggerHandler.OnBeforeInsert: line 47, column 1


Apex trigger :
trigger OpportunityTrigger on Opportunity (After Insert,After update) {

OpportunityTriggerHandler.onAfterInsertUpdate(trigger.newmap,trigger.oldmap);
OpportunityTriggerHandler.onBeforeInsert(trigger.new);
}

Apex class :
public with sharing class OpportunityTriggerHandler {

    public OpportunityTriggerHandler(){
    
    }
    
    public static void onAfterInsertUpdate(map<ID,Opportunity> newmap,map<ID,Opportunity> oldmap)
    {
        set<ID> accIDs = new set<ID>();
        for(Opportunity oppObj : newmap.values()){
            if(oppObj.AccountID != null){
                accIDs.add(oppObj.AccountID);
            }
        }
        map<ID,Account> accountMap = new map<ID,Account>([SELECT ID,OwnerID FROM Account WHERE ID IN: accIDs]);
        map<ID,String> oppTeam = new map<ID,String>();
        map<ID,String> oppTeamRole = new map<ID,String>(); 
                  
       for(Opportunity oppObj2 : newmap.values()){
            if(oppObj2.AccountID != null && accountMap.containsKey(oppObj2.AccountID) && accountMap.get(oppObj2.AccountID).OwnerID != oppObj2.OwnerID){
                    oppTeam.put(accountMap.get(oppObj2.AccountID).OwnerID,oppObj2.ID);
                    oppTeamRole.put(oppObj2.ID,'Account Manager'); 
            }
        }                   
        addAccountTeamMember.addOpportunityTeamMembers(oppTeam,oppTeamRole); 
                  
    }
    
       
    public static void OnBeforeInsert(List<Opportunity> newRecords){
     set<ID> accIDs = new set<ID>();
     set<ID> conIDs = new set<ID>();     
        for(Opportunity oppObj : newRecords){
            if(oppObj.AccountID != null){
                accIDs.add(oppObj.AccountID);
            }
            if(oppObj.Primary_Contact__c != null){
                conIDs.add(oppObj.Primary_Contact__c);
            }                                    
        }
        map<ID,Account> accountMap = new map<ID,Account>([SELECT ID,OwnerID FROM Account WHERE ID IN: accIDs]);
        map<ID,Contact> contactMap = new map<ID,Contact>([SELECT ID,OwnerID FROM Contact WHERE ID IN: conIDs]);

        for(Opportunity oppObj2 : newRecords)
        {
            if(oppObj2.AccountID != null && accountMap.containsKey(oppObj2.AccountID) && accountMap.get(oppObj2.AccountID).OwnerID != null){
                oppObj2.OwnerID = accountMap.get(oppObj2.AccountID).OwnerID;
            }
            // add field in both query and here
            if(oppObj2.Primary_Contact__c != null && contactMap.containsKey(oppObj2.Primary_Contact__c)){
                //oppObj2.Expense_Planning__c = contactMap .get(oppObj2.Primary_Contact__c).Expense_Planning__c;
            }
                          
                        
            
        }        
    }
 
Hi All,

Requirement : When new lead comes in i need to check If there is any activity on account or  contact or opportunity related to that account. If yes i need to assign to the queue. What is the best way to do this ? I know one account can have multiple contacts & opportunities but still i need to check on all contacts & opportunities where it is become complex.
Can any one help me out with this ?The overall code coverage is displaying  a constant value in production as 74% only, i have written and modiifed 'n' no of test classes and deployed ,but still it displays the constant value.Any suggestion or alternatives where i can get the code coverage increased and can do a deployment successfully.