3

I need a method of updating the Property_Rate__c.Account__r.Next_Arrival__c lookup-field with the resulting AccountId of the SOQL query. This should happen when a Property_Rate__c record is updated. Any suggestions?

Note: The query wants to locate the next accountId that is arriving at the same property for a future date.

trigger PopulateNextArrivalOnAccount on Property_Rate__c (after update) 
{

    Set<String> thisAccountId = new Set<String>();
    Set<String> thisPropertyId = new Set<String>();
    Set<Date> thisDate = new Set<Date>();

    for (Property_Rate__c rate : Trigger.new)
    {
        thisAccountId.add(rate.Account__r.Id);
        thisPropertyId.add(rate.Property__r.Id);
        thisDate.add(rate.Date__c);
    }

    List<Account> findNextAccount = [SELECT Id, Next_Arrival__c FROM Account 
    WHERE Property__r.Id IN :thisPropertyId AND Id NOT IN :thisAccountId AND Arrival_Date__c >= :thisDate
    ORDER BY Arrival_Date__c ASC];


 //Help finish trigger here...Below is my attempt at update

   for (Property_Rate__c rate : Trigger.new)
   {
        rate.Account__r.Next_Arrival__c = findNextAccount[0].Id;
   }

}
8
  • Is the Property_Rate__c.Id the same as Property__r.Id and how are the two related to the Property_Rate__c.Account__r? Want to make certain I understand that because they need to be put into a map.
    – crmprogdev
    May 31, 2015 at 18:19
  • Properties and Properties Rates are separate SObjects. Every Property_Rate__c record must be related to a Property. A Property_Rate__c represents the price(rate) of a property on a certain day. Therefore there are 365 property rate records for each Property in a given year. Each Property_Rate record that is "Booked" has an associated account. Think of the Property Rate Object as a reservation calendar, and the account associated to the rate as a reservation. May 31, 2015 at 18:27
  • Now that you know this, you can understand the problem deeper. On any given account, I want to have a look-up field which tells me the next account that will arrive there. Here is where the complexity begins: If Booking 1 arrives on January 1, and Booking 2 arrives on January 10, we know that the Next_Arrival__c field on the first Account should have the Id of the 2nd Account. But if suddenly, we have a new booking for January 5, we need the First Account's Next_Arrival__c field to now show Booking 3's Account ID instead. I will I understand if I've lost you. LOL May 31, 2015 at 18:38
  • Is there anything on the account that links it back to either the Property or the Property_Rate_c? If so, that's the object that needs to be queried. Are you saying the current Account has the next Account's Id stored in it? If so, what's the name of that field?
    – crmprogdev
    May 31, 2015 at 18:39
  • Yes, each Account (booking) contains a required Property look-up field. This is how we know which house the account is staying in. Then we attach the account name to each of the Property Rates (calendar days) that they have booked. May 31, 2015 at 18:43

1 Answer 1

3

You've said you want the trigger to fire when Property_Rate__c is updated. That means the first part of your trigger should look like you have it, but I'd create a map of Property_Rate__c Ids to AccountIds:

trigger PopulateNextArrivalOnAccount on Property_Rate__c (after update) 
{

    Set<String> thisAccountId = new Set<String>();
    Set<String> thisPropertyId = new Set<String>();
    Set<Date> thisDate = new Set<Date>();
    Set<Date>OldNextDates = new Set<Date>(); // Next_Arrival__c
    map<string,string>prop2acctMap = new map<string,string>();
    map<Id,string>pc2propMap = new map<Id,string>();
    map<Id,string>pc2acctMap = new map<Id,string>();
    map<string,date>acct2DatetMap = new map<string,date>();
    map<Id,date>prId2DateMap = new map<Id,date>();

    for (Property_Rate__c rate : Trigger.new)
    {

        prIDs.add(rate.Id);
        thisAccountId.add(rate.Account__r.Id);
        thisPropertyId.add(rate.Property__r.Id);
        thisDate.add(rate.Date__c);
        OldNextDates.add(trigger.oldMap.get(rate.Id).Next_Arrival__c);
        prop2acctMap.put(rate.Property__r.Id,rate.Account__r.Id);
        pc2propMap.put(rate.Id,rate.Property__r.Id);
        pc2acctMap.put(rate.Id,rate.Account__r.Id);
        acct2DatetMap.put(rate.Account__r.Id,rate.Date__c);
        prId2DateMap.put(rate.Id,rate.Date__c);

    }

    // You now have a Set of Property_Rate__c Id's that meet your criteria along  
    // with the other fields you were looking for which have also been filtered
    // plus you have maps of their relationships

    List<Account> findNextAccount = [SELECT Id FROM Account,  
    WHERE Property__r.Id IN :thisPropertyId AND Id NOT IN :thisAccountId AND Arrival_Date__c >= :thisDate
    ORDER BY Arrival_Date__c ASC];


    //Help finish trigger here...Below is my attempt at update

   // its possible there may need to be another for loop here that first loops on 
   // Property Rate, then on Property to get to the Account

   // Each one then needs to be pulled out from the maps in some manner
   // probably using maps of PropRate to Date and Account to Date

   // but the query above needs to relate them first.        

   for (Property_Rate__c rate : Trigger.new)
   {
       if(prId2DateMap.contains(rate.Id) and findNextAccount.contains??){
           prop2acctMap.get(rate.Id).Next_Arrival__c = findNextAccount[0].Id;



       }
   }


}
4
  • Thank you for your reply. How does that help me update the Account that is associated to the Property_Rate__c? My goal is to gather a specific Account Id from a query, and then place that Id in the Account associated to the Property Rate which is being edited. May 31, 2015 at 17:45
  • I apologize, I misread your post. I thought Property_Rate__c was a field that needed filtered; that you only wanted to update when that field changed. I'll revise my answer.
    – crmprogdev
    May 31, 2015 at 18:00
  • see edits and comments and code. I think this should point you more in the right direction to find the correct relationships. It will need to be done using maps and `contains' based on dates, etc. It appears you'll want to update the existing accounts as well as the Property Rate from what I can tell. I'm assuming the Property Rate's Date in trigger.new is the new date and not the old one. This is about the best I can do for you without seeing the relationships myself.
    – crmprogdev
    May 31, 2015 at 19:21
  • Thank you for all of your help. I will do my best to work from here, although much of it is new to me! Alex May 31, 2015 at 22:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .