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 have a process contains a custom button on opportunity layout. When the opportunity turns to closed won, a user clicks the button and go into a visual workflow several new records: license (custom object), service contract (with input of start date and end date), and account field update.

I have a trigger the grabs related opportunity line items and creates them a service contract line items (once service contract is created). I want to use this trigger as a plugin within the flow.

trigger NewSCLineItems on ServiceContract (After Insert) {

// Make a List of Opportunity ID's for all these quotes.
Set<Id> oppIds = new Set<Id>();
for(ServiceContract SC : Trigger.new) oppIds.add(SC.opportunity__c);

// Fetch all the Opportunity Line Items for all these Opportunities
List<OpportunityLineItem> olis = new List<OpportunityLineItem>([
    select 
         id, opportunityid, pricebookentryid, Quantity, UnitPrice, Discount
    from OpportunityLineItem 
    where opportunityid = :OppIds AND M_S_Product__c = 'TRUE'
]);    

// Build a Map, keyed by OppId, of Lists of the related OLI's
Map<Id, List<OpportunityLineItem>> oliMap = new Map<Id, List<OpportunityLineItem>>();
for (OpportunityLineItem oli: olis) {
   if (oliMap.containsKey(oli.OpportunityId)) {
        // If the map already has an entry for this Opp, add this OLI to the list.
        //oliMap.put(oli.OpportunityId, oliMap.get(oli.OpportunityId).add(oli));
        List<OpportunityLineItem> x;
        x = oliMap.get(oli.OpportunityId);
        x.add(oli);
        oliMap.put(oli.OpportunityId, x);
   } else {
        // This is the first entry for this Opportunity
        List<OpportunityLineItem> tmp = new List<OpportunityLineItem>();
        tmp.add(oli);
        oliMap.put(oli.OpportunityId, tmp);
   }
}


 List<ContractLineItem> qli = new List<ContractLineItem>(); 

   // Iterate through each SC
   for(ServiceContract sc : Trigger.new){
       // Do we have any OLI's for this SC Opportunity?
       if (oliMap.containsKey(SC.opportunity__c)) {
           // Yes, so for each OLI in the List, create a QLI
           for (OpportunityLineItem oli: oliMap.get(SC.opportunity__c)) {
                qli.add(
                     New ContractLineItem (
                         ServiceContractId = SC.id,
                         pricebookentryid = oli.pricebookentryid,
                         UnitPrice = oli.UnitPrice,
                         Discount = oli.Discount,
                         Quantity = oli.Quantity
                     )
                );
           }
       }
    }
    if (! qli.isEmpty()) insert qli;
}
share|improve this question
What is your question? What have you tried? Have you read the docs: www.salesforce.com/us/developer/docs/apexcode/Content/apex_process_plugin.htm – jkraybill Dec 31 '12 at 1:22

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.