Friday, October 2, 2015

Trigger Handler class for Before insert and before update

Public class InsuranceClaimHandler{
    // beforeInsert method,if claim reason is "Disease",it will check Insurance detail created date and insurance claim date difference
   
    public static void beforeInsert(List<Insurance_Claim__c> InsClmbeforeNewList){
       Id ClaimIntimation = RecordTypeUtil.recordTypeId('Insurance_Claim__c','Claim Intimation');
       DateTime InsDeCrd;
       Set<Id> relatedInsDetailsIdSet=new Set<Id>();
       Map<ID, Insurance_Detail__c> parentInsDet = new Map<ID, Insurance_Detail__c>();
      
        for(Insurance_Claim__c insClm : InsClmbeforeNewList){
            if(insClm .Insurance_Detail__c != null && insClm .Death_due_to__c=='Diseases'){
                relatedInsDetailsIdSet.add(insClm .Insurance_Detail__c);
            }
            if(insClm .RecordTypeId!=ClaimIntimation ){
                insClm.addError('Your not authorised select this RecordType');
             }
           
        }
        if(!relatedInsDetailsIdSet.isEmpty()){
            parentInsDet = new Map<Id, Insurance_Detail__c>([SELECT id,Ear_Tag_No__c,Name,createdDate FROM Insurance_Detail__c WHERE ID IN :relatedInsDetailsIdSet]);
  
       
       
        //DateTime dt = System.now();
        Date Todaydate = System.today();
        system.debug('Todaydate@@@@@@@@@@'+Todaydate);
        for (Insurance_Claim__c InClm: InsClmbeforeNewList){
                  InsDeCrd=(parentInsDet.get(InClm.Insurance_Detail__c).createdDate);
                  Date InsdCrdate=InsDeCrd.date();
                  system.debug('InsdCrdate@@@@@@@@@@'+InsdCrdate);
                  Integer numberDaysDue = InsdCrdate.daysBetween(Todaydate);
                  Integer numberDaysDue1=Integer.valueOf(system.label.InsuranceClaimDatedifference);// InsuranceClaimDatedifference store no of days
                  if(InClm.Death_due_to__c=='Diseases'){ 
                     
                     if(numberdaysdue<numberDaysDue1){
                       InClm.addError('Unable to create Insurance Claim for Disease category immediately ');
                     }
                  }  
       }
       }        
                 
    }// end of beforeInsert  method
   
    public static void beforeUpdate(List<Insurance_Claim__c> insDetBefUdtNewList,Map<Id,Insurance_Claim__c> oldRecordsMap){
   
          Id ClaimIntimation = RecordTypeUtil.recordTypeId('Insurance_Claim__c','Claim Intimation');
          Id ClaimSubmittedtoOpps = RecordTypeUtil.recordTypeId('Insurance_Claim__c','Claim Submitted to Opps');
          Id ClaimReceivedatOps = RecordTypeUtil.recordTypeId('Insurance_Claim__c','Claim Received at Ops');
          Id ClaimSubmittedToIns = RecordTypeUtil.recordTypeId('Insurance_Claim__c','Claim Submitted to Ins');
          Id ClaimSettled = RecordTypeUtil.recordTypeId('Insurance_Claim__c','Claim Settled');
          Id ClaimSettledbyIns = RecordTypeUtil.recordTypeId('Insurance_Claim__c','Claim settled by Ins');
        
         // system.debug('ClaimIntimation '+ClaimIntimation );
         
          Set<Id> insDetIdSet=new Set<Id>();
          /* Bellow Method will check the attcahment:Record Type is ClaimIntimation  CASE ONE*/
         
          Map<Id,Integer> insDetWithAttachmentSizeMap=new Map<Id,Integer>();
          for(Insurance_Claim__c insDet :insDetBefUdtNewList ){
             if(insDet.RecordTypeId==ClaimIntimation ){
                insDetIdSet.add(insDet.Id);
             }
           }
           system.debug('insDetIdSet'+insDetIdSet);
           // method1 will check attcahment if no attcahment throw error message
         if(!insDetIdSet.isEmpty()){
            for(Insurance_Claim__c inssDet: [SELECT id,Age__c,(SELECT Id,Name FROM Attachments WHERE ParentId IN :insDetIdSet) FROM Insurance_Claim__c WHERE Id IN :insDetIdSet LIMIT 50000]){
              insDetWithAttachmentSizeMap.put(inssDet.Id,inssDet.Attachments.size());
            }
        }// end of if condition for attcahment error   
       
        for(Insurance_Claim__c insDett :insDetBefUdtNewList){
                /* Attachments CHECK CONDITION CASE ONE */
                if(insDett.RecordTypeId==ClaimIntimation && insDett.Status__c=='Claim Intimated'){
                  system.debug('ClaimIntimation**CASE ONE'+ClaimIntimation);
                    if(insDetWithAttachmentSizeMap.get(insDett.Id) ==0){
                        insDett.addError('Please add attcahment in attcahment section to move forward.');
                    }
                } /* end of CASE ONE */
               
                 /* Attachments is uploaded user will check the checkbox attachment_attached then system will update status and Record Type CASE TWO*/
                
              else if(insDett.Status__c=='Claim Submitted to Ops' && insDett.RecordTypeId==ClaimSubmittedtoOpps && oldRecordsMap.get(insDett.id).Status__c=='Claim Submitted to Ops' /*&& insDett.attachment_attached__c==True*/){
                if(insDett.attachment_attached__c==True ){
                   system.debug('ClaimIntimation**CASE two'+ClaimSubmittedtoOpps);
                   insDett.Status__c='Claim Received at Ops';
                   insDett.RecordTypeId=ClaimReceivedatOps;
                }
                else{
                   insDett.addError('Please check the  attcahment checkbox');
                }              
              }/* END OF CASE TWO */
             
               /* USER WILL update the document details and provious one is null present will not null CASE THREE */
              
              else if((insDett.RecordTypeId== oldRecordsMap.get(insDett.id).RecordTypeId) && oldRecordsMap.get(insDett.id).attachment_attached__c==True && (insDett.Status__c=='Claim Received at Ops' || insDett.Status__c=='Claim Resubmitted to Ops' )){
                 if((insDett.Document_Comments__c!=null && oldRecordsMap.get(insDett.id).Document_Comments__c==null )&& (insDett.Documents_Checklist__c!=null && oldRecordsMap.get(insDett.id).Documents_Checklist__c==null )){
                      system.debug('ClaimIntimation**CASE THREE'+ClaimIntimation);
                      insDett.Status__c='Pending Claim Submitted to Ins';
                      insDett.RecordTypeId=ClaimReceivedatOps;
                  }
                   else{
                         insDett.addError('Please Update document details');
                    }
                } 
               /* END OF CASE THREE */
              
              /* USER WILL DOWNLOAD RECORDS AND Submitted TO Insurance COMPANY  CASE THREE */
              else if(insDett.RecordTypeId==ClaimReceivedatOps && oldRecordsMap.get(insDett.id).Status__c=='Pending Claim Submitted to Ins'){
                 if((insDett.ClaimSubmittedToInsDate__c!=null)){
                      system.debug('ClaimIntimation**CASE THREE'+ClaimIntimation);
                      insDett.Status__c='Claim Submitted to Ins';
                      insDett.RecordTypeId=ClaimSubmittedToIns;
                  }
                  
                } 
               /* END OF CASE THREE */
             
              /* WHEN USER UploadDate Status__c WILL UPDATE  CASE FOUR */
              else if(insDett.RecordTypeId==ClaimSubmittedToIns && insDett.Status__c=='Claim Submitted to Ins' ){
                  system.debug('ClaimIntimation**CASE FOUR'+ClaimSubmittedToIns);
                  if(insDett.Upload_Date__c!=null && oldRecordsMap.get(insDett.id).Upload_Date__c==null ){
                      insDett.Status__c='Claim settled by Ins';
                      insDett.RecordTypeId=ClaimSettled;
                      system.debug('insDett.Status__c'+insDett.Status__c);
                    }
                     else{
                         insDett.addError('Please update uploaded date');
                        }
             
                }  /* END OF CASE FOUR */
           
                /* WHEN USER UPDATE THE Settled AMOUNT  Status__c WILL UPDATE  CASE FIVE */
                else if(insDett.RecordTypeId==ClaimSettled && oldRecordsMap.get(insDett.id).Status__c=='Claim settled by Ins' ){
                  system.debug('ClaimIntimation**CASE FOUR'+ClaimSettled);
                  if(insDett.Upload_Date__c!=null && insDett.Settled_Amount__c!=null){
                      insDett.Status__c='Claim Cheque Dispatched to JC';
                      insDett.RecordTypeId=ClaimSettledbyIns;
                      system.debug('insDett.Status__c'+insDett.Status__c);
                    }
                     else{
                         insDett.addError('Please update settled amount');
                        }
             
                }  /* END OF CASE FIVE */
             /* WHEN USER UPDATE THE CHECK NUMBER  Status__c WILL UPDATE  CASE SIX */
              else if(insDett.RecordTypeId==ClaimSettledbyIns && oldRecordsMap.get(insDett.id).Status__c=='Claim Cheque Dispatched to JC' ){
                  system.debug('ClaimIntimation**CASE FOUR'+ClaimSettled);
                  if(insDett.Check_Number__c!=null){
                      insDett.Status__c='Claim Settled to Customer';
                      //insDett.RecordTypeId=ClaimSettledbyIns;
                      system.debug('insDett.Status__c'+insDett.Status__c);
                    }
                     else{
                         insDett.addError('Please update CHECK NUMBER');
                        }
             
            }  /* END OF CASE SIX */
        }
    }// END OF BEFOREUPDATE METHOD
}

No comments:

Post a Comment