• mechanicalRHINO
  • NEWBIE
  • 0 Points
  • Member since 2009

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies

Working on a trigger that creates a new opportunity when a particular type of opportunity is Closed Won.  I can't clone it because a number of things change, but for creating the new opportunity I need to be able to get at the values of not only the opportunity but the product line items.

 

There are two things I'm not sure of:

 

1) Are the product line items included via the Opportunity when I create a map of Opportunities?  If so, how do I reference them later when extracting certain values to apply to the new Opportunity?

 

2) How do I create the line items for the new opportunity?

 

Here's the code I have so far excluding the items referenced above:

 

 

trigger createRepSitesRecurringOpportunity on Opportunity (after insert, after update) { RecordType rt = [SELECT Id FROM RecordType WHERE Name = 'Rep Sites' AND SobjectType = 'Opportunity']; // Create map for Contact ID's & Opportunities, that match our RecordType criteria Map<Id,Opportunity> mapRepSitesOpportunities = new Map<Id, Opportunity>(); // Loop through the Opportunities being inserted and see if there are any for the Rep Sites record type and "Apply Recurring Billing = true". for (Opportunity o : trigger.new) { If ((o.RecordTypeId == rt.Id) && (o.Apply_Recurring_Billing__c == true)){ mapRepSitesOpportunities.put(o.Contact__c, o); } } system.debug('mapRepSitesOpportunities.size at Line 15 = ' + mapRepSitesOpportunities.size()); If (mapRepSitesOpportunities.size() > 0) { //Check for Rep Sites = Active and remove Opportunities from the list where Rep is no longer active. List<Id> contactidsForOpptToNotCreate = new List<Id>(); for (Contact c : [Select Id, Rep_Sites__c from Contact where Id in :mapRepSitesOpportunities.keySet()]) { If (c.Rep_Sites__c <> 'Active') { mapRepSitesOpportunities.remove(c.Id); } } } system.debug('mapRepSitesOpportunities.size at Line 29 = ' + mapRepSitesOpportunities.size()); //Create Oppt with the correct Recurring product/amount and set the Close Date to First Day of the next month If (mapRepSitesOpportunities.size() > 0) { List<Opportunity> opptsToInsert = new List<Opportunity>(); Opportunity newOppt = new Opportunity(); for(Opportunity oppt : [Select RecordTypeId, Name, Contact__c, AccountId, Amount From Opportunity where Id in :mapRepSitesOpportunities.values()]) { newOppt.Name = oppt.Name + ' Recurring Auto Generated'; newOppt.AccountId = oppt.AccountId; newOppt.Contact__c = oppt.Contact__c; newOppt.RecordTypeId = oppt.RecordTypeId; newOppt.CloseDate = Date.today(); //change this to first of next month newOppt.Amount = oppt.Amount; //stop mapping this once the product is created and amount set there. newOppt.StageName = 'Recurring Billing'; opptsToInsert.add(newOppt); } insert(opptsToInsert); } }

 

 Where do I go next?