+ Start a Discussion
TheLearnerTheLearner 

Need to display forth coming date

Hi Experts,

if Project.Estimated_Project_Start_Date__c != null AND Proj.Work_Type__c != null then it will select all those WPD Contracts which are having

WPD Contract.Work_Type__c matching the WPD Project.Work_Type__c  
AND
WPD Contract.End_Date__c >= Project.Estimated_Project_Start_Date__c

Should the above formula fetch two possible solutions (Contract records) then choose the Contract Number based on the nearest forthcoming End Date.
i.e. Contract Number 03805 and 03806 found Estimated Project Start Date = 23/09/2015.
03805 has an End Date of 31/10/2015
03806 has an End Date of 31/10/2016
Therefore i need to display as the nearest forthcoming date = 31/10/2015 select the Contract Number as 03805.  Could anyone help me where i need to make an amendment please.

trigger WPD_UpdateWorkPackSeenDate_Project  on WPD_Projects__c (before insert, before update) {
    map<id,boolean> mapWPS_New = new map<id,boolean>();
    map<id,boolean> mapWPS_Old = new map<id,boolean>();
    if(trigger.isinsert){
        for(WPD_Projects__c project : trigger.new){
            if(project.Work_Pack_Seen__c==true)
                 project.Work_Pack_Seen_Date__c=system.today();   
        }
    }
    else if(trigger.isupdate){
          for(WPD_Projects__c NewProj : trigger.new)
              mapWPS_New.put(NewProj.id,NewProj.Work_Pack_Seen__c);
              
          for(WPD_Projects__c OldProj : trigger.old)
              mapWPS_Old.put(OldProj.id,OldProj.Work_Pack_Seen__c);
          
          for(WPD_Projects__c proj : trigger.new){
              system.debug('rrrrr-----'+mapWPS_New.get(proj.id));
              system.debug('tttttt-----'+mapWPS_Old.get(proj.id));
               system.debug('wwwwww-----'+proj.Work_Pack_Seen__c);
              if(mapWPS_New.get(proj.id)!=mapWPS_Old.get(proj.id) && proj.Work_Pack_Seen__c==true)
                 proj.Work_Pack_Seen_Date__c=system.today();    
          }             
    }
    
    //CH01.Start
    Set<Date> setDates = new Set<Date>();
    Set<String> setWorkType = new Set<String>();
    Date estimatePlanDate = Date.newInstance(2014,10,31);//condition needs to satisfy for above dates only
    
    //select all the Work types
    for(WPD_Projects__c Proj:trigger.new){
        System.debug(Proj.Estimated_Project_Start_Date__c+'= '+(Proj.Estimated_Project_Start_Date__c >= estimatePlanDate));
        if(Proj.Contract__c != null && Proj.Work_Type__c != null && Proj.Estimated_Project_Start_Date__c >= estimatePlanDate)
            setWorkType.add(Proj.Contract__c);
    }
    Map<String,WPD_Contract__c> mapContract = new Map<String,WPD_Contract__c>();
    if(!setWorkType.isEmpty())
     for(WPD_Contract__c con:[SELECT Id,Work_Type__c,End_Date__c FROM WPD_Contract__c WHERE id IN:setWorkType])
            mapContract.put(con.id,con);
  /* 
  CH02 
    System.debug('setWorkType = '+setWorkType+' , ='+estimatePlanDate);
    
    String queryStr = '';
    //Preparing query.
    if(!setWorkType.isEmpty()){
        queryStr = 'SELECT Id,Work_Type__c,End_Date__c FROM WPD_Contract__c WHERE Work_Type__c INCLUDES (';
        for(String st:setWorkType){
            queryStr = queryStr+'\''+st+'\';';
        }
        
        queryStr = queryStr.subStringBeforeLast(';')+')';
        System.debug('queryStr => '+queryStr+', setWorkType size = '+setWorkType.size()+', estimatePlanDate = '+estimatePlanDate);
        
        for(WPD_Contract__c con:Database.query(queryStr))
            mapContract.put(con.id,con);
    }
   CH02
    */
    System.debug('mapContract ='+mapContract);
    //condition.    
    for(WPD_Projects__c Proj:trigger.new){
        if(Proj.Estimated_Project_Start_Date__c != null && Proj.Estimated_Project_Start_Date__c >= estimatePlanDate){
            if(Proj.Contract__c!= null && !(Proj.Work_Type__c != null && mapContract.containsKey(Proj.Contract__c) && mapContract.get(Proj.Contract__c) != null
                && Proj.Estimated_Project_Start_Date__c <= mapContract.get(Proj.Contract__c).End_Date__c
                && mapContract.get(Proj.Contract__c).Work_Type__c.ContainsIgnoreCase(Proj.Work_Type__c))){
                    Proj.addError('Work Type/Estimated Project Start Date is not matched for the Contract.');
            }
        }
    }
    //CH01.End
}
pconpcon
If I understand what you are trying to do, the modifications below (lines 56-86) should do what you are looking to do
 
