Tell me more ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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

share|improve this question
please add addl. information on how you identify which section id needs to be set – Seb__Wagner May 15 at 8:48
I have updated my question ,Thanks – Enry May 15 at 8:59
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. – Sdry May 15 at 9:01
Yes, i have.Which custom setting? is the query necessary ?can i declare just one get method? – Enry May 15 at 9:07
add comment (requires an account with 50 reputation)

1 Answer

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.

share|improve this answer
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 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. – Sdry May 15 at 9:51
thank you very much ;) – Enry May 15 at 10: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. – Jeremy Nottingham Jun 14 at 12:40
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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