+ Start a Discussion
Karthi_VeluKarthi_Velu 

System.debug('Log messgae'); and Trigger on Lead after update/insert

Hi,

 

I am New to salesForce. I had two issue.

1). using System.debug('Log messgae'); in trigger apex code, i can't the log in "System Log" link.

2). while using Trigger on Lead after update/insert i cant change the lead object value.

3). can you give any update example in apex or trigger(how to set the id).

 

example code:

 

trigger ChangeLeadCheckStatus on Lead (after insert, after update) {
       system.debug('test Karthig');
       if (true) {
            Lead[] newLead = Trigger.new;
            newLead[0].LastName = newLead[0].LastName + '_VB_';
       }

}

Best Answer chosen by Admin (Salesforce Developers) 
Karthi_VeluKarthi_Velu
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger ChangeLeadCheckStatus caused an unexpected exception, contact your administrator: ChangeLeadCheckStatus: execution of AfterUpdate caused by: System.Exception: Record is read-only: Trigger.ChangeLeadCheckStatus: line

All Answers

Karthi_VeluKarthi_Velu
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger ChangeLeadCheckStatus caused an unexpected exception, contact your administrator: ChangeLeadCheckStatus: execution of AfterUpdate caused by: System.Exception: Record is read-only: Trigger.ChangeLeadCheckStatus: line
This was selected as the best answer
Karthi_VeluKarthi_Velu

Not Yet solved

 

Thnaks advance

JimRaeJimRae

Not sure about your system log issue.  But your other error is caused by the fact that you can't update a field on the object with an after trigger, you need to use a before trigger.  Also, you need to make your trigger bulk safe.  It should handle multiple records being processed in one step.

 

 

trigger ChangeLeadCheckStatus on Lead (before insert, before update) { system.debug('test Karthig'); if (true) { Lead[] newLead = Trigger.new; for(Lead l :newLead){ l.LastName = l.LastName + '_VB_'; } //newLead[0].LastName = newLead[0].LastName + '_VB_'; //this would only update the first record } }

 

 

Karthi_VeluKarthi_Velu

Many Thanks for your response!

 

It means that we can't update using trigger(by after update/insert) the same object just updated. And if possible send any example or link for trigger bulk safe.

JimRaeJimRae

Yes, my general use case for after triggers is to insert or update other related objects, but not the direct object being acted on in the trigger.

 

I am not sure of any specific references on how to bulk safe your triggers, but I follow these rules:

 

1. Remember that ALL triggers are batch triggers and could process multiple records

2. Try to keep any SOQL queries outsideof the main processing loop for the array.

3. Use Set's to store unique ID's needed to query for information

4. use Maps to hold those results queried using the sets as an "IN" clause

5. Any inserts or updates of  objects should also be done in batch, collect all updated or new records in a list and insert the entire list in one transaction.

 

I hope this helps at least somewhat.

 

Karthi_VeluKarthi_Velu

Thank you very much.

 

I got some idea how to do the trigger in SF..