function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Will EdwardsWill Edwards 

Is there a way to reference child campaigns on a Visualforce page located on the parent campaign?

I'm trying to pull a field from all child campaigns and reference it on a Visualforce page accessed via a button on the parent campaign. Is there anyway to do that? Thanks.
Best Answer chosen by Will Edwards
Mahesh DMahesh D
Hi Will,

Please find the code for Campaigns:

Visualforce Page:
<apex:page standardController="Campaign" extensions="CampaignController">
    <apex:form>
        <apex:pageBlock title="List of Campaigns">
            <apex:pageBlockTable value="{!camp.ChildCampaigns}" var="cc">
                <apex:column value="{!cc.Name}"/>
                <apex:column value="{!cc.Description}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Apex Class:
public class CampaignController {
    public Campaign camp {get; set;}
    public CampaignController(ApexPages.StandardController controller) {
        camp = (Campaign) controller.getRecord();

        if(camp != null && camp.Id != null) {
            camp = [Select Id, Name, (Select Id, Name, Description from ChildCampaigns) from Campaign where Id =: camp.Id];
        }
    }
}
I also tested the above code in my DE environement and below is the result:

User-added image

Please do let me know if it helps you.

Regards,
Mahesh

 

All Answers

Mahesh DMahesh D
Hi Will,

Please find the below code:

Visualforce Page:

 
<apex:page standardController="Quote" extensions="QuoteController">
    <apex:form>
        <apex:pageBlock title="List of Quote Line Items">
            <apex:pageBlockTable value="{!q.QuoteLineItems}" var="qli">
                <apex:column value="{!qli.Product2.Name}"/>
                <apex:column value="{!qli.Quantity}"/>
                <apex:column value="{!qli.Quote.Name}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Apex Class:
 
public class QuoteController {

    public Quote q {get; set;}
    public QuoteController(ApexPages.StandardController controller) {
        q = (Quote) controller.getRecord();

        if(q != null && q.Id != null) {
            q = [Select Id, Name, (Select Id, Description, QuoteId, Product2Id, Product2.Name, Quote.Name, Quantity, ServiceDate, PricebookEntryId, ListPrice from QuoteLineItems) from Quote where Id =: q.Id];
        }
    }
}

I also tested the above code in my DE environement and below is the result:

User-added image

Please do let me know if it helps you.

Regards,
Mahesh
Will EdwardsWill Edwards
Mahesh,

Super helpful, thanks! Could you provide some further advice?

How do I adjust the code if I'm not dealing with a quote but just a child campaign?
Mahesh DMahesh D
Hi Will,

Please find the code for Campaigns:

Visualforce Page:
<apex:page standardController="Campaign" extensions="CampaignController">
    <apex:form>
        <apex:pageBlock title="List of Campaigns">
            <apex:pageBlockTable value="{!camp.ChildCampaigns}" var="cc">
                <apex:column value="{!cc.Name}"/>
                <apex:column value="{!cc.Description}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Apex Class:
public class CampaignController {
    public Campaign camp {get; set;}
    public CampaignController(ApexPages.StandardController controller) {
        camp = (Campaign) controller.getRecord();

        if(camp != null && camp.Id != null) {
            camp = [Select Id, Name, (Select Id, Name, Description from ChildCampaigns) from Campaign where Id =: camp.Id];
        }
    }
}
I also tested the above code in my DE environement and below is the result:

User-added image

Please do let me know if it helps you.

Regards,
Mahesh

 
This was selected as the best answer
Will EdwardsWill Edwards
Mahesh, this is amazing! Thank you. 

One final question, do you have a suggestion for how to add each child campaign as a column in the table rather than a row?
Mahesh DMahesh D
Hi Will,

It will be tough because columns should be fixed, we can't make them dynamic.

Regards,
Mahesh
Will EdwardsWill Edwards
Thanks, Mahesh!
Susan Hiller TroenSusan Hiller Troen

I'm also trying to pull fields from the child campaign and reference them on a visual force page.
 

I copied & pasted the Apex Class code into a new Apex Class and received the following error:
"Error: Compile Error: unexpected token: 'public class CampaignController' at line 1 column 0"

Any idea what I am doing wrong?  Screenshot attached. 
Thank You!
User-added image