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
ranirani 

Trigger on Opportunity

Hi All,

 

I want to create a trigger on Opportunity which would update a field on Opportunity depending on values from Events associated with that Opportunity. Event has a date field and the maximum of that field should be updated to one of the Opportunities field when the Opportunity is updated and meets the criteria.

 

Regards,

Rani

dev_jhdev_jh

Hi Rani,

 

Although that might change in the new release of Salesforce (scheduled for Feb) there is currently no way to use aggregate functions in SOQL to my knowledge (such as SELECT MAX(Date) FROM...) .

 

A workaround I have used in the past is do a select ordering desc on the specific field and using the "LIMIT 1" clause. This way you will easily get the MAX value.

 

Hope it helps. Regards,

 

J

Message Edited by dev_jh on 01-23-2009 12:40 AM
ranirani

Hey J,

 

Thanks for the prompt response and solution, but in the trigger i would loop the opportunity object so again including SOQL in the for loop to retrieve the Event record as you mentioned would throw an error due to limitation of SOQL in loops. Do you have any other alternatives?

 

Appreciate your help.

 

Regards,

Rani

dev_jhdev_jh

Hi Rani,

 

Why are you looping through opportunities do you need to evaluate other Opps as well?

 

Maybe you can post part of the code?

 

J

ranirani

J, to handle bulk updates we need to loop thru the opportunites. For example the code of trigger would look as follows:

 

trigger OpportunityTrigger on Opportunity (before update)
{
    for (Opportunity O: trigger.new)
   {
    ....
    ......
   }
}

 

gv007gv007

I need some clear description,possile post full code