1

I am writing a trigger for the object OpportunityLineItem to set for all new records the field section__c.

trigger setsection on OpportunityLineItem (before insert)
{
    for (OpportunityLineItem opp: Trigger.new )
{ 
      opp.section__c=
}
}

I want set the field section__c for all new record with the value of the field SectionID of the first record of another object named SectionopportunityProduct Object SectionopportunityProduct has ever just one record.I want get the value from this.

There isn't a relationship between opportunitylineitem-SectionopportunityProduct. How can I do this?

Thanks in advance for any advice. BR

3
  • please add addl. information on how you identify which section id needs to be set
    – Seb Wagner
    May 15 '13 at 8:48
  • 1
    So you have an object which will always have only 1 records ? Have you considered using a custom setting ? In your current approach you could query for the record using SOQL, outside your iteration. May 15 '13 at 9:01
  • Yes, i have.Which custom setting? is the query necessary ?can i declare just one get method?
    – Enry
    May 15 '13 at 9:07
1

Easy & Fast & really not the best practise

trigger setsection on OpportunityLineItem (before insert)
{
      SectionopportunityProduct temp = [SELECT field_you_need FROM SectionopportunityProduct  LIMIT 1];
           //if there ever are multiple records this query can not predict which record you receive 
           //update: this will also fail if there is no data. Take into account normal development practises around these scenarios
      for (OpportunityLineItem opp: Trigger.new ){ 
           opp.section__c= temp.field_you_need;
       }
 }

However If you have an object which you only intend to use for a single record, containing some values you may be better of with a different approach. If the values you are storing are basic types you could store them in a list custom setting which will still allow admin configurability.

http://help.salesforce.com/help/doc/en/cs_about.htm

If the values you need are likely to never change you could also store them in a static class as public static variables. Changing would require deployment as this is apex.

Or .. even simpler, you could use a field update workflow.

Options are pretty general and the specific choice would depend on the overall thing you are trying to implement.

3
  • Thank you very much!! clean and easy!! I want try the custom setting solution but i insert the values in the fields of the only one record from a visual force page.Field's values aren't static. Custom setting can be updated from apex code?
    – Enry
    May 15 '13 at 9:43
  • Haven't played with hierarchical settings, but you can insert and update a list custom setting like any sObject. To query you can use the get() method which uses the cache. Querying them over soql doesn't use the cache I believe. Chek the documentation. Much examples & snippets online if you'd look for it. May 15 '13 at 9:51
  • The trigger as written will throw an exception if there is not at least one record in your SectionopportunityProduct__c table. For example, anytime you refresh a sandbox, you will not be able to create Opportunities until you populate your record. Jun 14 '13 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.