trigger WPD_UpdateWorkPackSeenDate_Project  on WPD_Projects__c (before insert, before update) {
    Map<Id, Boolean> mapWPS_New = new Map<Id, Boolean>();
    Map<Id, Boolean> mapWPS_Old = new Map<Id, Boolean>();

    if (trigger.isInsert) {
        for (WPD_Projects__c project : Trigger.new) {
            if (project.Work_Pack_Seen__c) {
                project.Work_Pack_Seen_Date__c=system.today();
            }
        }
    } else if (Trigger.isUpdate) {
        for (WPD_Projects__c newProj : Trigger.new) {
            mapWPS_New.put(newProj.Id, newProj.Work_Pack_Seen__c);
        }

        for (WPD_Projects__c oldProj : Trigger.old) {
            mapWPS_Old.put(oldProj.Id, oldProj.Work_Pack_Seen__c);
        }

        for (WPD_Projects__c proj : Trigger.new)
            if (
                mapWPS_New.get(proj.Id) != mapWPS_Old.get(proj.Id) &&
                proj.Work_Pack_Seen__c
            ) { 
                proj.Work_Pack_Seen_Date__c = system.today();
            }
        }   
    }   

    Set<Date> setDates = new Set<Date>();
    Set<String> setWorkType = new Set<String>();
    Date estimatePlanDate = Date.newInstance(2014, 10, 31);//condition needs to satisfy for above dates only
    
    for (WPD_Projects__c proj : Trigger.new) {
        if (
            proj.Contract__c != null &&
            proj.Work_Type__c != null &&
            proj.Estimated_Project_Start_Date__c >= estimatePlanDate
        ) {
            setWorkType.add(Proj.Contract__c);
        }   
    }           

    Map<String, WPD_Contract__c> mapContract = new Map<String, WPD_Contract__c>();
    if (!setWorkType.isEmpty()) {
        for (WPD_Contract__c con : [
            select Work_Type__c, 
                End_Date__c
            from WPD_Contract__c
            where id in :setWorkType
        ]) {
            mapContract.put(con.id, con);
        }
    }       

    for (WPD_Projects__c proj : Trigger.new) {
        WPD_Contract__c currentContract = null;
        Integer currentClosest;

        for (WPD_Contract__c contract : mapContract.values()) {
            if (
                contract.End_Date__c < proj.Estimated_Project_Start_Date__c ||
                contract.Work_Type__c != proj.Work_Type__c
            ) {
                continue;
            }

            if (currentContract == null) {
                currentContract = contract;
                currentClosest = proj.Estimated_Project_Start_Date__c.daysBetween(contract.End_Date__c);

                continue;
            }

            Integer diff = proj.Estimated_Project_Start_Date__c.daysBetween(contract.End_Date__c);

            if (diff < currentClosest) {
                currentContract = contract;
                currentClosest = diff;
            }
        }

        if (currentContract != null) {
            proj.Contact__c = currentContract;
        }
    }

    for (WPD_Projects__c proj : Trigger.new) {
        if (
            proj.Estimated_Project_Start_Date__c != null &&
            proj.Estimated_Project_Start_Date__c >= estimatePlanDate
        ) { 
            if (
                proj.Contract__c != null &&
                !(  
                    proj.Work_Type__c != null &&
                    mapContract.containsKey(proj.Contract__c) &&
                    mapContract.get(proj.Contract__c) != null &&
                    proj.Estimated_Project_Start_Date__c <= mapContract.get(proj.Contract__c).End_Date__c &&
                    mapContract.get(proj.Contract__c).Work_Type__c.ContainsIgnoreCase(proj.Work_Type__c)
                )
            ){  
                proj.addError('Work Type/Estimated Project Start Date is not matched for the Contract.');
            }
        }
    }
}

NOTE: This code has not been tested and may contain typographical or logical errors
NOTE: When adding code please use the "Add a code sample" button (icon <>) to increase readability and make it easier to reference.
TheLearnerTheLearner

HI Pcon,

Thanks for the reply and sorry for the late reply, its thrwong error by saying illgal assignment id to 'proj.Contract__c = currentContract' WPD contract, here contract is Look up field in the wpd project.

I ameneded like this could you tell me please, where i need to amend exactly.here ch03 is mine



