4

I have created one wrapper class with sObject and index for sObject and im getting and setting list of wrapper to show on VF page. On VF page im showing some fields from that sObject and provided one button to clone that record so that it will be shown on next row. At the controller if i assign values directly from wrapper (i.e. new wrapper instance = wrapper instance to be clone) its getting cloned and displayed on VF but while saving its giving me error as "id" of sObject is also get clonned. If i try to get field values one by one from wrapper its giving me null values. (eg new wrapper.sObject.field = wrapperToBeCloned.sObject.field), sObject from the wrapper to be cloned is giving null values. How to solve this problem ?

4
  • Are you using the clone method of the sObject? You need to use it like this: clone(false, true, false, false); More info here Sep 11, 2013 at 7:23
  • Ya, I tried that also, but problem is i am getting null values from sObject from the wrapper to be clone. Sep 11, 2013 at 7:26
  • Show your code. Sep 11, 2013 at 7:28
  • public void clone() { * created instanc of newWrapper newWrapper.sobjct = wrapToBeCloned.sobject.clone(false,true); } this is giving me null value in wrapToBeCloned.sobject and if i tried this newWrapper.sobjct = wrapToBeCloned.sobject; then its giving me value but not allowing to save. Sep 11, 2013 at 7:32

3 Answers 3

0

I think you need to clone an sObject first, and then add it to a new instane of the wrapper. I've created a simple example page and controlle where the wrapper records could be cloned:

Page:

<apex:page controller="test1">
<apex:form>
<apex:pageBlock>

    List 1
    <apex:pageBlockTable value="{!wrapperList}" var="w">
        <apex:column>
            <apex:commandButton action="{!cloneWrappeer}" reRender="clones" value="clone"/>
        </apex:column>
        <apex:column value="{!w.index}"/>
        <apex:column value="{!w.acc.Street__c}"/>
    </apex:pageBlockTable>

    <br/>

    Clones
    <apex:pageBlockTable value="{!wrapperClonedList}" var="w" id="clones">
        <apex:column value="{!w.index}"/>
        <apex:column value="{!w.acc.Street__c}"/>
    </apex:pageBlockTable>

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

Controller:

public with sharing class test1 {
    public List<MyWrapper> wrapperList { get; set; }
    public List<MyWrapper> wrapperClonedList { get; set; }

    public class MyWrapper{
        public String index { get; set; }
        public Account acc { get; set; }

        public MyWrapper(Account a){
            this.acc = a;
            this.index = a.name;
        }
    }

    public void cloneWrappeer(){
        Account a = wrapperList[0].acc.clone(false, true, false, false);
        wrapperClonedList.add(new MyWrapper(a));
    }

    public test1() {
        wrapperList = new List<MyWrapper>();
        wrapperClonedList = new List<MyWrapper>();
        testacc = [select id, name, Street__c from account limit 1];
        wrapperList.add(new MyWrapper(testacc));
    }
}

The result page:

enter image description here

3
  • Thanks for help. But this also not working. Its giving me null object. Actually i am using inputfield to enter data with duplicate button. When im direct assigning it taking values but if i tried cloning its giving null. Sep 11, 2013 at 8:49
  • @RahulNagardalekar I think your index is broken if trying to reference a parent wraper in the controller. Read this topic: Removing rows separately from the Visualforce pageBlockTable list Sep 11, 2013 at 9:21
  • @mast0r- No. I am getting the wrapper correctly. Only thing is if I direct assign that wrapper to new instance of the wrapper i am getting values but i am not able to store it as ID of sObject is same. If I try to get wrapper.sObject its giving me null. Sep 11, 2013 at 9:32
0

In case of clone sObject It is not possible to that if you clonning the wraper class instance then sObject in it will be clonned properly.

If you wrote a wrapper class to store sObject then you need to write your own custom method to
deep clone the sObject first and the add to new Wrapper class instance.

Wrapper wrper = oldWrapper // This will not work..

Or a user-defined constructor constructor something like

WrapperClass wc = new Wrapperclass(oldWrapperInstance.getSObjectfield,OtherParams);

or

WrapperClass wc = new Wrapperclass(oldWrapperInstance.getSObject.clone(false, true, false, false),OtherParams);
0

I clonned it by passing values from apex:param, and then assigning it to wrapper.sObject.field = passed param.

You must log in to answer this question.

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