40 votes
Accepted

Dynamically Determine Calling Context?

Just to play devil's advocate, I think you may be somewhat missing the point of a generic utility method. Generic is powerful. Now you seek to pollute that. To some extent, context specific logic ...
Adrian Larson's user avatar
  • 150k
15 votes

Dynamically Determine Calling Context?

There are different ways how to handle that. In the same fashion as you mentioned Trigger.isExecuting (it's boolean and can only have 2 values), you can create a static variable (of type String or ...
Boris Bachovski's user avatar
10 votes

Why does Salesforce has Trigger.isInsert or Trigger.isUpdate when Trigger.old !=Null would have worked?

First of all, you get significantly clearer semantics when you check if (trigger.isInsert) rather than checking if (trigger.old == null). It also adds consistency and completeness with the isBefore ...
Adrian Larson's user avatar
  • 150k
8 votes
Accepted

Can someone clarify when a Trigger executes on a single record vs. multiple in one context?

If the external system is doing a simple REST API call into Salesforce (albeit many of them very quickly together, even in parallel), Salesforce will treat each call as a separate Transaction, and ...
Brian Miller's user avatar
  • 5,212
7 votes

Can triggers tell if a Workflow invoked them?

Triggers (which represent the domain layer in a Separation of Concerns architecture) should not have to know how they were invoked as they could be invoked via Web or REST service DML VF controller ...
cropredy's user avatar
  • 71.3k
6 votes

I need to calculate the sum of amount of all related opportunities of an account and change the status of rating accordingly

If your only concerned is the functionality you described above you don't need to rely on a trigger for doing so. As a best practice you should try solving your tasks with formulas, workflows, ...
Marc Zaharescu's user avatar
6 votes

Dynamically Determine Calling Context?

Converting comment from @identigral to an answer for completeness of the OP's question & future readers. Starting Winter 21 this indeed is possible to determine the calling context of the request ...
Sachin Hooda's user avatar
  • 1,585
6 votes
Accepted

Apex Enterprise Pattern - Advantages Of Selector Layer

The main point of a Selector layer is to standardize queries. In most applications, the same queries tend to be used in various places, so it makes sense to centralize them; you can easily update all ...
sfdcfox's user avatar
  • 491k
5 votes
Accepted

Non-Read-Only field giving error with trigger that it is read-only

This sounds like you're trying to modify a record contained in a trigger context variable (possibly in an After trigger). Taking a look at the documentation on trigger context variables, there is ...
Derek F's user avatar
  • 61.5k
5 votes
Accepted

Retrieving grandparent relationship from Trigger.Old in AfterDelete context

I'm assuming the deleted record would be in the recycle bin, in which case all you need do is use QueryAll in your query. That will allow you to access the records that are in the recycle bin and have ...
crmprogdev's user avatar
5 votes
Accepted

best way to handler trigger Recursive call

So as @cropredy suggested using static map instead this is what I tried. I used static set to solve this issue. I can't say this is the best solution or not but this is working. may me some usecases ...
Ratan Paul's user avatar
  • 22.7k
5 votes

Why does Salesforce has Trigger.isInsert or Trigger.isUpdate when Trigger.old !=Null would have worked?

There are more trigger events than just insert and update. Trigger.old is also available in delete triggers (see Trigger Context Variables), so comparing Trigger.old to null is not sufficient by ...
David Reed's user avatar
  • 92.8k
4 votes
Accepted

Add contact to Account if Account doesnt have any contact

This line a.id = mapAccounts.get(acc.Id); mapAccounts.get(acc.Id) will return Account object in-order to get the Id mapAccounts.get(acc.Id).Id So you code will be like this a.id = mapAccounts.get(...
Ratan Paul's user avatar
  • 22.7k
4 votes
Accepted

Methods to Check if Sobjects are Equal

The Trigger.old and Trigger.new values will always differ; even after running SObject.clone(false, false, false, false), which clears out autonumber fields, and the four audit fields, the ...
sfdcfox's user avatar
  • 491k
4 votes

Methods to Check if Sobjects are Equal

It will likely be inefficient agonizingly slow, but you might want to loop through the results of the SObject.getPopulatedFieldsAsMap() method. public with sharing class ModificationTracker { ...
Adrian Larson's user avatar
  • 150k
4 votes

Why does Salesforce has Trigger.isInsert or Trigger.isUpdate when Trigger.old !=Null would have worked?

You can get Trigger.Old in Update and Delete context, so you cannot guarantee its insert just by adding a null check on Trigger.old. Thats where isInsert ,isDelete,isUpdate,isUndelete comes in ...
Pranay Jaiswal's user avatar
4 votes
Accepted

MailingCountry contact field in before insert trigger

If you have enabeled 'State and Country/Territory Picklists' in your org use the below code to get country details. Please don't add inside for loop. Schema.DescribeFieldResult fieldResult = User....
Reshma's user avatar
  • 4,019
4 votes
Accepted

Complicated Trigger Handler Need to Replicate Trigger Context Values

There seem to be a few fundamental mistakes here. The main one seems to be that you aren't thinking about object instances correctly, but let me start with another one first. Looking at the following ...
Derek F's user avatar
  • 61.5k
3 votes
Accepted

Solving a challenging case using an Apex trigger - Advices please

A trigger isn't the right tool for the job here, at least not for all of the work that you're trying to do. The reasons why a trigger doesn't work completely for this situation are: An Upsert call ...
Derek F's user avatar
  • 61.5k
3 votes
Accepted

Trigger.new List During a Transaction

The collection itself remains the same, but the context records are modified. Consider the following contrived example: Service Layer public with sharing class MyObjectService { public static ...
Adrian Larson's user avatar
  • 150k
3 votes

Can someone clarify when a Trigger executes on a single record vs. multiple in one context?

In addition to @BrianMiller answer, If one or more publishers publish Platform Event Foo__e as follows T(0) - publish 10 Foo__e T(1) - publish 3 Foo__e T(2) - publish 6 Foo__e then the trigger that ...
cropredy's user avatar
  • 71.3k
3 votes
Accepted

Does "Re-evaluate Workflow Rules after Field Change" behave more like a Boolean or a Set-ID for recursion prevention?

The system is that "clever." It reevaluates the rules only for records that were changed (like a Set<Id>). There wouldn't be any point in checking the rules again if the records haven't changed. ...
sfdcfox's user avatar
  • 491k
3 votes
Accepted

Inherited Sharing not respected in trigger context

I tried to do the same test and it worked for me. This is what I created: // Trigger trigger AccountTrigger on Account (before insert) { System.debug(AccountHandler.callAccountUtil()); } // ...
Alba Rivas's user avatar
  • 2,561
3 votes

Looking for documentation that explains when SF sends multiple records to a Trigger

Some examples (to add to David Reed's answer) Data Loader - the number of records presented to the trigger in a single transaction is configured in Data Loader Settings (default = 200) Bulk API (...
cropredy's user avatar
  • 71.3k
3 votes

Looking for documentation that explains when SF sends multiple records to a Trigger

Historically, triggers only worked on one record at a time, back in the days when the API had no bulk capability, either. Bulk capability was added sometime around API 5.0 or so, from what I remember ...
sfdcfox's user avatar
  • 491k
2 votes

Can triggers tell if a Workflow invoked them?

I was searching the same some time ago and it looks like the answer is NO. There is also an idea posted 6 years ago on that but yet not implemented. So it is an old one.
Oles Malkov's user avatar
  • 2,161
2 votes

Data Loader and after update triggers running twice

This is normal behavior for an After Update Trigger that causes workflow to fire. See Item 12 in Triggers and Order of Execution in the Apex Docs. If you want to prevent your trigger from running it'...
crmprogdev's user avatar
2 votes
Accepted

More than 100 opportunities should not be created by a user

Try this ;) Trigger NotMoreThan100Opportunities on Opportunity(before insert){ Integer canCreate = 0; final Integer MAX_ALLOWED_TODAY = 4; List<Opportunity> oppList = [SELECT Id ...
itzmukeshy7's user avatar
  • 3,170
2 votes

More than 100 opportunities should not be created by a user

You can use OwnerId =: userInfo.getUserId() too in the query instead of CreatedById check. Trigger NotMoreThan1000Opportunities on Opportunity(before insert) { //checking for opportunities ...
sfdcFanBoy's user avatar
  • 4,066
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,...
Subash Chandrabose's user avatar

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