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
fredkafredka 

Correct Apex to Run for Bulk

I am feeling my way through Apex.... I am trying to modify this code to allow it to run in bulk (data loader)  I am just a little confused about how to get there.  I am guessing I should replace what I have in red with a list.  I am unsure on how to make this
usable in bulk?? any help would be greatly appreciated!!! thanks!!!!!!  Fred
Code:
trigger UpdatePrimaryQuoteOnOpp on Quote__c (after insert, after update) {
for (Quote__c q : Trigger.new)
{   
        String OppId = q.Opportunity__c;
        String CurrentQuoteId = q.Id;
        String CurrentQuoteStage = q.Stage__c;
                try{
            Quote__c[] allquotes = [Select Quote__c.Id, Quote__c.Date_Closed__c, Quote__c.Opportunity__c, Quote__c.Stage__c 
            FROM Quote__c WHERE Quote__c.Opportunity__c = :OppId ORDER BY Quote__c.Date_Closed__c DESC];
            if(allquotes.size()>0)
            {
                //Opportunity o = [select Id from Opportunity where Id=:q.Opportunity__c]; 
                Opportunity o = new Opportunity(Id=OppId);

                //if there is only one quote, use that one
                if(allquotes.size() == 1)
                {
                    o.Primary_Quote__c = CurrentQuoteId;
                    o.StageName = CurrentQuoteStage;
                    update o;
                }
                else        //else there is more than one, so we must find the one with most advanced stage
                {
                    Quote__c FinalQuote = New Quote__c();
                    Integer FinalStage = 0;
                    
                    for (Integer i = 0; i < allquotes.size(); i++)
                    {
                        Integer NewStage = 0;
                        if(allquotes[i].Stage__c == 'Closed Won')
                        {
                            NewStage = 7;
                        }
                        else if(allquotes[i].Stage__c == 'Finalist')
                        {
                            NewStage = 6;
                        }
                        else if(allquotes[i].Stage__c == 'Released to Client')
                        {
                            NewStage = 5;
                        }
                        else if(allquotes[i].Stage__c == 'Released to Sales')
                        {
                            NewStage = 4;
                        }
                        else if(allquotes[i].Stage__c == 'In Underwriting')
                        {
                            NewStage = 3;
                        }
                        else if(allquotes[i].Stage__c == 'Closed Lost')
                        {
                            NewStage = 2;
                        }
                        else if(allquotes[i].Stage__c == 'Closed Sales Declined')
                        {
                            NewStage = 2;
                        }
                        else if(allquotes[i].Stage__c == 'Closed U/W Declined')
                        {
                            NewStage = 2;
                        }
                        //now we evaluate
                        
                        if(NewStage > FinalStage)
                        {
                            //check to see which one is most recent
                            FinalQuote = allquotes[i];
                            FinalStage = NewStage;
                            
                        }
                    }
                    
                    o.Primary_Quote__c = FinalQuote.Id;
                    o.StageName = FinalQuote.Stage__c;
                    update o;
                }
                
                /*Qualify
                In Underwriting
                Released to Sales
                Released to Client
                Finalist
                Closed Lost
                Closed Won
                Closed Sales Declined
                Closed U/W Declined*/
            }
        }
        catch(Exception e){}
}
}

 
carramrod81carramrod81
Why are you pulling the opportunity back out of the table? You already have it in q. Because this is an after insert/after update trigger
you already have the id, what about setting Opportunity o = q;   ?

Code:
for (Quote__c q : Trigger.new)
{   
        String OppId = q.Opportunity__c;
        String CurrentQuoteId = q.Id;
        String CurrentQuoteStage = q.Stage__c;
                try{
            Quote__c[] allquotes = [Select Quote__c.Id, Quote__c.Date_Closed__c, Quote__c.Opportunity__c, Quote__c.Stage__c 
            FROM Quote__c WHERE Quote__c.Opportunity__c = :OppId ORDER BY Quote__c.Date_Closed__c DESC];
            if(allquotes.size()>0)
            {
                //Opportunity o = [select Id from Opportunity where Id=:q.Opportunity__c]; 



 



Message Edited by carramrod81 on 06-25-2008 02:48 PM
fredkafredka

the object I am on is a custom object named "quote" .... q represents the quote ... the opportunity id is on the quote.  I am going out and pulling in the opportunity so I can  update it.

basically, whenever a quote is changed, I am comparing all quotes with this opportunity id and then assigning a "primary quote" to the opportunity.  there is a primary quote field and stage field on opportunity that is getting updated.

Hope that makes sense. thanks!!!

carramrod81carramrod81
my bad, i misread, what about something like this? this takes all the the q.Opportunity__c in the current trigger, and queries them before processing
anything in the trigger. hope this makes sense, i had a similar issue a couple weeks ago and this is the way i did it.
also fwiw, you can reduce the batch size in data loader to a lower number (basically setting a max on how many records the trigger will see at once) to get around apex restrictions. I wouldn't make this a frequent thing, as you'll get lazy with your code. (btw, it's under Settings->Settings.

Code:
String[] qOpportunityIds = new List<String>();
for(Quote__c q:Trigger.new){
 if(q.Opportunity__c != null) qOpportunityIDs.add(q.Opportunity__c);
}

//this will give you all your opportunity id's that are processing in this trigger
//pull all the opportunities into a list before re-iterating each Quote__c in your trigger

Opportunity[] Matches = [select Id,Name from Opportunity where Id IN :q.qOpportunityIDs];

for(Quote__c q:Trigger.new){
//do what you need to do for each q
}
//basically your iterating through it the first time to get the q.Opportunity__c, then pulling them
//all down in one query, then iterating through your Trigger and doing any processing
//you need




Message Edited by carramrod81 on 06-26-2008 09:32 AM