4

I'm working on an interface that allows users to create activities directly from a list of records of a custom object, so I created a wrapper class that contains said object and a task, in order to display the data and gather inputs in the same table. The table renders correctly, and I know the inputFields are binding to the appropriate task fields since they display the appropriate pick-list values, widget displays, etc. However, when the task is inserted by calling a controller method from a commandLink on the same row of the wrapper class, said task fields end up as null on the inserted record, even though the user gave non-null values to the corresponding inputFields on the VF page. Am I misunderstanding the mechanics of inputFields binding to controller variables? Below is sample code.

Wrapper class:

public class ObjWrapper
{
        public Task tsk { get; set; }
        public Custom_Object__c custObj { get; set; }

        public ObjWrapper()
        {
            tsk = new Task();
            custObj = new custObj();
        }

        public ObjWrapper(Task tsk, Custom_Object__c custObj)
        {
            this.tsk = tsk;
            this.custObj = custObj;
        }
}

Controller:

public class WrapperController 
{
        public Map<Id, ObjWrapper> wrapperMap { get; set; }
        public Id custObjIdFromTable { get; set; }

        public WrapperController()
        {
            wrapperMap = new Map<Id, ObjWrapper>();
            for(Custom_object__c obj: [SELECT Id, Name, Field1__c FROM Custom_Object__c])
                wrapperMap.put(obj.Id, new ObjWrapper(new Task(), obj));
        }

        public List<ObjWrapper> getWrapperListForTable()
        {
            return wrapperMap.values();
        }

        public void saveTask()
        {
            Task tsk = wrapperMap.get(custObjIdFromTable).tsk;
            tsk.WhatId = custObjIdFromTable;
            tsk.Status = 'Completed';
            tsk.ActivityDate = System.TODAY();
            tsk.Subject 'Custom Object Task';
            insert tsk;
        }
}

VF Page:

    <apex:page controller="WrapperController">
        <apex:form >
            <apex:pageBlock title="Wrapper Objects" >
                <apex:pageBlockTable  value="{!wrapperListForTable}" var="w">
                    <apex:column headerValue="Custom Object Name">
                        <apex:outputText value="{!w.custObj.Name}"/>
                    </apex:column>
                    <apex:column headerValue="Custom Object Field 1">
                        <apex:outputText value="{!w.custObj.Field1__c}"/>
                    </apex:column>
                    <apex:column headerValue="Task Field 1">
                        <apex:inputField value="{!w.tsk.Task_Field1__c}"/>
                    </apex:column>
                    <apex:column headerValue="Task Field 2">
                        <apex:inputField value="{!w.tsk.Task_Field2__c}"/>
                    </apex:column>
                        <apex:commandLink value="Complete Task" action="{!saveTask}" > 
                            <apex:param name="custObjIdFromTable" value="{!w.custObj.Id}" assignTo="{!custObjIdFromTable}"/>
                        </apex:commandLink>
                    </apex:column>
                </apex:pageBlockTable>
            </apex:pageBlock>
        </apex:form>
    </apex:page>
  • side note: Shouldn't saveTask() method set tsk.whatId = customObjIdFromTable; ? Instead of tsk.CustObjLookup__c = custObjIdFromTable – cropredy Feb 27 '14 at 17:15
  • Yes, you're right. As mentioned below I re-wrote this from scratch, my actual scenario doesn't contain a custom look-up field on the Task object, I was just using it here as an example. – kbentsen Feb 27 '14 at 17:18
  • Did you ever figure out this problem? I also have a commandLink that isn't binding the param to my controller variable – Doug Ayers May 17 '14 at 4:19
  • Have you tried putting the wrapper instance back to the map with the updated task after saving? – Ranga Jun 9 '18 at 7:29
0

You should create your Map like below in controller -

public Map<Id, ObjWrapper> wrapperMap { get; set; }

Instead of

public Map<Id, Custom_Object__c> wrapperMap { get; set; }

Actually in your map, you are not taking value as ObjWrapper so VF is not able to locate custObj and tsk objects.

Are you sure that you are not getting any exception? Have you put any <apex:messages /> in your visualforce page to see if any exception occur?

  • Sorry, that was a mistype on my part. I wrote the above from scratch as it's a watered down version of my actual code, but has the same essential functionality. And yes, I have tried adding pageMessages to the VF page to catch any errors, but nothing comes up. – kbentsen Feb 27 '14 at 16:23

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.