0

I have an apex class with one method that returns a list of Standard Campaign objects that I'm trying to print in a visualforce page. For some reason, the list does not print in the visualforce page but another list of a custom type (defined in the class) is successfully printed on the page. In the visualforce page, I'm setting the controller attribute to the name of the controller class so I was thinking that maybe the controller doesn't recognize objects of types that aren't specified in the <apex:page/> tag.

I'm not directly extending any classes in the visualforce page and I'm accessing other classes besides Campaign so I'm not sure if I would need to set each sObject I use as a controller or even how I would do that. How do I display standard objects in a visualforce page?

EDIT: visual force page code:

<apex:page controller="CreateCampaign"   >
    <apex:form >
         <apex:pageBlock title="Quick Edit: ">
            <apex:pageBlockButtons >
                <apex:commandButton action="{!createCampaignFromPage}" value="Save"/>
            </apex:pageBlockButtons>
             </apex:pageBlock>
            <h1>Hello World</h1> 

<apex:repeat value="{!availMailingsRet}" var="string" id="theRepeat3">
            <apex:outputText value="{!string}" id="theValue3"/><br/>
        </apex:repeat> //This works
        <apex:repeat value="{!campaignsRet2}" var="string" id="theRepeat4">
            <apex:outputText value="{!string}" id="theValue3"/><br/>
        </apex:repeat> // This doesn't

   </apex:form>
</apex:page>

CONTROLLER CODE:

The method included in the snippet below works if I switch out the standard Campaign object with CampaignObj which is a class defined within the controller

public class CreateCampaign {
 public static List<CampaignMember> campaignMembersRet {get; set;}
    public static List<AvailableMailing> availMailingsRet {get; set;}
    public static List<Campaign> campaignsRet {get; set;}
    public static List<CampaignObj> campaignsRet2 {get; set;}

public  List<Campaign> upsertCampaigns(){
        /*Create campaigns that don't currently exist
        */
        List<Campaign> campaigns = new  List<Campaign>(); 
        List<AvailableMailing> availMailings =  getAvailableMailings();
        List<Integer> campaignsNotFound = new List<Integer>();
        List<Campaign> allCampaigns = [SELECT Id , Campaign_Key__c FROM Campaign WHERE Campaign_Key__c <> ''];
        List<CampaignObj> nullCampaigns = new  List<CampaignObj>(); 
        for(Integer i = 0; i < availMailings.size(); i++){
            for(Campaign cmp : allCampaigns ){
                if(availMailings[i].Campaign_Key == cmp.Campaign_Key__c){
                    break;
                }
            }
            campaignsNotFound.add(i);
        }

        for(Integer i = 0; i < campaignsNotFound.size();i++){
            AvailableMailing currentMailing = availMailings[campaignsNotFound[i]];
            Campaign camp = new Campaign(Campaign_Key__c = currentMailing.Campaign_Key, name = currentMailing.name,Mailing_ID__c= currentMailing.MailingId,Type = currentMailing.type);            
            campaigns.add(camp);

       }
   try{
            upsert campaigns Campaign_Key__c;
        }
        catch (DmlException e) {
            System.debug(e.getMessage());
        }
    return campaigns;
    }
5
  • You don't need to use an sObject's standard controller to display values of that type in the page. You simply need to reference them using the appropriate Visualforce syntax ({! }) inline or in the components you are using.
    – David Reed
    Feb 28, 2018 at 20:51
  • @DavidReed I'm currently doing that, I just updated the post with the code from the page Feb 28, 2018 at 20:57
  • Can we see the controller too? Feb 28, 2018 at 21:08
  • @SebastianKessel I updated the post with code from the class Feb 28, 2018 at 21:20
  • Not Enough.... what do your inner classes look like? Feb 28, 2018 at 21:23

1 Answer 1

2

Based upon the discussion on a previous question, I think the what you have here is a type mismatch. You're working with a controller property like this:

public List<Campaign> campaignsRet2 { get; set; }

and you're trying to display it like this:

<apex:repeat value="{!campaignsRet2}" var="string" id="theRepeat4">
    <apex:outputText value="{!string}" id="theValue3"/><br/>
</apex:repeat> 

The reason this doesn't work is that the variable string defined by your <apex:repeat> is not a string that can be displayed by <apex:outputText> - it's a Campaign, an sObject instance, and standard output components don't display sObjects.

Instead, you can use one or more standard output components to display fields from your sObject, along this pattern:

<apex:repeat value="{!campaignsRet2}" var="theCampaign" id="theRepeat4">
    <apex:outputText value="{! theCampaign.Name }" id="theValue3"/><br/>
</apex:repeat> 

(Note that I've changed your variable name for clarity).

You can add further output components within the same <apex:repeat> to display other fields from the same Campaign, and they don't all have to be <apex:outputText>.

1
  • Ok, that makes sense. It looks like in apex, pretty-printing was implemented for lists objects but I guess not for sObjects. Feb 28, 2018 at 21:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .