function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
JN22JN22 

Compare Child Record Fields

Hello,

 

I have a trigger on a custom object (Contract_Summary__c) that populates a custom lookup field on the associated Account record with the Contract_Summary__c record as long as it is a particular type.  I would now like to add a condition that will only overwrite the value in the lookup field on the Account if the Current_Effective_Date__c field on the new Contract_Summary__c record is greater than the Current_Effective_Date__c field on the Contract_Summary__c record that is already populated in the Account custom lookup field.  I'm guessing this is not difficult to accomplish, but I am stumped.  My code is below if anyone can help:

 

 

trigger Contract2Account on Contract_Summary__c (after insert, after update)
{
Try
{
Map<Id,Account> oppMap = new Map<Id,Account>();
Set<id> Ids = new Set<id>();

if(trigger.isInsert || trigger.isUpdate)
{

for (Contract_Summary__c prgm : Trigger.new)
{
if((prgm.Type_of_Contract__c == 'Renewal' || prgm.Type_of_Contract__c == 'Initial MSA') && (prgm.Current_Effective_Date__c > prgm.Account_Name__r.Contract_Summary__r.Current_Effective_Date__c || prgm.Account_Name__r.Contract_Summary__c == null))
Ids.add(prgm.Account_Name__c);
}

Map<id,Account> acctMap2 = new Map<id,Account>([Select Id,Name,Contract_Summary__c from Account Where Id in :Ids]);

for (Contract_Summary__c prgm2 : Trigger.new)
{
Account a = acctMap2.get(prgm2.Account_Name__c);
a.Contract_Summary__c = prgm2.Id;

oppMap.put(a.id,a);
}
update oppMap.values();
}

}

catch(Exception e)
{}

}

 

 

Thanks.

Best Answer chosen by Admin (Salesforce Developers) 
vishal@forcevishal@force

You can modify your query on Account where you're putting values in the map. Add the Current_Effective_Date__c field in your query. And then compare it with Contract_Summary__c record.

 

Make the following changes:

 

Map<id,Account> acctMap2 = new Map<id,Account>([Select Id,Name,Contract_Summary__c, Contract_Summary__r.Current_Effective_Date__c from Account Where Id in :Ids]);

for (Contract_Summary__c prgm2 : Trigger.new)
{

if(acctMap2.containsKey(prgm2.Account_Name__c) && acctMap2.get(prgm2.Account_Name__c).Contract_summary__r.Current_effective_Date__c < prgm2.Current_Effective_Date__c) {

     Account a = acctMap2.get(prgm2.Account_Name__c);

     a.Contract_Summary__c = prgm2.Id;

     oppMap.put(a.id,a);

     }
}

All Answers

vishal@forcevishal@force

You can modify your query on Account where you're putting values in the map. Add the Current_Effective_Date__c field in your query. And then compare it with Contract_Summary__c record.

 

Make the following changes:

 

Map<id,Account> acctMap2 = new Map<id,Account>([Select Id,Name,Contract_Summary__c, Contract_Summary__r.Current_Effective_Date__c from Account Where Id in :Ids]);

for (Contract_Summary__c prgm2 : Trigger.new)
{

if(acctMap2.containsKey(prgm2.Account_Name__c) && acctMap2.get(prgm2.Account_Name__c).Contract_summary__r.Current_effective_Date__c < prgm2.Current_Effective_Date__c) {

     Account a = acctMap2.get(prgm2.Account_Name__c);

     a.Contract_Summary__c = prgm2.Id;

     oppMap.put(a.id,a);

     }
}

This was selected as the best answer
JN22JN22

Vishal,

 

That did the trick, thanks so much for your help!!!