+ Start a Discussion
Elavarasi VElavarasi V 

Inserting 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.
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 both Accounts( Test Account and Test Account1 ) also.
Kindly provide solution for this.

User-added image
 
Best Answer chosen by Elavarasi V
Alain CabonAlain Cabon
Hi,

There is the same question here:

https://salesforce.stackexchange.com/questions/181350/inserting-accounts-with-attachments-using-wrapper-class/

I wrote a very simple VFP and fixed the former apex code but the response after the click on "Save" is still missing. 

Regards

All Answers

bob_buzzardbob_buzzard
I'd imagine you've bound the apex:inputFile to the same attachment object, but it's rather difficult to tell from a screenshot of the UI!
vijayabhaskarareddyvijayabhaskarareddy
Hi @Elavarasi V,
pls  post your code here,
it wil help u to resolve  your issue

regerds,
vijay
 
Alain CabonAlain Cabon
Hi,

There is the same question here:

https://salesforce.stackexchange.com/questions/181350/inserting-accounts-with-attachments-using-wrapper-class/

I wrote a very simple VFP and fixed the former apex code but the response after the click on "Save" is still missing. 

Regards
This was selected as the best answer
Elavarasi VElavarasi V

Hi,

This is my visualforce page and controller

visualforce page

<apex:page standardController="Account" extensions="WrapperClassFileController">
<html>
<apex:form >
<apex:pageblock title="Accounts">

<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!save}"/>
</apex:pageBlockButtons>

<apex:pageBlockSection title="Accounts">
<apex:pageblockTable value="{!Wrapperlist}" var="a">
<apex:column headerValue="Name">
<apex:inputText value="{!a.str1}"/>
</apex:column>
<apex:column headerValue="Account Code">
<apex:inputText value="{!a.str2}"/>  
</apex:column>
<apex:column headerValue="Name">
<apex:inputFile accept="doc, txt, pdf" filename="{!a.attached.Name}"   filesize="1000" size="50" value="{!a.attached.Body}"/>  
</apex:column>
</apex:pageblockTable>
</apex:pageBlockSection>
</apex:pageblock>

</apex:form>
</html>
</apex:page>

controller:

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)
          attach = new Attachment( );
     
        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;
      

    
}
}

}

Elavarasi VElavarasi V
Thanks  Alain Cabon