0

I have a custom controller extension below which pre-populates the Opportunity ID and a Contact Role ID to a Visualforce page for a custom object that has a Master-Detail relationship to the Opportunity object. I am now trying to create 2 buttons on the VF page that will populate a 3rd custom field, save the custom object record, and re-direct the user to another VF page. However, when I try to save the code below I get the error:

Error: VF_CampaignCaseCreateController Compile Error: Initial term of field expression must be a concrete SObject: List at line 37 column 9

I'm sure my syntax is incorrect for populating the 3rd field in each method, but I'm not sure how to fix it.

public class VF_CampaignCaseCreateController{

public List<Campaign_Create_Request__c> CCR {get; set;}

    private final Opportunity opp;
    public VF_CampaignCaseCreateController(ApexPages.StandardController myController){
        CCR = new List<Campaign_Create_Request__c>();
            if (!Test.isRunningTest())
            {
            myController.addFields(new List<String>{'Id', 'OwnerId', 'Owner.Phone'});
            }
        opp=(Opportunity)myController.getrecord();
    }

    public Campaign_Create_Request__c CCR2 = new Campaign_Create_Request__c();
        public void CampaignCaseCreate(){

            CCR2.Opportunity__c = opp.Id;

            Opportunity o = [SELECT (SELECT Id, contactId
                                       FROM OpportunityContactRoles
                                       WHERE role = 'Signatory')
                            FROM Opportunity
                            WHERE id = :opp.id];
            CCR2.Primary_User__c = o.opportunityContactRoles.size() != 0 
                                    ? o.opportunityContactRoles[0].contactId  
                                    : null;

            CCR.add(CCR2);
        }


    public PageReference AdCamp() {

        insert CCR;
        CCR.Requested_Action__c = 'Create and Add';

        PageReference RetPage = new PageReference('/VF_CampaignCreate_Edit?id=' + CCR[0].id);
        RetPage.setRedirect(true);
        return RetPage; 
    }

    public PageReference Camp() {

        insert CCR;
        CCR.Requested_Action__c = 'Create Only';

        PageReference RetPage = new PageReference('/VF_CampaignCreate_Edit?id=' + CCR[0].id);
        RetPage.setRedirect(true);
        return RetPage; 
    }

}
3
  • You're calling CCR.Requested_Action__c , but CCR is a list. That is the source of your exception. Jan 11, 2016 at 19:57
  • Thanks. How would I fix that?
    – J. Neilan
    Jan 11, 2016 at 20:03
  • Well, you notice on the line below where you instantiate the PageReference you use CCR[0].id. I'm assuming you'd want to do the same thing. CCR[0].Requested_Action__c. Also, you're changing that value just after you insert the list so if I'm not mistaken, those changes are lost when you redirect. Jan 11, 2016 at 20:05

2 Answers 2

3

In this line:

CCR = new List<Campaign_Create_Request__c>();

You instantiate CCR as a list.

This line is causing the exception:

CCR.Requested_Action__c = 'Create and Add';

Just below that, you are referencing the object correctly using CCR[0].id.

I'm assuming you'd want all the assignments to be saved, so substitute the lines like:

    insert CCR;
    CCR.Requested_Action__c = 'Create and Add';

With:

CCR[0].Requested_Action__c = 'Create and Add';
insert CCR;

Do that substitution in both methods.

Hope this helps!

1
  • 1
    @J.Neilan Glad I could help! Jan 11, 2016 at 20:19
2

Your controller is a little confused. It looks like you only ever create one request record, so holding on to a list is getting you tripped up. I would change the controller to something more like:

public with sharing class RequestCampaignExtension
{
    public static final String CREATE_ONLY = 'Create Only';
    public static final String CREATE_AND_ADD = 'Create and Add';

    final Id oppId;
    public Campaign_Create_Request__c request;
    public RequestCampaignExtension(ApexPages.StandardController opportunityController)
    {
        oppId = opportunityController.getId();
        request = new Campaign_Create_Request__c(Opportunity__c=oppId);
    }
    public void setPrimaryContact()
    {
        List<OpportunityContactRole> contactRoles = [
            SELECT ContactId FROM OpportunityContactRole
            WHERE OpportunityId = :oppId AND Role = 'Signatory'
        ];
        if (!contactRoles.isEmpty()) request.Primary_User__c = contactRoles[0].Id;
    }
    public PageReference createOnly() { return create(CREATE_ONLY); }
    public PageReference createAndAdd() { return create(CREATE_AND_ADD); }
    PageReference create(String requestedAction)
        request.Requested_Action__c = requestedAction;
        insert request;

        PageReference redirect = Page.VF_CampaignCreate_Edit;
        redirect.getParameters().put('Id', request.Id);
        return redirect;
    }
}

If you override the Edit action with the page specified above, you can build the redirect more simply.

// instead of the last three lines above
return new ApexPages.StandardController(request).edit();

You must log in to answer this question.

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