Map Out Your Function

What needs to happen? What data do you need to retrieve using COQL? Order of operations. What updates? Conditional logic?

Using Deluge, and more specifically, COQL (who would have guessed?), we need to then retrieve all Deal records that were closed  (successfully, not lost) in the last 365 days. There are two criteria in there:

  • The deal is Closed Won
  • The closing date is within 365 days of today

We also need a way to group all of those records together by Account. That means we need to actually pull Account info in. We really just need the Name and ID though.

So, let’s pull that info in (JOIN via the Account Name lookup field) and then Order By Account Name. All fields retrieved will be:

  • Deal Name
  • Deal ID
  • Deal Amount
  • Account Name
  • Account ID

We can then use a series of ‘for each’ and ‘if’ statements to add up the Deal Sizes by Account and run the corresponding updates in the CRM Account records (using the IDs we pulled).

Essentially, within the ‘for each’ loop for all of the deals we pull, ordered by account, we need to run a check for if the deal we’re cycling through is for the same account as the previous one.

Set a dealCount variable to 0 and for each deal we cycle through, add 1 to that variable. We also need to check how many deals there are total.

We set a totalAmount variable to 0 outside the loop.

We’ll grab the first (the 0’th) deal and its corresponding account and set that as the account name to start.

Then, we start adding the Deal Amount to the totalAmount variable.

If the account name for a deal is the same:

  • Add the deal amount to it and then reset the account name/id variables

If the account name for a deal is different:

  • run the account update for the previous account using the acount id  and totalAmount variables that you had set in the last loop
  • then, reset the totalAmount variable to 0 and also reset the account name/id variables

If the deal we’re cycling through is the last deal in the list (if dealCount variable = the total deal count):

  • run the update for the current account we’re working with
Back to Lesson