+ Start a Discussion
Soundar Raj PonpandiSoundar Raj Ponpandi 

Create Records With Child Records (When Stage Change to Facility)

Dear Friends,

I am trying to create Checklist_Details__c With WS_Sub_Checklist_Details__c  (As Available in Checklist_Master__c With Check_Point__c ) In Opportunity When Stage Change to Facility.

Please Refer My Code and give me an idea how can i achieve This Process.

************************************************   Trigger *********************************************
Map<Id,Opportunity> OptyMap = New Map<Id,Opportunity>();
    List<WS_Facility_Details__c> Fsd = New List<WS_Facility_Details__c>();
    Set<ID> PrdId = New Set<ID>();
    Set<ID> LnTpId = New Set<ID>();
    Set<ID> OppId = New Set<ID>();
    Map<ID, Checklist_Master__c> CkLstId = New  Map<ID, Checklist_Master__c>();
    List<Checklist_Master__c> CMList_Prod = New List<Checklist_Master__c>();
    List<Checklist_Master__c> CMList_LnTp = New List<Checklist_Master__c>();
    List<Checklist_Details__c> Fac_CMList_Prd = New List<Checklist_Details__c>();
    List<Checklist_Details__c> Fac_CMList_LnTp = New List<Checklist_Details__c>();
    List<WS_Sub_Checklist_Details__c> SubCPList = New List<WS_Sub_Checklist_Details__c>();
    List<Checklist_Details__c> CDList  = new List<Checklist_Details__c>();
    List<Check_Point__c> CPList = New List<Check_Point__c>();
    List<Checklist_Master__c > CLMaster = New List<Checklist_Master__c>();
    List<Check_Point__c> CPList1 = New List<Check_Point__c>();
    Map<ID,Check_Point__c> CPSet = New Map<ID,Check_Point__c>();
    Map<ID,Checklist_Details__c> CdSet = New  Map<ID,Checklist_Details__c>();
    Set<ID> CmSet = New Set<ID>();
    
    
    For(Opportunity opp : Trigger.New){
        IF(opp.StageName != Trigger.oldMap.get(opp.Id).StageName){
            If(opp.StageName == 'Facility'){
                OptyMap.Put(opp.Id, opp);
            }
        }
        
    }
    System.debug('OptyMap Size | ' + OptyMap.Size() );
    If(OptyMap.Size() > 0 && OptyMap.Size() !=Null){
        /* ------ Checkilist From Product ------ */
        Fsd = [Select id,name,Opportunity__c,Product__c, Loan_Type__c from WS_Facility_Details__c Where Opportunity__c IN : OptyMap.KeySet() Order By CreatedDate Desc];
        For(WS_Facility_Details__c irow_Fsd_Prd : Fsd){
            PrdId.add(irow_Fsd_Prd.Product__c);
            OppId .add(irow_Fsd_Prd.Opportunity__c);
        }
        If(PrdId.Size() !=Null && PrdId.Size() > 0){
            /*Adding CheckList In Opportunity From Product*/
                       
            Opportunity Crnt_Opp_Prd = [Select id,StageName From Opportunity Where ID IN : OppId];
            
            CLMaster = [Select Id,Name, Stage__c, CL_Product__c, Check_List__c,Loan_Type__c From Checklist_Master__c Where CL_Product__c IN : PrdId];
            
            For(Checklist_Master__c Irow_cm2 : CLMaster){
              CmSet.add(Irow_cm2.Id);
            }
          
            CPList = [Select Id,Checklist__c, Checklist_Master__c From Check_Point__c Where Checklist_Master__c IN : CPSet.KeySet()];

                For(Checklist_Master__c irow_CM_Prd : CLMaster){
                    Checklist_Details__c CM_New = New Checklist_Details__c();
                    CM_New.Checklist_Master__c = irow_CM_Prd.Id;
                    CM_New.Stage__c= irow_CM_Prd.Stage__c;
                    CM_New.Checklist__c= irow_CM_Prd.Check_List__c;
                    CM_New.Opportunity__c = Crnt_Opp_Prd.Id ;
                    Fac_CMList_Prd.add(CM_New);
                    //CkLstId.Put(irow_CM_Prd.Id, irow_CM_Prd);
            }
            }
            System.debug('******** SubCPList ********'  + 'SubCPList.Checklist_Details__c');
            /*Ready to Insert Checklist Details From Product*/
            If(Fac_CMList_Prd.Size()>0 && Fac_CMList_Prd.Size() != Null){
                Insert Fac_CMList_Prd; 
               
                for(Checklist_Details__c irow_Cm : Fac_CMList_Prd){
                    CDSet.Put(irow_Cm.id,irow_Cm);
                }
                
                CDList = [Select id From Checklist_Details__c Where ID IN : CDSet.KeySet()];
                
                For(Check_Point__c Irow_cp : [Select Id,Checklist__c, Checklist_Master__c From Check_Point__c Where Checklist_Master__c IN : CmSet]){
                    WS_Sub_Checklist_Details__c  SubCD = New WS_Sub_Checklist_Details__c();
                    SubCD.Checklist__c = Irow_cp.Checklist__c;
                    For(Checklist_Details__c Irow_cp1 :  CDList){
                    IF(CDSet.containskey(Irow_cp1.Id)){
                    SubCD.Checklist_Details__c = Irow_cp1.Id;
                    }
                    }
                    SubCPList.add(SubCD);
                
            }
            
           System.debug('*********SubCPList | ' + SubCPList.Size());
            IF(SubCPList.Size() > 0 && SubCPList.Size() != NUll){
                Insert SubCPList;
            }
        }
        
}


I 'll feel Happy When You give a good Solution !!!!
Regards,
Soundar.