-
Notifications
You must be signed in to change notification settings - Fork 0
Feedback #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: feedback
Are you sure you want to change the base?
Feedback #1
Changes from 8 commits
04b8e4b
2ee852d
c3ee2b6
1b29c53
e9cadfb
0d34c0e
d395a08
21ba6b9
6c6c3f7
fee9215
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| trigger AccountTrigger on Account (before insert, after insert) { | ||
|
|
||
| if (Trigger.isBefore && Trigger.isInsert) { | ||
|
|
||
| for (Account acc : Trigger.new) { | ||
|
|
||
| // Question 1 solution | ||
| if (acc.Type == null) { acc.Type = 'Prospect'; } | ||
|
|
||
| // Question 2 solution | ||
| if (!String.isBlank(acc.ShippingStreet)) { acc.BillingStreet = acc.ShippingStreet; } | ||
| if (!String.isBlank(acc.ShippingCity)) { acc.BillingCity = acc.ShippingCity; } | ||
| if (!String.isBlank(acc.ShippingState)) { acc.BillingState = acc.ShippingState; } | ||
| if (!String.isBlank(acc.ShippingPostalCode)) { acc.BillingPostalCode = acc.ShippingPostalCode; } | ||
| if (!String.isBlank(acc.ShippingCountry)) { acc.BillingCountry = acc.ShippingCountry; } | ||
|
|
||
| // Question 3 solution | ||
| if (acc.Fax != null && acc.Phone != null && acc.Website != null) {acc.Rating = 'Hot'; } | ||
| } | ||
| } else if (trigger.isAfter && trigger.isInsert) { | ||
| // Question 4 solution | ||
| List<Contact> newContacts = new List<Contact>(); | ||
| for (Account acc : Trigger.new) { | ||
| Contact con = new Contact(); | ||
| con.LastName = 'DefaultContact'; | ||
| con.Email = 'default@email.com'; | ||
| con.AccountId = acc.Id; | ||
| newContacts.add(con); | ||
| } | ||
| insert newContacts; | ||
| } | ||
| } | ||
|
Comment on lines
+1
to
+42
Check warningCode scanning / PMD As triggers do not allow methods like regular classes they are less flexible and suited to apply good encapsulation style. Therefore delegate the triggers work to a regular class (often called Trigger handler class). See more here: <https://developer.salesforce.com/page/Trigger_Frameworks_and_Apex_Trigger_Best_Practices> Warning
Avoid logic in triggers
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <ApexTrigger xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
| <apiVersion>60.0</apiVersion> | ||
| <status>Active</status> | ||
| </ApexTrigger> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| trigger MyFirstTrigger on Opportunity (before insert, after insert) { | ||
| System.debug('Hello World!'); | ||
| } | ||
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <ApexTrigger xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
| <apiVersion>60.0</apiVersion> | ||
| <status>Inactive</status> | ||
| </ApexTrigger> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| trigger OpportunityTrigger on Opportunity (before delete, before update) { | ||
| if (Trigger.isBefore && Trigger.isDelete) { | ||
| // Question 6 solution | ||
| Set<Id> accIds = new Set<Id>(); | ||
| for (Opportunity opp : Trigger.old) { | ||
| if (opp.AccountId != null) { | ||
| accIds.add(opp.AccountId); | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is good. A different approach would be to add the ids where the Opp = closed won. So you only query for those. |
||
| } | ||
|
|
||
| Map<Id, Account> accMap = new Map<Id, Account>([SELECT Id, Industry FROM Account WHERE Id IN :accIds]); | ||
|
||
|
|
||
| for (Opportunity opp : Trigger.old) { | ||
| if (opp.StageName == 'Closed Won') { | ||
| Account acc = accMap.get(opp.AccountId); | ||
| if (acc != null && acc.Industry == 'Banking') { | ||
| opp.addError('Cannot delete closed opportunity for a banking account that is won'); | ||
| } | ||
|
||
| } | ||
| } | ||
| } | ||
|
|
||
| if (Trigger.isBefore && Trigger.isUpdate) { | ||
| // Question 5 solution | ||
| for (Opportunity opp : Trigger.new) { | ||
| if (opp.Amount < 5000) { | ||
| opp.addError('Opportunity amount must be greater than 5000'); | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good. I would check less than or equal too. My assumption since the error message says that the amount must be greater than |
||
| } | ||
|
|
||
| // Question 7 solution | ||
| Set<Id> accIds = new Set<Id>(); | ||
| for (Opportunity opp : Trigger.new) { | ||
| accIds.add(opp.AccountId); | ||
| } | ||
|
|
||
| Map<Id, Contact> conIdMap = new Map<Id, Contact>(); | ||
| for (Contact con : [SELECT Id, AccountId FROM Contact WHERE AccountId IN :accIds AND Title = 'CEO']) { | ||
| conIdMap.put(con.AccountId, con); | ||
| } | ||
|
|
||
| for (Opportunity opp : Trigger.new) { | ||
| Contact ceoCon = conIdMap.get(opp.AccountId); | ||
| if (ceoCon != null) { | ||
|
||
| opp.Primary_Contact__c = ceoCon.Id; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <ApexTrigger xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
| <apiVersion>60.0</apiVersion> | ||
| <status>Active</status> | ||
| </ApexTrigger> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is really good! I typically hate seeing the curly braces with the logic on the same line as a predicate condition. But in this case, I think it made your mode much more readable. I like it.
Another thing you could explore is the use of the null coalescing operator to assign a default value if something evaluates to null