+ Start a Discussion
Jim MontgomeryJim Montgomery 

get the MAX value of a related fields in apex trigger

I need to get the MAX value of the extended_maintenance_months__c field on the R00N70000001yUfBEAU__r relationship (proposal_line_item__c), then flag that on the proposal.

trigger HasExtendedMaintenance on Apttus_Proposal__Proposal_Line_Item__c (after insert,after update) {
Map<ID, Apttus_Proposal__Proposal__c> parentProposal = new Map<ID, Apttus_Proposal__Proposal__c>();
    
    List<Id> listIds = new List<Id>();
    List<Id> LineItems = new List<Id>();

    for (Apttus_Proposal__Proposal_Line_Item__c childObj : Trigger.new) {
        listIds.add(childObj.Apttus_Proposal__Proposal__c);
        LineItems.add(childObj.Id);
    }
  
    parentProposal = new Map<Id, Apttus_Proposal__Proposal__c>([SELECT id,Extended_Maintenance_months__c,(Select id,extended_maintenance_months__c from R00N70000001yUfBEAU__r where Extended_Maintenance_Months__c > 0 ) FROM Apttus_Proposal__Proposal__c WHERE ID IN :listIds]);
    
    for (Apttus_Proposal__Proposal_Line_Item__c ProposalLineItem: Trigger.new){
         Apttus_Proposal__Proposal__c myParentProposal = parentProposal.get(ProposalLineItem.Apttus_Proposal__Proposal__c);
        if(parentProposal.containsKey(ProposalLineItem.Apttus_Proposal__Proposal__c) && parentProposal.get(ProposalLineItem.Apttus_Proposal__Proposal__c).R00N70000001yUfBEAU__r.size() > 0)
        {
            myParentProposal.extended_maintenance_months__c = parentProposal.get(ProposalLineItem.Apttus_Proposal__Proposal__c).R00N70000001yUfBEAU__r.size();
            myParentProposal.extended_maintenance__c = True;         
        }    
       else
       {
       myParentProposal.extended_maintenance_months__c =0;
       myParentProposal.request_extended_maintenance_approval__c = false;
       myParentProposal.extended_maintenance_Approved__c = false;
       myParentProposal.extended_maintenance__c = False;
       }
       }

               update parentProposal.values();
}
Steven NsubugaSteven Nsubuga
Try this tweak
trigger HasExtendedMaintenance on Apttus_Proposal__Proposal_Line_Item__c (after insert,after update) {
Map<ID, Apttus_Proposal__Proposal__c> parentProposal = new Map<ID, Apttus_Proposal__Proposal__c>();
    
    List<Id> listIds = new List<Id>();
    List<Id> LineItems = new List<Id>();
	List<Apttus_Proposal__Proposal__c> apttusProposals = new List<Apttus_Proposal__Proposal__c>();

    for (Apttus_Proposal__Proposal_Line_Item__c childObj : Trigger.new) {
        listIds.add(childObj.Apttus_Proposal__Proposal__c);
        LineItems.add(childObj.Id);
    }
  
    parentProposal = new Map<Id, Apttus_Proposal__Proposal__c>([SELECT id,Extended_Maintenance_months__c,(Select id,extended_maintenance_months__c from R00N70000001yUfBEAU__r where Extended_Maintenance_Months__c > 0 ) FROM Apttus_Proposal__Proposal__c WHERE ID IN :listIds]);
    
    for (Apttus_Proposal__Proposal_Line_Item__c ProposalLineItem: Trigger.new){
         Apttus_Proposal__Proposal__c myParentProposal = parentProposal.get(ProposalLineItem.Apttus_Proposal__Proposal__c);
        if(parentProposal.containsKey(ProposalLineItem.Apttus_Proposal__Proposal__c) && parentProposal.get(ProposalLineItem.Apttus_Proposal__Proposal__c).R00N70000001yUfBEAU__r.size() > 0)
        {
            myParentProposal.extended_maintenance_months__c = parentProposal.get(ProposalLineItem.Apttus_Proposal__Proposal__c).R00N70000001yUfBEAU__r.size();
            myParentProposal.extended_maintenance__c = True;         
        }    
       else
       {
       myParentProposal.extended_maintenance_months__c =0;
       myParentProposal.request_extended_maintenance_approval__c = false;
       myParentProposal.extended_maintenance_Approved__c = false;
       myParentProposal.extended_maintenance__c = False;
       }
	   apttusProposals.add(myParentProposal);
       }

               update apttusProposals;
}

 
Jim MontgomeryJim Montgomery
I get a duplicate ID in list error.
Steven NsubugaSteven Nsubuga
This should fix it.
trigger HasExtendedMaintenance on Apttus_Proposal__Proposal_Line_Item__c (after insert,after update) {
Map<ID, Apttus_Proposal__Proposal__c> parentProposal = new Map<ID, Apttus_Proposal__Proposal__c>();
    
    List<Id> listIds = new List<Id>();
    List<Id> LineItems = new List<Id>();
	List<Apttus_Proposal__Proposal__c> apttusProposals = new List<Apttus_Proposal__Proposal__c>();
	Set<Apttus_Proposal__Proposal__c> apttusProposalsSet = new Set<Apttus_Proposal__Proposal__c>();

    for (Apttus_Proposal__Proposal_Line_Item__c childObj : Trigger.new) {
        listIds.add(childObj.Apttus_Proposal__Proposal__c);
        LineItems.add(childObj.Id);
    }
  
    parentProposal = new Map<Id, Apttus_Proposal__Proposal__c>([SELECT id,Extended_Maintenance_months__c,(Select id,extended_maintenance_months__c from R00N70000001yUfBEAU__r where Extended_Maintenance_Months__c > 0 ) FROM Apttus_Proposal__Proposal__c WHERE ID IN :listIds]);
    
    for (Apttus_Proposal__Proposal_Line_Item__c ProposalLineItem: Trigger.new){
         Apttus_Proposal__Proposal__c myParentProposal = parentProposal.get(ProposalLineItem.Apttus_Proposal__Proposal__c);
        if(parentProposal.containsKey(ProposalLineItem.Apttus_Proposal__Proposal__c) && parentProposal.get(ProposalLineItem.Apttus_Proposal__Proposal__c).R00N70000001yUfBEAU__r.size() > 0)
        {
            myParentProposal.extended_maintenance_months__c = parentProposal.get(ProposalLineItem.Apttus_Proposal__Proposal__c).R00N70000001yUfBEAU__r.size();
            myParentProposal.extended_maintenance__c = True;         
        }    
       else
       {
       myParentProposal.extended_maintenance_months__c =0;
       myParentProposal.request_extended_maintenance_approval__c = false;
       myParentProposal.extended_maintenance_Approved__c = false;
       myParentProposal.extended_maintenance__c = False;
       }
	   apttusProposalsSet.add(myParentProposal);
       }
               apttusProposals.addAll(apttusProposalsSet);
               update apttusProposals;
}