30

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 belongs in the calling context. I understand that sometimes, reality doesn't play nice, and you may need to do source dependent processing. But a lot of times, ...


21

For records that already exist, it will fire updates. For records that are new, it will fire inserts. As a result, an upsert can cause both insert and update triggers to fire if you have both new and existing records in your upsert, but they both won't fire for the same record.


16

The first key to Bulkification is removing SOQL and DML from loops (there are others, but this seems to be your biggest issue above). You need to leverage collections to help you do this. While I won't rewrite your trigger for you, I will give you some starting points. In your trigger above you have a SOQL query for every record in trigger.new. So for ...


14

It is general best practice to consolidate. The main advantage here is control of execution. When you have 2 triggers on the account object, there is no way to know or control which fires first. If however, you move those triggers into a utility class, called by a single account trigger, you now can control the order in which they are fired. 2 Trigger ...


12

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 Integer or Enum, whatever type that makes sense) inside your utility class, to which you can assign a different value from each of the callers. For an example if ...


11

You can do this by comparing the current value of the IsMain field with the value in the trigger.oldMap collection. This will tell you if the field has changed. Something like this will work trigger FileUploadedTrigger on File_uploaded__c (after update) { list<File_uploaded__c> changedIsMainList = new list<File_uploaded__c>(); for(...


11

Suppose you have a custom object Custom_obj__c Trigger.New means it is a List<Custom_obj__c> and Trigger.NewMap means it is a map<Id, Custom_obj__c> In before insert context your Trigger.NewMap will always be null because in before context records are not submitted to the database, so the Id is not generated yet. That's why in before insert ...


9

There is no such thing as passing to a trigger; a trigger fires on a DML event. However, DML operations operate on a transaction chunk of 200 records at a time. So, let's say for example, you have a Visualforce page that edits 200 records, and an update trigger on that object, your trigger would operate on all 200 records at once, in one chunk. Another ...


9

A longstanding trick that I've seen used time and time again to work around MIXED_DML_OPERATION when doing test data setup is to wrap the setup DML operation in a System.runas(new User(Id = UserInfo.getUserId())){ insert myTestUser; } The System.runAs(User) method is only available inside of tests, but by setting up a new context albeit with the ...


9

Trigger.new can be accessed in insert and update triggers, and the records can only be modified in before triggers. Trigger.old can be accessed in update and delete triggers. Here all the full details of all of the Trigger Context Variables taken from the Trigger Context Variables documentation. All triggers define implicit variables that allow ...


9

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 and isAfter attributes, which you could not check by inspecting the collection values. Second of all, checking for the presence of trigger.new and trigger.old ...


8

In brief Trigger.isExecuting determines if the context is trigger or Visualforce. If it returns true context is trigger otherwise context is Visualforce or other. In your case Trigger.isExecuting will return true. Simple ex: Create a class: public class TriggerContextDemo { public Boolean updateContact(Contact[] conList) { // Will not update ...


7

I don't think you want the ContentUtility check to be at the Trigger level - I typically refer to it in my helper class, or even at the individual method level in that class. There may well be instances where you want one method to run twice if something was updated, but not others. So I would move the check for TriggerContextUtility into the helper class ...


7

Two important things to keep in mind for bulkifying code is to: Avoid SOQL inside for loops Performing DML operations such insert/updates on a List of objects instead of a single object inside a loop. Move your SOQL statement outside the loop and store the data in a List/Map. Also add the objects that need to be updated into a List and perform one ...


7

I tend to use a cache for this purpose. The actual cache can be simple: public class Cache { static Map<Id, SObject> records = new Map<Id, SObject>(); public static void add(SObject data) { record.put(data.Id, data.clone(true, true, true, true)); } public static void add(SObject[] data) { records.putAll(data....


7

This error is generating because your trigger on after update is running recursively. You can resolve this error by using a static boolean variable check. For eg. 1)create a class( say triggerHelperClass). create a public static boolean variable( say bool), with value = true/false; 2) Add a check if(triggerHelperClass.bool ==true) "// if u set the ...


7

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 Standard controller Data Loader AJAX Toolkit Asynchronous process ...


6

You can code the controller as without sharing and copy the values into and out of an intermediate object. The documentation for with sharing states: The with sharing keyword allows you to specify that the sharing rules for the current user be taken into account for a class. You have to explicitly set this keyword for the class because Apex code runs in ...


6

Try this. As said above, the main points are to remove SOQL & DML statement from any loops. trigger updateSerialList on Meter_Serial_Info__c(Before insert, Before update, Before delete) { if (Trigger.isDelete || Trigger.isUpdate) { Set<Id> serialIds = new Set<Id>(); for (Meter_Serial_Info__c msio : Trigger.old) { ...


6

System.Trigger behaves as a final static variable of an unknown, or anonymous, class, that resides in the System namespace, which itself behaves much like a static final variable of an anonymous class. Namespaces work like they do in C#, in that they can contain both members and classes, but without an explicit Type that can be referenced, as a class would ...


6

Triggers only fire on DML operations which you have already pointed out, does not occur until step 17 in the order of execution seen below and here. So in your scenario, The trigger for Object B will not fire until step 17 (the DML operation is committed) in the trigger for Object A.


6

trigger.new is simply a list of the records being processed by the trigger, so if all you need to do is loop through them then you can use that. trigger.newMap just allows you to target specific records by Id should you not need to process everything, or if you need to map other records back to these.


6

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, processes and only after that go for triggers as the latter ones are harder to maintain. Having said this you can achieve all the functionality from your description ...


5

Trigger.New is a list of sObjects. If you want a set of keys you want to use trigger.newMap. This is a map of the objects in trigger.new with the ID as the key and the object as the value. So you are trying to call keyset(), which is a map method, on a list. From the docs Trigger.new - Returns a list of the new versions of the sObject records. ...


5

Maybe there's other triggers on that object that cause an update after a record is inserted? Or maybe there's workflow on the object that causes a field update? That will in turn trigger the update trigger.


5

When the Trigger.isDelete is true, all the records in Trigger.old are being deleted.


5

If you convert a Lead using the UI then any CampaignMember records that refer to that Lead refer to the Contact instead. Using Database.convertLead() results in exactly the same behavior. The only thing to keep in mind is that you cannot convert Lead records until after you have inserted them, so the call to Database.convertLead() will have to be in an ...


5

My guess is that you have a work flow rule that has a field update on the Unit_Placements_Sales__c object that is causing the trigger to fire a second time. A common way to prevent this problem is to use a Static variable. You need to use class to do this. This is an example of of how a static variable with prevent the recursion. Utility Class public ...


5

You are trying to add the Id of an Order to OrderIds before you insert the orders. sObjects don't have Ids before you insert them* I'd suggest taking your insert OrderToInsert; DML statement outside of the for loop. Then, after you insert your new orders, loop through your OrderToInsert list, and add the Ids to OrderIds at that point. *Well...they can if ...


5

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 will be there please feel free to comment on this post. So I used set of Id static variable. Trigger trigger RN_Account on Account (after update) { ...


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