So I have a custom object Campaign__c which I need to relate with the Opportunity object. This will be many-to-many, so I've created a junction object called Campaign_plan__c with lookup (also tried M-D) to the other two objects (schema below).
The workflow will be:
- User creates a new "Campaign" record
- User will need to add existing Opportunities to this Campaign
- User should also have the option to create new Opportunities from here, and associate them with the Campaign
What I have so far:
- I've added the Campaign_Plan__c related list to the Campaign__c object, with two custom buttons
- First button "Add Opportunity" takes you to a create new Campaign_Plan__c (junction) record with the Opportunity lookup field, to pick the Opportunity to add
- Second button "Create Opportunities" is a VF page that lets the user create a bunch of Opportunities and save them
Issue(s):
- While the "Create Opportunities" works fine, I'm struggling to pass on the "Campaign" ID or create a Campaign_Plan__c (junction) object record
I tried a trigger, but I'm only able to create a junction object record while creating the Opportunity record and associate with that.
trigger CreateCampPlan on Opportunity (after insert) { List camplan = new List();
for (Opportunity newOpp: Trigger.New)
{
camplan.add(new Campaign_Plan__c(
Deal__c = newOpp.Id));
}
insert camplan;
}
Apex Class and VF to create bulk opportunities
<apex:page controller="ManageListController" tabstyle="Opportunity">
<apex:form >
<apex:pageBlock title="Create associated Deals">
<apex:pageBlockTable value="{!wrappers}" var="wrapper" id="wtable">
<apex:column headerValue="Ident">
<apex:outputText value="{!wrapper.ident}"/>
</apex:column>
<apex:column headerValue="Deal Name">
<apex:inputField value="{!wrapper.opp.Name}"/>
</apex:column>
<apex:column headerValue="Account">
<apex:inputField value="{!wrapper.opp.AccountId}"/>
</apex:column>
<apex:column headerValue="Expected Local NOI">
<apex:inputField value="{!wrapper.opp.Amount}"/>
</apex:column>
<apex:column headerValue="Expected Sale Start Date">
<apex:inputField value="{!wrapper.opp.CloseDate}"/>
</apex:column>
<apex:column headerValue="Deal Status">
<apex:inputField value="{!wrapper.opp.StageName}"/>
</apex:column>
<apex:column headerValue="Action">
<apex:commandButton value="Delete" action="{!delWrapper}" rerender="wtable">
<apex:param name="toDelIdent" value="{!wrapper.ident}" assignTo="{!toDelIdent}"/>
</apex:commandButton>
</apex:column>
</apex:pageBlockTable>
<apex:commandButton value="Add Row" action="{!addRows}" rerender="wtable">
<apex:param name="addCount" value="1" assignTo="{!addCount}"/>
</apex:commandButton>
<apex:commandButton value="Add 5 Rows" action="{!addRows}" rerender="wtable">
<apex:param name="addCount" value="5" assignTo="{!addCount}"/>
</apex:commandButton>
<apex:commandButton value="Save" action="{!save}"/>
</apex:pageBlock>
</apex:form>
</apex:page>
public class ManageListController
{
public List<OpportunityWrapper> wrappers {get; set;}
public static Integer toDelIdent {get; set;}
public static Integer addCount {get; set;}
private Integer nextIdent=0;
public ManageListController()
{
wrappers=new List<OpportunityWrapper>();
for (Integer idx=0; idx<5; idx++)
{
wrappers.add(new OpportunityWrapper(nextIdent++));
}
}
public void delWrapper()
{
Integer toDelPos=-1;
for (Integer idx=0; idx<wrappers.size(); idx++)
{
if (wrappers[idx].ident==toDelIdent)
{
toDelPos=idx;
}
}
if (-1!=toDelPos)
{
wrappers.remove(toDelPos);
}
}
public void addRows()
{
for (Integer idx=0; idx<addCount; idx++)
{
wrappers.add(new OpportunityWrapper(nextIdent++));
}
}
public PageReference save()
{
List<Opportunity> opps=new List<Opportunity>();
for (OpportunityWrapper wrap : wrappers)
{
opps.add(wrap.opp);
}
insert opps;
return new PageReference('/' + Schema.getGlobalDescribe().get('Opportunity').getDescribe().getKeyPrefix() + '/o');
}
public class OpportunityWrapper
{
public Opportunity opp {get; private set;}
public Integer ident {get; private set;}
public OpportunityWrapper(Integer inIdent)
{
ident=inIdent;
opp=new Opportunity(Name='');
}
}
}
Any ideas? Let me know if you need more info. As always, thanks so much for your help!