#Be A Builder – Add Automation


Day 7 – #Be A Builder – Define Permissions

Day 7 – Add Automation

  • Key Takeaways
    • Use automation to reduce manual data entry, create reminders and save time
    • Consider the automation funnel when choosing the right automation tool
    • Process builder is always first consideration
  • Business Requirements: Automatically Populate data to maintain good data quality
    • Start Date
    • End Date
    • How many projects have been completed for each account
  • Automation Funnel
    1. Can this be done with Process Builder
    2. Can this be done with a flow
    3. Can this be done with code

Process Builder:

  • Object – Project
    • New or Edited
    • Nodes
      • NEW:
        • Business Requirement: Automatically populate Start Date
        • isnew()
          • Immediate Action
            • Field Update – Start Date
              • Formula: Today ()
      • Status is closed
        • Business Requirement: Automatically populate End Date
        • Status equals “Closed”
          • Immediate Action
            • FIeld Update – End Date
              • Formula: Today()

Flow:

Possible Solution – Each time a project is closed a flow could be used to update and count all closed project related to the current project’s account. Populate the count into the Completed Project field.

  • Collection Sobject – Projects related to Account
  • Variable -varCompletedProjects
  • Loop
    • Count each record n collection
  • Fast Update – Account Completed Projects

Code:

Possible Solution – After Update Trigger: Each time a project is edited count all closed project related to the current project’s account and populate the count into the Completed Projects field.

/**
* Populate account field Completed_Projects__c with count of related projects with status=closed
**/
trigger ProjectTrigger on Project__c (after delete, after insert, after update) {

Set accountIds = new Set();

if (Trigger.isUpdate || Trigger.isDelete) {
for (Project__c proj : Trigger.old){
accountIds.add(proj.Account__c);
}
}

Map<Id,Account> accountMap = new Map<Id,Account>(
[SELECT id, Completed_Projects__c,
(SELECT Id, Status__c, End_Date__c
FROM Projects__r
WHERE Status__c = ‘closed’)
FROM Account
WHERE Id IN :accountIds]);

if(!accountMap.isEmpty()){
for (Account acc : accountMap.values()) {
Integer completeProj = 0;
for (Project__c p : acc.Projects__r){
if (p.Status__c == ‘closed’){
completeProj++;
}
}
accountMap.get(acc.Id).Completed_Projects__c = completeProj;
}
}

if (accountMap.size() > 0) {
update accountMap.values();
}

}