You need to sign in to do that
Don't have an account?
fredka
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){} } }
you already have the id, what about setting Opportunity o = q; ?
Message Edited by carramrod81 on 06-25-2008 02:48 PM
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!!!
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.
Message Edited by carramrod81 on 06-26-2008 09:32 AM