+ Start a Discussion
BussBuss 

Deployment Error

Hi all,

 

Below failure errors is showing while depolying to production, but while 'Run Tests'  no error is showing.

 

Failure Message: "System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, OpportunityTostageHistory: execution of BeforeInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Oppor...

 if i remove below code it's working fine.

if(AcSet.size()>0){
AcList = [SELECT id,Name,First_Oppty_Won__c FROM Account where id In:AcSet];
for(Account a:AcList){
a.First_Oppty_Won__c = a.First_Oppty_Won__c+1 ;
UAL.add(a);
}
update UAL;
}



below is complete code:

 

 

 

trigger OpportunityTostageHistory on Opportunity (before insert, after insert, before update)
{
    Set<Id> AcSet=new Set<Id>();
    List<Account> AcList;
    List<Account> UAL=new List<Account>();
         //variable to store old stage value to comparision
         String previousstage;
         List<Account> accs;
         //Query for the opportunity record types
         List<RecordType> rtypes = [Select Name, Id From RecordType
                      where sObjectType='Opportunity' and isActive=true];
         
         //Create a map between the Record Type Name and Id for easy retrieval
         Map<String,String> oppRecordTypes = new Map<String,String>{};
         for(RecordType rt: rtypes)
            oppRecordTypes.put(rt.Name,rt.Id);
           
         system.debug('%%%%%&&&&&&&&&&%%%%%%%%%%%%%%%%%%'+oppRecordTypes.get('Sales force'));
            String s3 = oppRecordTypes.get('Sales force');
            String qid = s3.substring(0,15);
            System.debug('$$$$$$$$$$$$^&&&&&&&&&&'+qid);
           
            /*-----Once Stage verbal apporval does not allow to previous stage----*/
            //create a map to opportunity stage and id's
            Map<String,Integer> mapp=new Map<String,Integer>();
            mapp.put('Call',01);
            mapp.put('Meeting',02);
            mapp.put('Demo',03);
            mapp.put('Custom Demo',04);
            mapp.put('Budget Validated',05);
            mapp.put('Need Analysis',06);
            mapp.put('Solution Proposed',07);
            mapp.put('POC Paid',08);
            mapp.put('POC unpaid',09);
            mapp.put('Written Proposal',10);
            mapp.put('Quote Approval',11);
            mapp.put('Verbal Approval',12);
            mapp.put('Request',13);
            mapp.put('Poc',14);
            mapp.put('Proposed',15);
            mapp.put('Negotiation',16);
            mapp.put('Closed/Lost',17);
            mapp.put('Closed/Won',17);    
       
 
 for(Opportunity I:Trigger.new)
   {  
     Stage__c  IV = new Stage__c ();
  
    if(trigger.isInsert){
   
       if(trigger.isBefore){
             if(I.StageName=='Closed/Won'){
                 AcSet.add(I.accountid);                                               
             }   
            }       
      
           //after insert functionality
          
            IV.Opportunity__c = I.Id ;
            IV.CloseDate__c =I.CloseDate ;
            IV.Amount__c = I.Amount ;
            IV.Comments_c__c = I.Comments__c ;
             
        
           system.debug('**********232323232********'+I.RecordTypeId+'@@@@@@@@@@@@'+I.StageName+'##########'+I.Vertical__c) ;
          
           IV.Name = I.StageName ;
      if(I.Vertical__c != null){
          if(I.Vertical__c == 'Sales force' || I.Vertical__c == 'MIDAS'){
         
           IV.Name = 'Call';
           IV.Probability__c = 5+'%' ;
          system.debug('**********'+IV.Name) ;
          }else if(I.Vertical__c == 'On-Demand Consulting' || I.Vertical__c == 'Projects'){
          IV.Name = 'Request';
          IV.Probability__c = 10+'%' ;
            system.debug('*****55555555*****'+IV.Name) ;
          }
         }
         
       //  if(I.RecordTypeId =='012T00000000bcg' || I.RecordTypeId =='012T00000000bcl'){
       //   if(I.RecordTypeId =='012F0000000qEF6' || I.RecordTypeId =='012F0000000qEF3'){
      if(I.RecordTypeId==oppRecordTypes.get('MIDAS') || I.RecordTypeId==oppRecordTypes.get('Sales force') || I.RecordTypeId==oppRecordTypes.get('IBM') ){
       
         if(IV.Name!= null){
             if(IV.Name == 'Call')
             {
                IV.Probability__c = 5+'%' ;
              
             }else If(IV.Name == 'Request'){
              IV.Probability__c = 10+'%' ;
             }
            }else{
             If(I.StageName == 'Call'){
                 IV.Probability__c = 5+'%' ;
                   }
            }
           
            If(I.StageName == 'Meeting'){
               IV.Probability__c = 10+'%' ;
            }else If(I.StageName == 'Demo'){
               IV.Probability__c = 15+'%' ;
            }else If(I.StageName == 'Custom Demo'){
               IV.Probability__c = 20+'%' ;
            }else If(I.StageName  == 'Budget Validated'){
               IV.Probability__c = 25+'%' ;
            } else If(I.StageName  == 'Need Analysis'){
               IV.Probability__c = 30+'%' ;
            } else If(I.StageName  == 'Solution Proposed'){
               IV.Probability__c = 40+'%' ;
            } else If(I.StageName  == 'POC Paid'){
               IV.Probability__c = 50+'%' ;
            } else If(I.StageName  == 'POC unpaid'){
               IV.Probability__c = 50+'%' ;
            } else If(I.StageName  == 'Written Proposal'){
               IV.Probability__c = 60+'%' ;
            }else If(I.StageName  == 'Quote Approval'){
               IV.Probability__c = 70+'%' ;
            }else If(I.StageName  == 'Verbal Approval'){
               IV.Probability__c = 80+'%' ;
            }else If(I.StageName  == 'Closed/Lost'){
               IV.Probability__c = 0+'%' ;
            }else If(I.StageName == 'Closed/Won'){
               IV.Probability__c = 100+'%' ;
            }
          
            }
        //   if(I.RecordTypeId  =='012T00000000bcq' || I.RecordTypeId  =='012T00000000bcv' ){ 
       //   if(I.RecordTypeId  =='012F0000000qEF4' || I.RecordTypeId  =='012F0000000qEF5' || I.RecordTypeId  =='012F0000000qEHn'){
    if(I.RecordTypeId==oppRecordTypes.get('On Demand Const') || I.RecordTypeId==oppRecordTypes.get('Projects') || I.RecordTypeId==oppRecordTypes.get('Others')){ 
            system.debug('**********11111*********'+I.RecordTypeId+'@@@@@@@@@@@@'+I.StageName) ;
             If(I.StageName == 'Request'){
             system.debug('*********222**********'+I.StageName);
                IV.Probability__c = 10+'%' ;
             }else If(I.StageName == 'Poc'){
               IV.Probability__c = 25+'%' ;
            }else If(I.StageName == 'Proposed'){
               IV.Probability__c = 50+'%' ;
            }else If(I.StageName == 'Negotiation'){
               IV.Probability__c = 75+'%' ;
            }else If(I.StageName == 'Closed/Won'){
               IV.Probability__c = 100+'%' ;
            }else If(I.StageName  == 'Closed/Lost'){
               IV.Probability__c = 0+'%' ;
            }
           
            }
          
        
          insert IV; 
    
      }
     
      if(trigger.isupdate){   
     
            if(I.Vertical__c!=null){
         
            I.Vertical__c = null ;
                  
           
            }else{
  
    List<Stage__c> s = [select Id,Name,Amount__c,Comments_c__c,Opportunity__c from Stage__c where Opportunity__c =:I.Id ];
   Integer inc =0 ;
  for(Stage__c s1 : s){
 
          if(s1.Amount__c == I.Amount && s1.Name == I.StageName && s1.Comments_c__c == I.Comments__c ){
         
          inc ++ ;
         
          }
    }
 
            IV.Opportunity__c = I.Id ;
            IV.CloseDate__c =I.CloseDate ;
            IV.Amount__c = I.Amount ;
            IV.Comments_c__c = I.Comments__c ;
            IV.Name = I.StageName ;
            
       //  if(I.RecordTypeId =='012T00000000bcg' || I.RecordTypeId =='012T00000000bcl'){ 
   //  if(I.RecordTypeId =='012F0000000qEF6' || I.RecordTypeId =='012F0000000qEF3'){
       if(I.RecordTypeId==oppRecordTypes.get('MIDAS') || I.RecordTypeId==oppRecordTypes.get('Sales force') || I.RecordTypeId==oppRecordTypes.get('IBM')){
            If(I.StageName == 'Call'){
         
               IV.Probability__c = 5+'%' ;
         
            }else If(I.StageName == 'Meeting'){
               IV.Probability__c = 10+'%' ;
            }else If(I.StageName == 'Demo'){
               IV.Probability__c = 15+'%' ;
            }else If(I.StageName == 'Custom Demo'){
               IV.Probability__c = 20+'%' ;
            }else If(I.StageName  == 'Budget Validated'){
               IV.Probability__c = 25+'%' ;
            } else If(I.StageName  == 'Need Analysis'){
               IV.Probability__c = 30+'%' ;
            } else If(I.StageName  == 'Solution Proposed'){
               IV.Probability__c = 40+'%' ;
            } else If(I.StageName  == 'POC Paid'){
               IV.Probability__c = 50+'%' ;
            } else If(I.StageName  == 'POC unpaid'){
               IV.Probability__c = 50+'%' ;
            } else If(I.StageName  == 'Written Proposal'){
               IV.Probability__c = 60+'%' ;
            }else If(I.StageName  == 'Quote Approval'){
               IV.Probability__c = 70+'%' ;
            }else If(I.StageName  == 'Verbal Approval'){
               IV.Probability__c = 80+'%' ;
            }else If(I.StageName  == 'Closed/Lost'){
               IV.Probability__c = 0+'%' ;
            }else If(I.StageName == 'Closed/Won'){
               IV.Probability__c = 100+'%' ;
               AcSet.add(I.accountid);
              
            }
          
            }
        //      if(I.RecordTypeId  =='012T00000000bcq' || I.RecordTypeId  =='012T00000000bcv' ){ 
         //   if(I.RecordTypeId  =='012F0000000qEF4' || I.RecordTypeId  =='012F0000000qEF5' || I.RecordTypeId  =='012F0000000qEHn'){
      if(I.RecordTypeId==oppRecordTypes.get('On Demand Const') || I.RecordTypeId==oppRecordTypes.get('Projects') || I.RecordTypeId==oppRecordTypes.get('Others')){ 
      
           
             If(I.StageName == 'Request'){
                IV.Probability__c = 10+'%' ;
             }else If(I.StageName == 'Poc'){
               IV.Probability__c = 25+'%' ;
            }else If(I.StageName == 'Proposed'){
               IV.Probability__c = 50+'%' ;
            }else If(I.StageName == 'Negotiation'){
               IV.Probability__c = 75+'%' ;
            }else If(I.StageName == 'Closed/Won'){
               IV.Probability__c = 100+'%' ;
               AcSet.add(I.accountid);
              
            }else If(I.StageName  == 'Closed/Lost'){
               IV.Probability__c = 0+'%' ;
            }
           
            }
           
          if(inc != 1 )   
            {
              insert IV;
            }
       
         
      }
     
           //to retrieve old opportunity stage value
        for(Opportunity l1:Trigger.old){
            previousstage=l1.StageName;
            //prevForeCastCatg=l1.ForecastCategoryName;
           
        }
       
        //for each new insert opportunity stage
       
            System.debug('@@@@@@@@@@@@@@@@@@@@@@@@@**************'+previousstage);
           
            if(previousstage == 'Verbal Approval' || previousstage == 'Negotiation' || previousstage == 'Closed/Won' || previousstage == 'Closed/Lost'){
                System.debug('Previous fore cast catagory is commit or Closed/Won or Closed/Lost is enterd'+previousstage);          
                if(!(mapp.get(previousstage)<mapp.get(I.StageName))){
                    if(previousstage == 'Closed/Won'){
                        I.StageName.addError('Opportunity already Closed/Won');
                    }else if(previousstage == 'Closed/Lost'){
                         I.StageName.addError('Opportunity already Closed/Lost');
                    }
                    else{
                        I.StageName.addError('Please Select the Next Stage');
                    }
                }
               
            }
      
     }
      
            
   }   
   if(AcSet.size()>0){
       AcList = [SELECT id,Name,First_Oppty_Won__c FROM Account where id In:AcSet];
       for(Account a:AcList){
           a.First_Oppty_Won__c = a.First_Oppty_Won__c+1 ;
           UAL.add(a);
       }
       update UAL;            
   }
}

 

 

GuyClairboisGuyClairbois

did you check whether the field that is mentioned in the error message is the same on your sandbox as it is on production? It might be required on production but not on sandbox?

 

 

BussBuss

Thanks GuyClairbois.

 

issue is resolved by removing 'before insert' from trigger i.e only after insert and before update triggers are running.

 

Regards,

Buss