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
cbrocbro 

update Trigger to only update date if date field not equal to null

What I'd like to do is have this trigger update the Asset "Inactive_Date__c" with the Contract "End_Date__c" ONLY if Contract "End_Date__c" != null.  That is, only if there is an End Date on the Contract should the Inactive Date on the Asset be updated.  The Inactive Date should never be changed if the End Date on the Contract is 'null'.

 

Can anyone help me with this?

 

 

  for(Asset a: [Select Contract__c, Contract__r.End_Date__c, Inactive_Date__c from Asset where Contract__c =: Trigger.newMap.keyset() ])
        {
            System.debug('++++ querying associated assets' );
            a.Inactive_Date__c = Trigger.newMap.get(a.Contract__c).End_Date__c; 
            System.debug('++++ updating inactive date on assets with contract inactive date = ' + a.Inactive_Date__c);
            assetsToUpdate.add(a);
        } 
      

 

 

 

Thanks,
Chris

 

Best Answer chosen by Admin (Salesforce Developers) 
AneeshaAneesha

Try this

 

for(Asset a: [Select Contract__c, Contract__r.End_Date__c, Inactive_Date__c from Asset where Contract__c =: Trigger.newMap.keyset() ])
        {
            System.debug('++++ querying associated assets' );
if(Trigger.newMap.get(a.Contract__c).End_Date__c != null){

            a.Inactive_Date__c = Trigger.newMap.get(a.Contract__c).End_Date__c; 
            System.debug('++++ updating inactive date on assets with contract inactive date = ' + a.Inactive_Date__c);
            assetsToUpdate.add(a);
}
        } 

 

All Answers

AneeshaAneesha

Try this

 

for(Asset a: [Select Contract__c, Contract__r.End_Date__c, Inactive_Date__c from Asset where Contract__c =: Trigger.newMap.keyset() ])
        {
            System.debug('++++ querying associated assets' );
if(Trigger.newMap.get(a.Contract__c).End_Date__c != null){

            a.Inactive_Date__c = Trigger.newMap.get(a.Contract__c).End_Date__c; 
            System.debug('++++ updating inactive date on assets with contract inactive date = ' + a.Inactive_Date__c);
            assetsToUpdate.add(a);
}
        } 

 

This was selected as the best answer
cbrocbro

This is working just fine.


So simple...  thank you...

 

Now, of course, I realize I need another condition.  

 

If Asset.Inactive_Date__c !: null AND Asset.Inactive_Date__c <= Contract.EndDate, then it shouldn't update that Asset's Inactive_Date__c).

 

That is, if the Asset already has an Inactive_Date__c that is less than or equal to Contract.End_Date__c, then Contract.End_Date__c should not update the Asset.

 

Another way of putting it:  (Contract.End_Date__c) should only update the Asset.Inactive_Date__c if Asset.Inactive_Date__c > Contract.End_Date__c OR Asset.Inactive_Date__c is equal to null.

 

Does that make sense?

 

Is this close?  See my changes directly after the "if" statement you added. 

 

for(Asset a: [Select Contract__c, Contract__r.End_Date__c, Inactive_Date__c from Asset where Contract__c =: Trigger.newMap.keyset() ])
        {
            System.debug('++++ querying associated assets' );
                if(Trigger.newMap.get(a.Contract__c).End_Date__c != null && (Asset.Inactive_Date__c > a.Contract__c.End_Date__c || Asset.Inactive_Date__c == null))
                {
                    a.Inactive_Date__c = Trigger.newMap.get(a.Contract__c).End_Date__c; 
                    System.debug('++++ updating inactive date on assets with contract inactive date = ' + a.Inactive_Date__c);
                    assetsToUpdate.add(a);
                }
        } 

 

 

 

Thanks,