function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
john98755john98755 

soql with triggers

hi,
 i am new to salesforce i have small doubt we have run time information store using trigger.new holding all newly created redords.
my doubt is we have all new record then why we are writing soql queries in triggers
ManojjenaManojjena
Hi John,

Inside trigger if any refrence field value if you want then you need to query as trigger.new only contain the reference id not any other field value .

Like in contact trigger you will get only account  id not any other field value to get other field values you need to query from account with id .

incase of after event you need to query as trigger .new is always readonly on after event .In that case also you need to query .

Check the below link for some concepts of trigger .

http://manojjena20.blogspot.in/2013/03/manojs-trigger-blog.html

Let me know if it helps

Thanks 
Manoj
Amit Chaudhary 8Amit Chaudhary 8
In following case we need to execute Query inside trigger

1) Some time we need to fatch the related object record in trigger in that case we also need to execute Soql
2) We need to fatch the reference record value (Lookup) . For example Account detail in Contact Trigger.
3) If Trigger is on After Event in that case also we need query the record if we need to update the same record.

Trigger Best Practices
1) One Trigger Per Object
2) Logic-less Triggers
3) Context-Specific Handler Methods
4) Bulkify your Code
5) Avoid SOQL Queries or DML statements inside FOR Loops
6) Using Collections, Streamlining Queries, and Efficient For Loops
7) Querying Large Data Sets

NOTE:- Trigger.new will give you same object record only
Please check below post for more info
http://amitsalesforce.blogspot.com/2015/06/trigger-best-practices-sample-trigger.html

Please let us know if this will help you

Thanks
Amit Chaudhary