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
Jaci BantonJaci Banton 

Field changes based on SOQL trigger

Hi there!

Just starting to learn and trying to figure out how to update a pick list field using a trigger, based on a related list.
If an Account does not have a record in the related list whose date field is within the past year, I want to set the pick list field to 'Inactive'.

1. If you have advice towards when to trigger this, please let me know your thoughts.
2. I am trying to sort the SOQL list and then set the pick list field based on the last date in the list, but can't seem to get it. Do you know of a better way to go about this or how to make this work? Thank You in advance!
trigger IsActive on Account (before update, before insert) { 

    if(Trigger.isAfter || Trigger.isUpdate){

        List<Integer> dates1 = new list<Integer>();{
        for(Account ac : [SELECT Id,Name,(SELECT Event_End_Date__c FROM Experiences_Associated_With_Delivery__r Order By Event_End_Date__c desc) FROM Account Where id IN :Trigger.new]){

            if(dates1.get(dates1.size()-1) > 1) {
            Account.Activity_Status__c = Inactive; 
            } else { Account.Activity_Status_c = Active;
          }
        }
    }
}
}

 
Jaci BantonJaci Banton
Missed the date calculation - was going to insert:
IF(
  AND(
    MONTH( date ) = 2,
    DAY( date ) = 29,
    NOT( 
      OR( 
        MOD( YEAR( date ), 400 ) = 0, 
        AND( 
          MOD( YEAR( date ), 4 ) = 0,
          MOD( YEAR( date ), 100 ) != 0
        )
      )
    ) 
  ),
  DATE( YEAR( date ) + num_years, 3, 1),
  DATE( YEAR( date ) + num_years, MONTH( date ), DAY( date ) )
)

 
Ankit Solanki 6Ankit Solanki 6
you can do it without trigger ,if using trigger then check date with condition of less than today's date.Here, you are using date that is only integer not date field.
Ankit Solanki 6Ankit Solanki 6
HI @Jaci Banton, If it's solved or still open ?