[SalesForce] Inserting Accounts With Attachments Using Wrapper Class

I have created one visualforce page for Inserting Accounts with Attachments.
I have used Wrapper class for insering accounts and Attachments.

But while insertion the two Accounts has been inserted with same file attachments.
Below is my visualforce page.Here the second attachment(lighthouse) gets inserted for the Test Account and Test Account1 also.

enter image description here

Apex Class File:

public class WrapperClassFileController {

    Public List<Wrapper>Wrapperlist{get;set;}
    public String string1{get;set;}
    public String string2{get;set;}
    public Attachment attach{get;set;}
    public Blob bodyvalue{get;set;}

    public WrapperClassFileController(ApexPages.StandardController controller) 
    {

        Wrapperlist=new List<Wrapper>();
        for(Integer i=0;i<=1;i++)
        {
            string1='';
            string2='';
            if(attach==null)
                //bodyvalue='Test';
                attach = new Attachment(Body=bodyvalue);

            Wrapperlist.add(new Wrapper(string1, string2,attach ));
            system.debug('WrapperlistWrapperlist'+Wrapperlist);
        }

    }


    public void save()
    {
        system.debug('>>>>>'+Wrapperlist);

        for(Wrapper wr : Wrapperlist)
        {
            Account acc = new Account();
            acc.Name=wr.str1;
            acc.Account_Code__c=wr.str2;
            insert acc;

            Attachment attach = new Attachment();
            attach.ParentId=acc.id;
            attach.ownerId=UserInfo.getUserId();
            attach.IsPrivate=true;
            attach.Description='Test Description';
            attach.name=wr.attached.name;
            attach.Body=wr.attached.body;
            system.debug('========='+wr.attached.body);
            insert attach;
        }


    }


    public class wrapper{
        public  String str1{get;set;}
        public String str2{get;set;}
        public Attachment attached{get;set;}
        //public String body {get;set;}

        public  wrapper(String string1,String string2,Attachment attach)
        {
            str1=string1;
            str2=string2;
            attached=attach;
        }
    }
}

The loop gets executed for insertion

for(Wrapper wr : Wrapperlist) {
    Account acc = new Account();
    acc.Name=wr.str1;
    acc.Account_Code__c=wr.str2;
    insert acc;

    Attachment attach = new Attachment();
    attach.ParentId=acc.id;
    attach.ownerId=UserInfo.getUserId();
    attach.IsPrivate=true;
    attach.Description='Test Description';
    **attach.name=wr.attached.name;
    attach.Body=wr.attached.body;**
    system.debug('========='+wr.attached.body);
    insert attach;
}

The above highlighted lines will attach the same filename and body for both Accounts.

enter image description here

Best Answer

It lacks the code for the VFP so I wrote this one.

<apex:page standardController="Account" extensions="WrapperClassFileController">
<apex:form >
    <apex:pageBlock >
        <apex:pageBlockButtons><apex:commandButton action="{!save}" value="Save"/></apex:pageBlockButtons>            
        <apex:pageBlockSection title="Accounts">
            <apex:pageBlockTable value="{!Wrapperlist}" var="wp">
                <apex:column headerValue="Name"><apex:inputtext value="{!wp.str1}" ></apex:inputtext></apex:column> 
                <apex:column headerValue="Code"><apex:inputtext value="{!wp.str2}" ></apex:inputtext></apex:column> 
                <apex:column ><apex:inputFile value="{!wp.attached.body}" filename="{!wp.attached.name}" id="file"></apex:inputFile>
                 </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlockSection>
    </apex:pageBlock>    
</apex:form>

And the problem is just the use of the properties attach and bodyvalue (unique references).

public class WrapperClassFileController {

Public List<Wrapper>Wrapperlist{get;set;}
// public String string1{get;set;}
// public String string2{get;set;}
// public Attachment attach{get;set;}
//  public Blob bodyvalue{get;set;}

public WrapperClassFileController(ApexPages.StandardController controller) 
{   
    Wrapperlist=new List<Wrapper>();
    for(Integer i=0;i<=1;i++)
    {
        String  string1='';
        String  string2='';
        Blob bodyvalue = null;
        Attachment attach = new Attachment();
        if(attach==null)
            //bodyvalue='Test';
            attach = new Attachment(Body=bodyvalue);

        Wrapperlist.add(new Wrapper(string1, string2,attach ));
        system.debug('WrapperlistWrapperlist'+Wrapperlist);
    }   
}


public void save()
{
    system.debug('>>>>>'+Wrapperlist);

    for(Wrapper wr : Wrapperlist)
    {
        Account acc = new Account();
        acc.Name=wr.str1;
        acc.Account_Code__c=wr.str2;
        insert acc;

        Attachment attach = new Attachment();
        attach.ParentId=acc.id;
        attach.ownerId=UserInfo.getUserId();
        attach.IsPrivate=true;
        attach.Description='Test Description';
        attach.name=wr.attached.name;
        attach.Body=wr.attached.body;
        system.debug('========='+ wr.attached.name + '=' +wr.attached.body);
        insert attach;
    }       
}

public class wrapper{
    public  String str1{get;set;}
    public String str2{get;set;}
    public Attachment attached{get;set;}
    //public String body {get;set;}

    public  wrapper(String string1,String string2,Attachment attach)
    {
        str1=string1;
        str2=string2;
        attached=attach;
    }
}

}

The two attachments are created.

Regards

Alain

Related Topic