12 votes
Accepted

Is it possible to write Trigger for Before & After DML?

It is quite possible, and in fact recommended (if you need trigger logic on both events). See also: Trigger Frameworks and Apex Trigger Best Practices Triggers (Apex Developer Guide) A common ...
Adrian Larson's user avatar
  • 149k
10 votes
Accepted

APEX Lock and Unlock a Record

== is for equality checks, = is for assignment. As such: If (soAccount.ISLock__c = True){ will assign the value of true to IsLock__c, and this statement will always be true. For this reason, I ...
sfdcfox's user avatar
  • 481k
8 votes

Variable does not exist:Trigger

You have a typo: Trigger.oldMap.get.(cl.id).Initial_Estimate__c should be: Trigger.oldMap.get(cl.id).Initial_Estimate__c You probably also need to apply an explicit cast, like: ((Claim__c) ...
Phil W's user avatar
  • 34.2k
8 votes
Accepted

Best use of After Update Trigger and Before Update

In some cases, using before update to update related records can be problematic, because if those records then try to recursively update the original records, you'll end up with an error you wouldn't ...
sfdcfox's user avatar
  • 481k
7 votes
Accepted

How can I know if it's the last execution of an After Trigger?

Conceivably what you could do is set up a Queueable class. On the first pass through the trigger, instantiate and enqueue it, and then save that in a static variable so you know it's already been ...
Charles T's user avatar
  • 10.9k
7 votes
Accepted

Why do we need to update related records in an after trigger but not before?

If I understand your question correctly, then you actually can take your related record update logic and put it in either a before or after trigger. Salesforce's suggestion is that before triggers ...
Derek F's user avatar
  • 59.4k
6 votes
Accepted

Why won't Apex IF statement work in Production Unit Testing after working in sandbox?

This is a Known Issue that should be fixed in Spring '19. Until then, consider using a List<String> instead (as mentioned in the workaround), or use a Set<Id> instead, which should be more ...
sfdcfox's user avatar
  • 481k
6 votes

How can I know if it's the last execution of an After Trigger?

AFAIK there is no way to do this without adding explicit logic. And that explicit logic is pretty ugly e.g. make the WF and PB set database data or static variables that signal they have run and then ...
Keith C's user avatar
  • 135k
6 votes
Accepted

Non-selective query against large object type (more than 200000 rows) error in trigger

This error has to do with the trigger context, not with the literal location of the query in the source code. It looks like your query is located within a trigger handler, meaning it's executed after ...
David Reed's user avatar
  • 92.1k
6 votes
Accepted

trigger to update field on Account record whenever a new Contact, opportunity record is inserted or deleted

This is because you are using an aggregate query on Contact: for (AggregateResult ar: [Select Count(Id) ContactCount, AccountId from Contact where AccountId IN: AccountIds ...
David Reed's user avatar
  • 92.1k
6 votes
Accepted

Trying to update Values in Standard Object from metaData

opp.get(metaData.get(0).Opportunity_Field_Name__c) = opp.Account.get(metaData.get(0).Account_Field_Name__c); The result of opp.get() is not an lvalue; it does not represent a location to which ...
David Reed's user avatar
  • 92.1k
6 votes

Is my after insert trigger efficient and safe to port to production?

No, you have not properly bulkified your code. Bulkification 101's most important lesson is to aggregate filter values, query before your loop and put records into a Map, and update values after your ...
Adrian Larson's user avatar
  • 149k
6 votes

Best use of After Update Trigger and Before Update

IMHO the first two of your cited reasons are spot on. Your senior dev's brush off of "chance of errors are very [low]" is entirely naive - simple addition of validation rules by admins could ...
Phil W's user avatar
  • 34.2k
5 votes
Accepted

How to avoid this "Too many query rows: 50001"?

You should filter only records that can be matched. You will get some extra results with the below approach, but hopefully fewer than 50k. Also note that NOT IN filters will give you selectivity ...
Adrian Larson's user avatar
  • 149k
5 votes
Accepted

Trigger After Insert Error : Record is read-only

In Apex Code, we use == for comparison, while = is for assignment. This means that you're actually trying to assign a value (true) to a field (acc.Use_School_Contacts__c). The system allows this ...
sfdcfox's user avatar
  • 481k
5 votes
Accepted

How do reference a related object within a Salesforce Trigger?

You need to query the records in the trigger context to lookup the related object fields: trigger objectTrigger on object__c (after insert) { List<relatedObject1__c> ListToInsert = new List&...
Bob Lopez's user avatar
  • 1,471
5 votes
Accepted

Method does not exist or incorrect signature: void containsKey(Id) from the type Set<Id>

For the first one, instead of: affectedCasesByContactId.keyset().containsKey(...) use either the Map method containsKey on the Map: affectedCasesByContactId.containsKey(...) or the Set method ...
Keith C's user avatar
  • 135k
5 votes

How can I know if it's the last execution of an After Trigger?

It sounds like you are looking for an exit event. If doesn't exist yet, but you can vote for the idea, the description of which is pasted below: Since triggers only handle 200 records at a time, it ...
Adrian Larson's user avatar
  • 149k
5 votes

Variable does not exist: StageName what is preventing from saving my apex class which i am calling from apex trigger

Trigger.new is a type of List<sObject> outside of a trigger context. You need to "cast" it back in to a List<Opportunity> for it to work: for(Opportunity oppt : (List<Opportunity>)...
sfdcfox's user avatar
  • 481k
5 votes
Accepted

Insert and return in test data factory

There's actually three different designs: insert and return, return without insert, and optional insert and return. All of the methods are more or less the same, it's mostly a matter of preference. ...
sfdcfox's user avatar
  • 481k
5 votes
Accepted

Need help in optimizing the below helper class

This is typically solved with the aggregate-query-update pattern, which I wrote about a few years back. To do so, we follow three steps. 1 Aggregate Gather the values we want to query. Map<...
sfdcfox's user avatar
  • 481k
5 votes
Accepted

Trigger fires multiple times recursively

if(checkRecursive.runOnce()){ This is perhaps the worst possible pattern to control recursion, specifically because it causes behavior like what you're observing right now. Here, you seem to have an ...
David Reed's user avatar
  • 92.1k
5 votes
Accepted

Do trigger.newMap and trigger.oldMap have the same keySet() in apex after update triggers?

Short answer is: Yes, both maps have the same key set.
Ideasforce's user avatar
  • 1,187
5 votes
Accepted

Issue while looping through child records

If you don't query something, you don't get it from the database. You need to change your query: list<policy__c> pollist = [ SELECT Close_Date__c, (SELECT Role_Name__c, ...
sfdcfox's user avatar
  • 481k
5 votes
Accepted

Trigger.old is returning the new value

You aren't accessing the data in Trigger.old: you're using it to query the database, where the new values have already been persisted because you are in after update context. If you access the sObject ...
David Reed's user avatar
  • 92.1k
5 votes
Accepted

Update trigger for Custom object having 470 custom fields

To easily look for any significant change, you can just: Id[] changedRecordIds = new Id[0]; for(Integer i = 0, s = Trigger.new.size(); i < s; i++) { sObject oldRecord = Trigger.old[i].clone(true, ...
sfdcfox's user avatar
  • 481k
5 votes

Obtaining Prior Record Values Using Asynchronous Flows

As of the time of this answer, you cannot use PRIORVALUE or $Record__Prior in an Asynchronous Path and see the values before the path executes. This is being considered for Run Asynchronously, but not ...
sfdcfox's user avatar
  • 481k
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){ ...
Eric's user avatar
  • 54k
4 votes
Accepted

Unexpected token trigger

This error appears when you try to create an Apex Class with an Apex Trigger code. Those two metadata types are syntax-similar, but they are totally different. Check if you are trying to save this ...
Renato Oliveira's user avatar
4 votes
Accepted

How can we find out the deleted record in After delete trigger?

To get or Query deleted records you need to use ALL ROWS So your query will be List<Object__c> deletedRecords = [SELECT Id FROM Object__c WHERE ID IN :lstTriggerOld AND ID NOT IN : ...
Tushar Sharma's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible