Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [trigger]

A trigger is a feature of the Salesforce platform consisting of Apex code that executes before or after DML operations.

96
votes
10answers
29k views

How do I start to debug my own Apex code?

Sometimes questions are posted here that would best be answered by helping the questioner learn how to do their own debugging. (Remember that proverb?) I'd like to have something to link to on this ...
43
votes
3answers
11k views

General trigger bulkification - best practices

A general question asked from time to time on stack exchange is question of trigger bulkification. Therefore I have 2 questions: Which best practices are you using? What is the best way to handle ...
17
votes
6answers
30k views

How to write a unit-test / test class for trigger?

How do I write a unit-test / test class for a trigger? What are my options? Inline with the trigger? New Class?
15
votes
4answers
5k views

System.Exception: Too many SOQL queries: 101 [duplicate]

I am getting a Too many SOQL queries: 101 error on a trigger when I am trying to deploy another trigger. I am posting the trigger which is causing the error. trigger rollupAccountShowings on Account ...
10
votes
2answers
2k views

Parent and children relationships accessible via trigger?

I'm sure this has been addressed before but I can't seem to find the answer. In a trigger on a record, aside from the actual fields on the record, what do I have access to? The parent info? that ...
28
votes
2answers
28k views

HTTP Callout from Triggers

I have a trigger on the Custom Object that calls a class. That class makes an HTTP GET request to a Web Service. However I get the following error: Callout from triggers are currently not supported. ...
46
votes
2answers
15k views

Lead Conversion Trigger Order of Execution

I've got a project where I'm looking to tweak the standard lead conversion process. Triggers and lead conversion has always been funky in past projects so I was hoping to start the design off by ...
37
votes
4answers
25k views

Deleting Triggers/Classes from Production

I was recently told ( Deleting a Class with IDE - cannot delete from Production? ): You cannot delete a class in production directly. You will need delete the class from your sandbox and then ...
14
votes
1answer
15k views

Exeception on After Insert, Record is read only

I'm writing a trigger for new opportunity products to populate a field Product_Category__c (on opportunity lineitem) getting the value from Product_Category_new__c (on Product). This is my trigger: ...
14
votes
3answers
49k views

System.FinalException: Record is read-only: Trigger.updateCompetitors: line 24

I am writing a trigger to get all the Opportunity competitors into a field on Opportunity, however, getting an error while testing it. Error: updateCompetitors: execution of AfterUpdate caused by: ...
80
votes
8answers
19k views

Can we determine if the Salesforce instance is production org or a Sandbox org?

I am looking for the most efficient way of identifying the type of org (Production or Sandbox) via Apex code. URL scraping is not useful to me, since I'd like to get this information inside a trigger ...
31
votes
5answers
18k views

How to generate a GUID/UUID?

What is the best way to generate a GUID/UUID from Apex code (such as a trigger)? Preferably in the following format: nnnnnnnn-nnnn-nnnn-nnnnnnnnnnnnnnnnn Example: 13219ec0-3a81-44c5-a300-...
9
votes
3answers
11k views

Make Validation Rule bypass if TRIGGER is run?

Is there a way to make a Validation Rule BYPASS via it's logic when a Trigger is doing it's job? I've already tried various methods including setting a hidden field on the object in the Trigger (can'...
22
votes
5answers
14k views

How can we disable triggers, Validation Rules and workflow rules in salesforce org. [on off - switch]

How can we disable triggers, Validation Rules and workflow rules in salesforce org. This becomes really needed and important when we are doing data migration(data imports) in salesforce. The way I ...
13
votes
3answers
6k views

Execute an action / run a trigger every time a User logs in to Salesforce

Is there any way to consistently execute a piece of Apex code whenever a User logs in to Salesforce? Posts such as this one seem to suggest that triggers on updates to the User object's 'LastLoginDate'...
1
vote
1answer
357 views

Navigating lookup relationships with dot notations, to retrieve field values

I need to access the value from a roll-up field on my Opportunities, from it's related Opportunity Product. When I use oli.Opportunity.Software_Line_Item__c the value retrieved is always null. So I'...
42
votes
4answers
13k views

Workflow rule causing trigger to fire twice

I need to be able to detect that the trigger execution is happening because of a workflow field update. There is a suggestion in the cookbook: Controlling Recursive Triggers But this solution is not ...
3
votes
2answers
1k views

Trigger to check for duplicate contacts on lead conversion [closed]

I'd like to write a Lead trigger, to check for duplicate Contacts before inserting the lead, or converting it. Contacts fields I'd like to check: Firstname - should not be the same as the leads ...
12
votes
5answers
4k views

When testing can I not use existing Data?

I am currently programming a trigger on Opportunities. I created a test class for this and in my Sandbox I created an account, contact and opportunity. When I try to reference them Opportunity Opp ...
-1
votes
3answers
5k views

Copying attachments from object to object trigger

I have a custom object called Server_Order__c used in my customer portal which is mirrored to the Opportunities object, as well as mirroring the data I would also like to mirror any attachments that ...
13
votes
1answer
15k views

What are recursive triggers? how to control them? Please provide an example also

What are recursive triggers? How can we control them, or prevent infinite recursion? Are these any risks inherent in them, and any limits that we should be aware of? Please explain with an example.
18
votes
6answers
4k views

Can I store information even if the trigger throws an exception?

I currently have a trigger which sometimes fails when new information is inserted from a 3rd party system. I can't simply check the logs as it usually happens during the night and it's not easy to ...
1
vote
2answers
344 views

Not able to get value of Picklist field of Parent on Child object record

In my org Lead is Child and a custom object (Intake__c) is parent. This custom object have 2 picklist fields called Intake_Month and Intake_Year. Now in trigger i am trying to access these 2 picklist ...
31
votes
1answer
5k views

Generic Apex Trigger

So I was kind of throwing round the idea of making a generic apex trigger that is callable by multiple objects. Say you have the following sObjects: Account Sales__c Sales_Contacts__c And I wanted ...
36
votes
4answers
20k views

How do I generate a random string?

How does one generate a random string sequence with Apex? I need to generate a code when a particular object is created in our Salesforce environment. I'd like the code to contain numbers from 0-9 ...
7
votes
1answer
4k views

trigger to find change in contact role for an opportunity

We have a scenario where we have contact roles added to an opportunity. We need to know if a contact role for a contact has changed for an opportunity. E.g John doe was an executive sponsor first for ...
14
votes
3answers
9k views

How to avoid recursive trigger other than the classic 'class w/ static variable' pattern?

How can we avoid trigger get executed again and again (recursive trigger)? One way is to have a class with static variable and have boolean value and check it is true or false and changing the boolean ...
12
votes
2answers
3k views

Are inactive triggers counted against test coverage?

We are in process of deploying a project to PROD and found that test coverage is coming up less then 75%. There are some triggers we have merged into single trigger. Question is: whether the ...
0
votes
2answers
4k views

DML Statement in ‘after trigger’ and not in ‘before trigger’?

Why we need to use DML Statement in ‘after trigger’ and not in ‘before trigger’? Thanks KS Kumar
10
votes
2answers
19k views

@future method: Unsupported parameter type

I am trying to copy records from one org to other via web services. I am calling a class which has logic written into a trigger. But @ future method is not taking sObject list as a parameter. Can ...
6
votes
3answers
5k views

Static variables defined in a trigger

While going through apex pdf i got line Static variables defined in a trigger don’t retain their values between different trigger contexts". Can anyone please explain me with some code sample. ...
3
votes
3answers
134 views

Rollup Summary Trigger Help

I am trying to create an Apex Trigger that performs a Rollup from a custom object to Contacts. Here is the error that I am receiving: Didn't understand relationship 'Assignments__r' in FROM part ...
3
votes
2answers
3k views

List Exception Duplicate id error

I get an error in my trigger when i tried to insert bulk data from data loader. Error is : ERROR: SubscriberUpdateTrigger: execution of AfterInsertcaused by: System.ListException: Duplicate id in ...
2
votes
1answer
3k views

DmlException:SELF_REFERENCE_FROM_TRIGGER

I have Opportunity-lookup field on Opportunity (self), Scenario: Opportunity A if select Opportunity B using lookup then Opportunity B's same lookup field automatically populate with Opportunity A. ...
1
vote
1answer
633 views

Triggers to update parent case

I have a Child and parent case. I have the following fields: Reopen Flag (Checkbox field) Case Reopened By (Drop down field) The vales in Reopened By fields are Customer and Internal Total Reopen ...
42
votes
5answers
2k views

What are all of the reasons why Salesforce customers should upgrade the API version on their Apex classes, triggers, and pages?

I haven't been able to find any official documentation or community discussion of the compelling reasons to upgrade the API version on Apex classes, triggers, and pages. I know there is the general ...
13
votes
3answers
3k views

How to download a VF page as PDF and email it from a trigger?

I have a renderAsPDF VF page that takes an object id parameter as part of the URL string. It returns a PDF with some textual data and some images (e.g. <apex:image id="theImage" value="/servlet/...
11
votes
3answers
6k views

How to Search Workflow Rules or Validation Rules etc in Apex i.e. Metadata Search via Apex?

Recently I'm working on building something for developers/administrators - a handy 100% force.com app allowing to search metadata. Currently this app allows you to quickly search (from sidebar) custom ...
10
votes
6answers
44k views

Trigger to update parent object value with child value

In my company we are using Oppotunity standard object and we have created a custom Quote object. The opportunity object is the parent and Quote is the child. How has to be the trigger in order to ...
8
votes
3answers
511 views

Control Fields vs Heap “Mock” Fields for Branching Trigger Behavior

I've seen solutions that talk about adding control fields to allow for clever trigger branching in problems like this. I know that works, but it annoys me (feels architecturally wrong) to create such ...
18
votes
1answer
39k views

Just update when a specific field changes

I created a trigger to open a case in my account if (Atribuido__c == 'SIM') I want the trigger to fire just when this field is updated. If this field is already updated and I update any other fields ...
16
votes
5answers
6k views

What sObject properties don't cause triggers to fire?

I recently discovered that updating your Chatter profile image won't cause the User trigger to fire (for fields SmallPhotoUrl or FullPhotoUrl). I was wondering what other fields, when updated, won't ...
6
votes
4answers
1k views

best way to handler trigger Recursive call

Below is my code snippet that I have used. And also I used static Boolean to handle the recursive call. But when I update more than 200 records then trigger only process first 200 record not all the ...
5
votes
1answer
17k views

Sending Email notification using trigger

I have created two objects Flight and Flight Attendant with fields name,gender,email Having lookup between them..whenever I insert Flight Attendant via Flight Object then email alert should be sent to ...
5
votes
2answers
5k views

How do I control recursive Triggers with static variables and permit Mass Edit from List View

I have a pretty complicated issue with users mass editing record from the Enhanced list View and using static methods to control the flow of recursive triggers. In case you are not familiar, due to ...
4
votes
1answer
5k views

How to display a message when the trigger finishes?

I have created a trigger that creates a new Contract object when an opportunity stage is equal to 'Closed/Won'. I need that once the trigger finishes, without error, it display some success message to ...
14
votes
1answer
14k views

How to Convert 18 char Id to 15 char Id using substring in Apex trigger

I have a trigger where i am assigning ID to a cusotm field like below - D.customfield__c = acc.Id; The ID is coming as 00P28000000EwpjEA0 But i need only 00P28000000Ewpj 15 char. Is there is a way ...
9
votes
2answers
3k views

What is the best way to check whether lead belongs to Queue or User?

I'm trying to determine in a trigger, whether a lead belongs to a Queue or a User. What is the best way of going about this? I tried the method mentioned in a dev. force discussion: System.debug( ...
8
votes
1answer
14k views

System.LimitException: Too many query rows: 50001

What I am trying to do is to set a date in my date field and also update the object. I am using the class below to do so which works just fine in my sanbox but in production I am getting this error: ...
8
votes
1answer
1k views

Is using Database.executeBatch from a trigger an anti-pattern?

In a managed package we have a trigger on Account that propagates a custom field status value from the Account to a large number of related custom objects and wrote the logic in a batchable to avoid ...