8
votes
Is there any way to use only one SOQL for both before and after trigger on the same object?
This is a good time to learn about the Lazy Load pattern. You can write your cache in such a way that you only consume the query if you actually need the data. Now if you make a reference to this Apex ...
8
votes
Accepted
Is there any way to use only one SOQL for both before and after trigger on the same object?
Yes static variables are meant for same purpose .
Create a static list in your helper class and use same in your before trigger and after trigger
public class cachequery {
public static List<...
4
votes
Is there any way to use only one SOQL for both before and after trigger on the same object?
You can do that by storing the results into a static variable outside the trigger - perhaps in your trigger handler class.
For an example we have a trigger on Account and we want to store a list of ...
4
votes
Accepted
Trigger to send Email to contacts
Triggers will not have information about parent or child records .You will have to explicitly query them back .
trigger SendEmailToContacts on Account (after update) {
map<id,Account> ...
4
votes
can we avoid 2nd for loop
Credit for aggregate result trick of aliasing the field to Id goes to sfdcfox
I believe this can all be reduce to:
Trigger Sum on Contact(after insert, after update, after delete, after undelete){
...
3
votes
Accepted
Update map.values() doesn't work
Agreeing with David, you need to add WHERE clause to your query and also the names of your variables should be meaningful.
Like you mentioned, I think you are missing multiple tool records for the ...
3
votes
Before Update Vs after Update Trigger
For your scenario before update makes more sense as you are not updating any other related record and in before update scenario you don't have to explicitly write update statement.
Also if we update ...
3
votes
Accepted
Question regarding "RunOnce" Booleans on AfterUpdate methods in a Salesforce Transaction
Very astute question! The answer is number 2. You want to put your boolean in your trigger where the class is called from. You'll need a trigger utility class to hold the boolean. This is a large part ...
2
votes
SObject row was retrieved via SOQL without querying the requested field: Account.Wholesaler__r:
There were so many issues with your code
SOQL Query inside for loop
DML Statement inside for loop
You code was not bulkified for queries
You were initialising your bill variable at the second line, ...
2
votes
Accepted
Trigger to make Same changes in child object
This code is same as your code but its optimized than your code
/* Here I mention
** Parent --> Subject Record
** Child --> subSubject Record
*/
trigger Clone on Subject__c(after insert,...
2
votes
Accepted
trigger event using after insert
Try to use before insert so you can skip the extra DML operation
Just check the field is null then increment the Percentage like you did for 2 fields
trigger PercentageTrigger on Percentile__c (...
2
votes
Is there any way to use only one SOQL for both before and after trigger on the same object?
I'll go one more on @AdrianLarson answer. The lazy load approach, which makes plenty of sense to minimize SOQLs until needed can sometimes run into trouble in test classes where your mocking of ...
2
votes
Update map.values() doesn't work
List<Tool__c> toUpdate = [SELECT Id, Comment__c, Mopp__r.Opportunity__c FROM Tool__c];
Running an unbounded query like this (no WHERE clause) is risky with regard to several limits, as well as ...
2
votes
Can't call a Subflow from a record triggered flow?
There is a clear indication in the documentation what kind of core action are available in record trigged flow. Unfortunately, subflow element is not supported
2
votes
Accepted
Why After trigger cannot update same record?
Apex triggers allow you to choose to run code at specific points in a database operation where the semantics are well defined:
before trigger: before the database has been updated
after trigger: ...
1
vote
Salesforce flow: prior values in after update
You cannot react to changes in formula fields using any form of Salesforce automation, because formula fields do not have persistent values. The value of a formula field is calculated at the time of ...
1
vote
Accepted
Update the parent case when the child case is closed only on certain recortypes
Apex always requires that you declare variables you wish to use. The variable commentBody is not declared, so you cannot assign to it here:
commentBody = c.CommentBody ;
CaseComment cc = new ...
1
vote
Accepted
Can't call a Subflow from a record triggered flow?
It looks like the Winter '22 release will support subflow elements in record-triggered flows. See the Winter '22 release notes here: https://help.salesforce.com/s/articleView?id=release-notes....
1
vote
Accepted
Problem with IN Clause in soql
By the way you have tagged your message it seems to me that you are running that code in an after delete trigger context. If the Ids you are using are the ones that are deleted, they are no longer ...
1
vote
Update map.values() doesn't work
You should check in the second for loop that if Comment__c of custom object OppDesc__c is actually null or blank or have value . If Comment__c is null then actually there is nothing to update.
Add a ...
1
vote
After update trigger fires twice, first with no updated info, second with info
Solved. This issue was a combination of pointing to the wrong field and multiple after update triggers firing. I pointed to the correct "Description" field on the event rather than a custom formula ...
1
vote
Read-only exception in trigger
As you are running in before insert so you can remove the last DML in your code
for(task t2:trigger.new){
string descr = t2.description == null ? 'blank' : t2.description;
t2....
1
vote
Accepted
How to avoid soql inside for loop in apex class?
1.In order to tackle the evoking on every update event issue, I would suggest to use, trigger.oldMap values so that you check if the oldValue is different to the new value and it corresponds to your ...
1
vote
Accepted
Is there an Alternative of 'After Delete' Trigger?
If deletion of a child sobject should update a parent with something that can't be calculable from a RSF on the parent (like a count of details, or sum of detail amounts), you are faced with:
Option ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
after × 25trigger × 15
apex × 12
update × 9
visual-workflow × 3
after-trigger × 3
soql × 2
case × 2
before-trigger × 2
delete × 2
map × 1
metadata × 1
event × 1
field-update × 1
future × 1
campaign-member × 1
triggercontext × 1
order-of-execution × 1
recursion × 1
child-to-parent × 1
reference × 1
boolean × 1
recordtriggeredflow × 1
subflow × 1