+ Start a Discussion
TheLearnerTheLearner 

Populating max values of the date field--- very urgent

Hi Experts,

There is problem with my trigger,my scenario is that i need to get max values of h_Application_Date_Submitted__c date in to another field called Claimed_Cost_Last_Updated__c, but here showing last updated date only not the max value.

Triio Apalication is Master object in this object contains field called "Date Application submitted".
Triio sub application is child of triio application  in this object we created field called h_Application_Date_Submitted__c.
Triio work order Claimed_Cost_Last_Updated__c field is there this field we need to update ,there lookup with triio sub application.

When ever if i create  two reocrds in the triio Application with Date application submitted maximum date  has to display in the Claimed_Cost_Last_Updated__c which is there in the triio workorder record. I bloed the code which we need to do

Could anyone help me please


trigger tRIIO_Update_WorkOrder_from_SubApplication on tRIIO_Sub_Application__c(after insert, after update, after delete) {
    set<Id> WorkIOrderIds = new set<Id>();
    set<Id> AllWOIds = new set<Id>();
    set<Id> ProjectIds = new set<Id>();
    double dblMilestones_Claimed;
    
    tRIIO_Works_Order__c objWO;
    tRIIO_Project__c objProj;
    
    Map<Id,Date> subapplicationRecordDateSubMap=new Map<Id,Date>();
    Map<ID,Double> subapplicationRecordClaimCostMap=new Map<Id,Double>();
    
    Map<Id,Date> subapplicationRecordAssRejMap=new Map<Id,Date>();
    Map<ID,Date> subapplicationRecordInvSubDateMap=new Map<Id,Date>();
    Map<ID,Date> subapplicationRecordCashRecDateMap=new Map<Id,Date>();
    Map<ID,Double> subapplicationRecordAssValMap=new Map<Id,Double>();
    
    List<tRIIO_Sub_Application__c> SubAppList = new List<tRIIO_Sub_Application__c>();
    map<Id,Double> workOrderMap = new map <Id,Double>();
    map<Id,Id> workOrderCheckMap = new map <Id,Id>();
    List<tRIIO_Works_Order__c> WOSupLst=new List<tRIIO_Works_Order__c>();
    List<tRIIO_Project__c> ProjSupLst=new List<tRIIO_Project__c>();
    
    List<AggregateResult> workorderSubAppNameResult=new List<AggregateResult>();
    
    //START : Milestones claimed amount update logic start  Wo.Milestone Claimed=SUm(Sub Appl.Milestone_Payment__c) where App.Type='Milestone'
    
    if(trigger.isInsert ||trigger.isUpdate){
        for(tRIIO_Sub_Application__c subapp : trigger.new)
        {
            if(subapp.Works_Order_Ref__c!=null){
                if(subapp.h_Application_Type__c=='Milestone')
                    WorkIOrderIds.add(subapp.Works_Order_Ref__c);
                
                AllWOIds.add(subapp.Works_Order_Ref__c);
                
            }
            if(subapp.Project_Name__c!=null)
                ProjectIds.add(subapp.Project_Name__c);
            
        }
    }
    if(trigger.isDelete ||trigger.isUpdate){
        for(tRIIO_Sub_Application__c subapp :  trigger.old)
        {
            if(subapp.Works_Order_Ref__c!=null){
                if(subapp.h_Application_Type__c=='Milestone')
                    WorkIOrderIds.add(subapp.Works_Order_Ref__c);
                
                AllWOIds.add(subapp.Works_Order_Ref__c);
            }
            if(subapp.Project_Name__c!=null)
                ProjectIds.add(subapp.Project_Name__c);
            
        }
    }
    system.debug('###'+ProjectIds);
    //START : Milestones claimed amount update logic start  Wo.Milestone Claimed=SUm(Sub Appl.Milestone_Payment__c) where App.Type='Milestone'
    if(!WorkIOrderIds.isEmpty())
    {
        for(AggregateResult  q : [select Works_Order_Ref__c ,sum(Milestone_Payment__c)
                from tRIIO_Sub_Application__c where Works_Order_Ref__c IN :WorkIOrderIds  group by Works_Order_Ref__c ]){
            workOrderMap .put(
                (ID)q.get('Works_Order_Ref__c'),
                (Double)q.get('expr0'));
            workOrderCheckMap.put((ID)q.get('Works_Order_Ref__c'),(ID)q.get('Works_Order_Ref__c'));
        }
    }
    //END : Milestones claimed amount update logic end
 
    // START : Update Claimed Cost Last Updated Date and claimed cost on WO
    workorderSubAppNameResult=[SELECT  Works_Order_Ref__c, Max(h_Application_Date_Submitted__c) APPDATESUB,                        
                        SUM(Applied_Value__c) APPSUM FROM tRIIO_Sub_Application__c                                                      
                        where Works_Order_Ref__c IN :AllWOIds  group by Works_Order_Ref__c ];                          

    for(AggregateResult supAppName: workorderSubAppNameResult)
    {
        subapplicationRecordDateSubMap.put((id)supAppName.get('Works_Order_Ref__c'),(Date)supAppName.get('APPDATESUB'));    
        subapplicationRecordClaimCostMap.put((id)supAppName.get('Works_Order_Ref__c'),(Double)supAppName.get('APPSUM'));        
    }                        
    for(Id itrWO: AllWOIds)
    {
        objWO= new tRIIO_Works_Order__c();
        objWO.id=itrWO;
        if(workOrderCheckMap.get(itrWO)!=null)
            objWO.Milestones_Claimed__c=workOrderMap.get(itrWO);
        objWO.Claimed_Cost__c=subapplicationRecordClaimCostMap.get(itrWO);
        objWO.Claimed_Cost_Last_Updated__c=subapplicationRecordDateSubMap.get(itrWO);        
        WOSupLst.add(objWO);
    }
    
    if(!WOSupLst.isEmpty())
        Update WOSupLst;
      
    //END : Update Claimed Cost Last Updated Date on WO


}
}
Agustina GarciaAgustina Garcia
Hi,

Did you try a declarative solution? As you have a M-D relation ship, you can create a Rollup field on your Master object

User-added image

That looks for tha Max value of a certain field of all the child records

User-added image

Having this, every time you create a new child record and set a date, the Master date field will be automatically (#clicksnotcode) populated with the Max one.

User-added image

Hope this helps.
TheLearnerTheLearner
HI Augstina,

Thanks for ur reply, actually rollup summary has been over as provided by the salesforce and one more thing is that, triio application is master of triio sub application and triio work order is noware related to triio applicatin it but i has a lookup relation ship with triio subapplication thats way i used to subapplication to gather the date of triio application and triio workorder.

Could you help me please