+ Start a Discussion
José UreñaJosé Ureña 

Modify Action Plans to insert opportunity on task creation

I want to modify Action Plans so depending on a field, a new record is inserted on Opportunity table with Opp's products.

I did the Action Plans Task modification to include a "Package" field (a very simple packages functionality i created consisting on the package object, and package products object).  I was able to add the field to the VFPs and it stores the "package" with the record without problems.

Now i need to create the Opp if the Action Plan detects that the Package field is not null when creating a Task... do you have some experience on this? do you know where i need to include this for what i want to achieve?

I included a method on the "ActionPlansTaskTriggerutilities" and i'm calling it on 2 lines (after "auxTask = new Task();" and "t2  = new Task();").  Here is the method:
private void InsertOppItem(APTaskTemplate__c dependentAptts)
        if (dependentAptts.Package__c==null)
                    List<Package_Item__c> itp = [Select product__c from Package_Item__c where Package__c =: dependentAptts.Package__c];
                    String pbID = [select id from Pricebook2 where isStandard=true limit 1].id;
                	Opportunity Newopp = new Opportunity();
                    Newopp.Accountid = dependentAptts.Action_Plan__r.Account__c;
                    Newopp.Name = 'Package Sale - ' + dependentAptts.Subject__c;
                    insert Newopp;
                    List<OpportunityLineItem> oliList = new List<OpportunityLineItem>();
                    for( Package_Item__c itemPaquete: itp)
                        String prid = itemPaquete.Product__c;
                        String prcbkeid = [select product2id from pricebookentry where pricebook2Id =: pbID and product2id =: prid limit 1].product2id;
                        PricebookEntry pbe;
                        if(prcbkeid == null)
                            pbe = new PricebookEntry (Pricebook2Id=pbID, Product2Id=prid, IsActive=true, UnitPrice=0.00);
                            insert pbe;
                        } else
                            pbe = [select product2id from pricebookentry where pricebook2Id =: pbID and product2id =: prid limit 1];
                        OpportunityLineItem oli = new OpportunityLineItem(OpportunityId=Newopp.Id, Quantity = 1, PricebookEntryId=pbe.Id, TotalPrice = pbe.UnitPrice);
                    insert oliList;