Last active
April 20, 2023 22:11
-
-
Save asmitakhare/adc9e47189c5035913257fea6ece38ca to your computer and use it in GitHub Desktop.
WeekFourHomework
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public with sharing class RecipeUtil { | |
public static String getComplexity(Recipe__c recipe){ | |
//Calling out to a method on the HelperFunctions class called rateRecipeComplexity | |
Integer intRating = HelperFunctions.rateRecipeComplexity(recipe); | |
String rating; | |
//: If the method returns a 3, that’s ‘Difficult’, a 2 is ‘Moderate’ and a 1 is ‘Simple’ | |
if (intRating == 3) { | |
rating = 'Difficult'; | |
} else if (intRating == 2) { | |
rating = 'Moderate'; | |
} else { | |
rating = 'Simple'; | |
} | |
return rating; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trigger RecipeTrigger on Recipe__c( | |
before insert, | |
after insert, | |
before update, | |
after update | |
) { | |
if (Trigger.isbefore && (Trigger.isUpdate || Trigger.isInsert)) { | |
RecipeTriggerHandler.beforeRecipeInsertOrUpdate(Trigger.new); | |
} | |
if (Trigger.isAfter && Trigger.isUpdate) { | |
RecipeTriggerHandler.afterRecipeUpdate(Trigger.old); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public with sharing class RecipeTriggerHandler { | |
//created a method before recipe insert or update to check if it is missing any field value | |
public static void beforeRecipeInsertOrUpdate(List<Recipe__c> listRec) { | |
//looping through list of recipe and checking if any of the field missing valu | |
for (Recipe__c recipe : listRec) { | |
if ( | |
recipe.Name == null || | |
recipe.Active_Time__c == null || | |
recipe.Description__c == null || | |
recipe.Active_Time_Units__c == null || | |
recipe.Servings__c == null | |
) { | |
//if field is missing value it will marked the Draft checkbox | |
recipe.Draft__c = true; | |
} | |
else | |
{ | |
recipe.Draft__c = false; | |
} | |
// Complexity field from Recipe object is getting the numerical rating back from the method rateRecipeComplexity. | |
// and use it to fill in the Complexity__c field. | |
// If the method returns a 3, that’s ‘Difficult’, a 2 is ‘Moderate’ and a 1 is ‘Simple’ | |
recipe.Complexity__c = RecipeUtil.getComplexity(recipe); | |
} | |
} | |
public static void afterRecipeUpdate(List<Recipe__c> listRec) { | |
List<Recipe_Usage__c> listUsage = [ | |
SELECT Recipe__r.Id, Recipe__r.Name, Cookbook__r.Id, Cookbook__r.OwnerId | |
FROM Recipe_Usage__c | |
WHERE Recipe__c IN :listRec AND Recipe__r.Draft__c = FALSE | |
WITH SECURITY_ENFORCED | |
]; | |
List<Task> lstTask = new List<Task>(); | |
for (Recipe_Usage__c ru : listUsage) { | |
Task t = new Task(); | |
t.OwnerId = ru.Cookbook__r.OwnerId; | |
t.ActivityDate = Date.Today().addDays(7); | |
t.Subject = 'Please Review this for Recipe ' + ru.Recipe__r.Name; | |
lstTask.add(t); | |
} | |
//checking security permission | |
if (Schema.SObjectType.Task.isCreateable()) { | |
insert lstTask; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment