New Batch#100 (10th Nov 2021) - Salesforce Admin + Dev Training (WhatsApp: +91 - 8087988044) :https://t.co/p4F3oeQagK

Showing posts with label trigger. Show all posts
Showing posts with label trigger. Show all posts

Saturday, 16 February 2013

Update a record on which we are writing the trigger

Use before insert or before update if you need to update or insert the record on which you are writing the trigger.

Trigger populate_userlookup on Book__c (before insert, before update) {
  for(Book__c b : Trigger.new) {
   b.buyer__c = b.ownerId;
 }
}

Note: No need to use DML statements like insert or update in case of before triggers.

Wednesday, 17 October 2012

maximum trigger depth exceeded MyObject__c trigger

* The above error is mainly because of the recursive execution of the trigger after update again and again.
* To control the recursive trigger, use if condition or any other controllable steps to avoid the recursion.
Refer:
http://boards.developerforce.com/t5/Apex-Code-Development/Classic-Error-maximum-trigger-depth-exceeded-Opportunity-trigger/td-p/209817

Wednesday, 15 August 2012

I have 1000 records, how can I execute 200 records each time in Trigger?

By default triggers will run in a batch of 200 records. For 200 record one trigger invocation, even in case of Bulk API.

Consider a scenario where you're inserting 1100 records then it will be run in 6 batches

200 * 5  + 100 * 1 = 6 (Invocations)

-------------------------------------------

* Use SOQL for loops to operate on records in batches of 200.
* SOQL for loop, which iterates over the returned results in batches of 200 records.
* This reduces the size of the ml list variable which now holds 200 items instead of all items in the query results, and gets recreated for every batch.

for (List<Merchandise__c> ml : [SELECT Id,Name FROM Merchandise__c])
{
// Do something.
}

* You can also use batch apex class to process large number of records.

Saturday, 11 August 2012

copy field values of one object to another object

Normal Logic:
trigger insertMember2 on Member__c (after insert) {
 Set<Id> Ids= new Set<Id>();
    for (Member__c member : Trigger.new)
    {
        Ids.add(member.Id);        
    }
 List<Member__c> memberList = new List<Member__c>([Select Id,FirstName__c,LastName__c  From Member__c e where Id in :Ids]);

 for(Member__c temp : memberList )
 {
  Member2__c member2 = new Member2();
  member2.FirstName__c = temp.FirstName__c;
  member2.LastName__c = temp.LastName__c;
  insert member2;

 }
 }
Good Logic:
* Trigger should be like this and will work for bulk data as well as insert statement is used out side the for loop

 List<Member2__c> listMember2ObjToInsert = new List<Member2__c>();
 for (Member__c memberObj : Trigger.new)
    {
        Member2__c member2Obj = new Member2();
 member2Obj.FirstName__c = memberObj.FirstName__c;
 member2Obj.LastName__c = memberObj.LastName__c;
        // If you have any refernce of member in member2 you should fill that as well
 listMember2ObjToInsert.add(member2Obj);
    }
insert listMember2ObjToInsert;
}

Friday, 10 August 2012

Trigger to update a field in parent record once a task is created

Question:
I need to create a trigger that does the following:

Whenever a new task with a subject of "Email: CO1 Feedback" is created on a contact record, a field on that contact record called "CO1_followup_email_date__c" should be updated to match the date that the task was created.

The code that I used is below. I am not getting any error messages, but when I create the task with the proper subject line, the CO1 Follow-up email date is not updated. Any ideas?? Thanks for your help!

trigger Contactbirthdayupdate on Task (after insert) 
{
List<Contact> ContactsToUpdate = new List<Contact>();
for (Task t: Trigger.new)
{
if (t.subject=='Email: CO1 Feedback')
{
Contact c = new Contact(Id=t.WhatId);

c.CO1_Followup_email_date__c = t.ActivityDate;
ContactsToUpdate.add(c);
}
}
   try {
        update ContactsToUpdate; 
    } catch (system.Dmlexception e) {
        system.debug (e);
    }
}


Solution:

Its whoId not WhatId... update to..

Contact c = new Contact(Id=t.WhoId);

Labels