trigger WPD_UpdateWorkPackSeenDate_Project  on WPD_Projects__c (before insert, before update) {
    map<id,boolean> mapWPS_New = new map<id,boolean>();
    map<id,boolean> mapWPS_Old = new map<id,boolean>();
    if(trigger.isinsert){
        for(WPD_Projects__c project : trigger.new){
            if(project.Work_Pack_Seen__c==true)
                 project.Work_Pack_Seen_Date__c=system.today();   
        }
    }
    else if(trigger.isupdate){
          for(WPD_Projects__c NewProj : trigger.new)
              mapWPS_New.put(NewProj.id,NewProj.Work_Pack_Seen__c);
              
          for(WPD_Projects__c OldProj : trigger.old)
              mapWPS_Old.put(OldProj.id,OldProj.Work_Pack_Seen__c);
          
          for(WPD_Projects__c proj : trigger.new){
              system.debug('rrrrr-----'+mapWPS_New.get(proj.id));
              system.debug('tttttt-----'+mapWPS_Old.get(proj.id));
               system.debug('wwwwww-----'+proj.Work_Pack_Seen__c);
              if(mapWPS_New.get(proj.id)!=mapWPS_Old.get(proj.id) && proj.Work_Pack_Seen__c==true)
                 proj.Work_Pack_Seen_Date__c=system.today();    
          }             
    }
    
    //CH01.Start
    Set<Date> setDates = new Set<Date>();
    Set<String> setWorkType = new Set<String>();
    Date estimatePlanDate = Date.newInstance(2014,10,31);//condition needs to satisfy for above dates only
    
    //select all the Work types
    for(WPD_Projects__c Proj:trigger.new){
        System.debug(Proj.Estimated_Project_Start_Date__c+'= '+(Proj.Estimated_Project_Start_Date__c >= estimatePlanDate));
        if(Proj.Contract__c != null && Proj.Work_Type__c != null && Proj.Estimated_Project_Start_Date__c >= estimatePlanDate)
            setWorkType.add(Proj.Contract__c);
    }
    Map<String,WPD_Contract__c> mapContract = new Map<String,WPD_Contract__c>();
    if(!setWorkType.isEmpty())
     for(WPD_Contract__c con:[SELECT Id,Work_Type__c,End_Date__c FROM WPD_Contract__c WHERE id IN:setWorkType])
            mapContract.put(con.id,con);
  //CH03.start
  
         for (WPD_Projects__c proj : Trigger.new) {
         WPD_Contract__c currentContract = null;
         Integer currentClosest;
  
         for (WPD_Contract__c contract : mapContract.values()) {
             if (
                 contract.End_Date__c < proj.Estimated_Project_Start_Date__c ||
                 contract.Work_Type__c != proj.Work_Type__c             ) 
               {
                 continue;
             }
  
            if (currentContract == null) {
                currentContract = contract;
                 currentClosest =proj.Estimated_Project_Start_Date__c.daysBetween(contract.End_Date__c);  
                 continue;
             }
  
             Integer diff = proj.Estimated_Project_Start_Date__c.daysBetween(contract.End_Date__c);
  
             if (diff < currentClosest) {
                 currentContract = contract;
                 currentClosest = diff;
             }
         }
  
         if (currentContract != null) {
             proj.Contract__c = currentContract;
         }
     }
  //CH03.End
    System.debug('mapContract ='+mapContract);
    //condition.    
    for(WPD_Projects__c Proj:trigger.new){
        if(Proj.Estimated_Project_Start_Date__c != null && Proj.Estimated_Project_Start_Date__c >= estimatePlanDate){
            if(Proj.Contract__c!= null && !(Proj.Work_Type__c != null && mapContract.containsKey(Proj.Contract__c) && mapContract.get(Proj.Contract__c) != null
                && Proj.Estimated_Project_Start_Date__c <= mapContract.get(Proj.Contract__c).End_Date__c
                && mapContract.get(Proj.Contract__c).Work_Type__c.ContainsIgnoreCase(Proj.Work_Type__c))){
                    Proj.addError('Work Type/Estimated Project Start Date is not matched for the Contract.');
            }
        }
    }
    //CH01.End
}
pconpcon
You need to modify the line to read
 
proj.Contract__c = currentContract.Id;

The Contact__c field holds an Id and you need to assign it an Id.  Instead of trying to assign the entire object to it.

NOTE: When adding code please use the "Add a code sample" button (icon <>) to increase readability and make it easier to reference.
TheLearnerTheLearner
HI PCON,

Thanks for the reply but our code is not populating the forth coming date contract record while selecting the contract in the lookup , here WPD Contract is parent and WPD Project is child, so i will create the in the WPD Contract with End date, now i will create WPD Project record in that record i need to select the forth coming date WPD COntract record, in case if i select other record i need to display error message which is there in the below. could you check and tell me please, as they are saying, cant we amend our code 

System.debug('mapContract ='+mapContract);
    //condition.    
    for(WPD_Projects__c Proj:trigger.new){
        if(Proj.Estimated_Project_Start_Date__c != null && Proj.Estimated_Project_Start_Date__c >= estimatePlanDate){
            if(Proj.Contract__c!= null && !(Proj.Work_Type__c != null && mapContract.containsKey(Proj.Contract__c) && mapContract.get(Proj.Contract__c) != null
                && Proj.Estimated_Project_Start_Date__c <= mapContract.get(Proj.Contract__c).End_Date__c
                && mapContract.get(Proj.Contract__c).Work_Type__c.ContainsIgnoreCase(Proj.Work_Type__c))){
                    Proj.addError('Work Type/Estimated Project Start Date is not matched for the Contract.');
            }
        }
    }
    //CH01.End