Friday, October 2, 2015

Trigger Handler class for


public class InsuranceDetailHandler{
    public static void beforeInsert(List<Insurance_Detail__c> insDetBefNewList){
        Set<Id> relatedOppoertunityIdSet=new Set<Id>();
        Map<Id,Integer> oppIdWithInsDetSizeMap=new Map<Id,Integer>();
        Id dataEntryInitialRecId =RecordTypeUtil.recordTypeId('Insurance_Detail__c','LiveStock Initial DE');
        for(Insurance_Detail__c inds :insDetBefNewList){
            if(inds.RecordTypeId !=dataEntryInitialRecId ){
                 inds.addError('Your not authorised select this RecordType');
            }
        }// checking recordtype for inserting record
        
        for(Insurance_Detail__c insDet : insDetBefNewList){
            if(insDet.Ear_Tag_No__c != null || insDet.Ear_Tag_No__c != '' && insDet.Status__c =='Draft'){
              insDet.Status__c='Pending DE';
              insDet.RecordTypeId=RecordTypeUtil.recordTypeId('Insurance_Detail__c','LiveStockFinal DE');
            }
            if(insDet.Insured_For__c != null){
                relatedOppoertunityIdSet.add(insDet.Insured_For__c);
            }
        }
        if(!relatedOppoertunityIdSet.isEmpty()){
            for(Opportunity opp : [Select Id,Name,(SELECT Id FROM Insurance_Details__r) FROM Opportunity WHERE Id IN :relatedOppoertunityIdSet LIMIT 50000]){
                oppIdWithInsDetSizeMap.put(opp.Id,opp.Insurance_Details__r.size());
            }
        }
        for(Insurance_Detail__c insDet : insDetBefNewList){
            if(oppIdWithInsDetSizeMap.get(insDet.Insured_For__c)>0){
                insDet.addError('You can create only One Insurance Record');
            }
        }
    }//End of before Insert
     public static void beforeUpdate(List<Insurance_Detail__c> insDetBefUdtNewList){
         Id dataEntryInitialRecId = RecordTypeUtil.recordTypeId('Insurance_Detail__c','LiveStock Initial DE');
         Id dataEntryFinalRecId = RecordTypeUtil.recordTypeId('Insurance_Detail__c','LiveStockFinal DE');
         Id AssignToOpps = RecordTypeUtil.recordTypeId('Insurance_Detail__c','AssignToOpps');
         Id FormVerification = RecordTypeUtil.recordTypeId('Insurance_Detail__c','Form Verification');
         Set<Id> insDetIdSet=new Set<Id>();
          Map<Id,Integer> insDetWithAttachmentSizeMap=new Map<Id,Integer>();
         for(Insurance_Detail__c insDet :insDetBefUdtNewList ){
             if(insDet.RecordTypeId==dataEntryFinalRecId ){
                insDetIdSet.add(insDet.Id);
             }
         }
         if(!insDetIdSet.isEmpty()){
            for(Insurance_Detail__c inssDet: [SELECT id,Age__c,(SELECT Id,Name FROM Attachments WHERE ParentId IN :insDetIdSet) FROM Insurance_Detail__c WHERE Id IN :insDetIdSet LIMIT 50000]){
              insDetWithAttachmentSizeMap.put(inssDet.Id,inssDet.Attachments.size());
            }
         }   
            for(Insurance_Detail__c insDett :insDetBefUdtNewList){
               system.debug('Inside For Loop');
              
                if(insDett.RecordTypeId==dataEntryFinalRecId && insDett.Status__c == 'Pending DE'){
                    if(insDetWithAttachmentSizeMap.get(insDett.Id)==0){
                        insDett.addError('Please add attcahment in attcahment section to move forward.');
                    }
                } // end of attachement error  
               
                 if(insDett.RecordTypeId==dataEntryFinalRecId&&insDett.Age__c !=null &&insDett.Status__c=='Pending DE'){
                        system.debug('pending DE status'+insDett.Status__c);       
                        insDett.Status__c='Assigned to Ops';
                        insDett.RecordTypeId=AssignToOpps;
                        system.debug('pending DE status'+insDett.Status__c); 
                }// end of assignedtoopps
                
                if(insDett.RecordTypeId==AssignToOpps &&(insDett.status__c=='Assigned to Ops' ||insDett.status__c=='Reassigned to Ops') &&  insDett.Form_verified__c==True){
                       system.debug('hello');
                        insDett.Status__c='Form Verified';
                        insDett.RecordTypeId=FormVerification;
                }// end of  FormVerification
                
            }// end of for loop
        
     }//End of before update
}//End of class

No comments:

Post a Comment