2

I am stuck at what I beleived to be a simple implementation using Visual Force Dynamic Bindings. I have the following scenario: A Standard Case has a relationship to a Custom Object (we can call it CustomObject__c). On the Case Standard Page Layout I have a Visual force page with the Standard Controller Case and the extension to my extension method (extensionmethod.cls). I create a list of the fields from the CustomObject__c to present as inputFields on the visual force page. This works fine and the visual force page gets created as it should. However, when I in my regular commandButton try to save the CustomObject__c, all the values are null before the insert. There is something obvious that I am overlooking but I cannot find it.

Visual Force page:

<apex:form id="mainForm">
<apex:pageBlock id="pbConsole" mode="edit">
    <apex:pageBlockButtons location="top">
        <apex:commandButton title="{!$Label.Save_Button_Label}" 
                                action="{!saveCustomObject}" 
                                value="Save"/>
        </apex:pageBlockButtons>

    <apex:pageBlockSection>
        <apex:repeat value="{!fieldList}" var="fld" >
            <apex:inputField value="{!covRep[fld]}" />
        </apex:repeat>
    </apex:pageBlockSection>

</apex:pageBlock>

Controller:

@TestVisible private Case theCase;
public List<String> fieldList {get;set;}
public CustomObject__c covRep {get;set;}

public extensionMethod(ApexPages.StandardController stdController) {

    this.theCase = (Case)stdController.getRecord();

    if(theCase.CustomObject__c == null){
        covRep = new CustomObject__c(Case__c = theCase.Id);
    }else{
        covRep = new CustomObject__c(Id = theCase.CustomObject__c);
    }

    System.debug('---covRep : ' + covRep);

    fieldList = new List<String>{'CustomField_1__c', 'CustomField_2__c', 'CustomField_2__c'};
}

public PageReference saveCustomObject(){
    System.debug('--- saveCustomObject : ' + this.covRep);
    try{
        upsert covRep;
    }catch(Exception e){
        System.debug('Exception : ' + e);
    }
    return null;

}

So when I fill in the values in the Visual Force page and clicks Save I would expect that the line System.debug('--- saveCustomObject : ' + this.covRep); would print out the values for the Object, but here everything is NULL, or more specificly the value Case__c is filled out correctly if the first IF theCase.CustomObject__c == null is fulfilled in the constructor method.

I am not sure if I need some additional logic or if it is better to do this in some other way, but I just cannot figure out why the covRep object is empty in my save method. I do not see any error messages or similar.

Ciao!

Update 1:

I have now verified that switching the repeat tag in my VF Page to specifically specifying the fields:

<apex:inputField value="{!covRep.CustomField_1__c}" />
<apex:inputField value="{!covRep.CustomField_2__c}" />
<apex:inputField value="{!covRep.CustomField_3__c}" />

works as intended, so there must be an issue with the binding in <apex:inputField value="{!covRep[fld]}" />...

  • 1
    Ids are auto generated...How do you provide value to it. Id = theCase.CustomObject__ – MIX DML Jan 19 '16 at 10:05
  • Sorry, I missed specifying that in the text here, in my constructor I also have the following: stdController.addFields(new List<String>{'CustomObject__c'}); So there I get the ID of the Custom Object, if there is any. – erikduvet Jan 19 '16 at 10:15
  • What you have should be working. I gave this a shot in one of my sandbox orgs, and the <apex:inputField value={!covRep[fld]}" /> syntax worked fine for me. The values persisted after I clicked the "Save" button (the one that calls saveCustomObject()), and my debug logs showed the correct values. I tested this with an API version down to 20.0 for the Visualforce page (the first version I found that supports this particular syntax), and an API version down to 10.0 for the controller extension. Also tested with a less privileged user. I'm out of ideas. Have you tried using a fieldset? – Derek F Jan 19 '16 at 16:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.