0

I have a before trigger to compare dates on entry. I want to get a recond based on an id from a map, How do I achieve this. Below is my example with my question on line

AAddress__c dummy = AccountAddress.get(aAN.id);

Apex

 public void OnBeforeInsert(List<AAddress__c> newAccountAddress){
    //EXECUTE BEFORE INSERT LOGIC
    date dateFilter = date.today().AddDays(- 5);

  Map<ID, AAddress__c> AccountAddress = new Map<ID, AAddress__c>([SELECT Id, Start_Date__c, End_Date__c, Account__c, Address_Type__c  FROM AAddress__c  WHERE Start_Date__c <= TODAY AND End_Date__c >= TODAY AND Status__c = true AND (LastModifiedDate >= :dateFilter OR LastModifiedDate = null)]);                           
      system.debug('AccountAddress: '+AccountAddress);                        

 for (AAddress__c aAN : newAccountAddress){
    AAddress__c dummy = AccountAddress.get(aAN.account__c);
     system.debug('dummy: '+dummy);
    if(aAN.Start_Date__c>= dummy.Start_Date__c && aAN.Start_Date__c <= dummy.End_Date__c ){
    aAN.addError('This startdate interferes with another adress');

    }
    if(aAN.End_Date__c>= dummy.Start_Date__c && aAN.End_Date__c <= dummy.End_Date__c ){
       aAN.addError( 'This enddate interferes with another adress');
    }
}                                                                   
}                                 
1

You can pass in trigger.newmap to this method. If you want old data then trigger.oldmap. By adding an extra parameter to your method. which will be Map<Id,AAddress__c> mymap.

Then you can get the record by id.

If you don't want to change the signature of the method then use below.

Map<Id, AAddress__c> mymap = new Map<Id,AAddress__c>(newAccountAddress);
//...
//...
AAddress__c dummy = mymap.get(aAN.id);
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.