Skip to content

Instantly share code, notes, and snippets.

@melissajhansen
Created February 4, 2020 03:45
Show Gist options
  • Save melissajhansen/f7f9ce94f3963fc84eadbaefce078d41 to your computer and use it in GitHub Desktop.
Save melissajhansen/f7f9ce94f3963fc84eadbaefce078d41 to your computer and use it in GitHub Desktop.
Sample Recipe Util
public with sharing class RecipeUtil {
//Variables to store Trigger.oldMap & Trigger.newMap
List < Recipe__c > newRecipesList;
Map<ID, Recipe__c> newRecipesMap;
Map<ID, Recipe__c> oldRecipesMap;
//Constructor that takes in the map of old recipe records and new/updated recipe records and assigns them to class variables
public RecipeUtil(List < Recipe__c > newRecipes, Map<ID, Recipe__c> newTriggerRecipes, Map<ID, Recipe__c> oldTriggerRecipes) {
this.newRecipesList = newRecipes;
this.oldRecipesMap = oldTriggerRecipes;
this.newRecipesMap = newTriggerRecipes;
}
//Handler Methods
public void onBeforeInsert() {
// Check for key values
for (Recipe__c r: newRecipesList) {
// TODO: what if it's edited to no longer be a draft?
if (String.isBlank(r.Name) || r.Active_Time__c==null || String.isBlank(r.Description__c) || String.isBlank(r.Active_Time_Units__c) || r.Servings__c==null) {
r.Draft__c = true; //Before Trigger - no DML Needed
}
// get the difficulty
Integer complexity = HelperFunctions.rateRecipeComplexity(r);
if (complexity==3) {
r.Complexity__c = 'Difficult';
} else if (complexity==2) {
r.Complexity__c = 'Moderate';
} else {
r.Complexity__c = 'Simple';
}
}
}
public void onAfterInsert() {
}
public void onBeforeUpdate() {
// Check for key values
for (Recipe__c r: this.newRecipesMap.values()) {
if (String.isBlank(r.Name) || r.Active_Time__c==null || String.isBlank(r.Description__c) || String.isBlank(r.Active_Time_Units__c) || r.Servings__c==null) {
r.Draft__c = true; //Before Trigger - no DML Needed
}
// get the difficulty
Integer complexity = HelperFunctions.rateRecipeComplexity(r);
System.debug('complexity: '+complexity);
if (complexity==3) {
r.Complexity__c = 'Difficult';
} else if (complexity==2) {
r.Complexity__c = 'Moderate';
} else {
r.Complexity__c = 'Simple';
}
}
}
public void onAfterUpdate() {
}
public void onBeforeDelete() {
}
public void onAfterDelete() {
}
public void onUndelete() {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment