0

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

  • 2
    Hi Elavarasi, welcome to SFSE! Please take a moment to visit the help center, scroll through the tour, and read How to Ask. Telling the community to "kindly provide a solution" is not a question, nor is it very polite. Please keep in mind that your post here is a Question and actually ask something. – Adrian Larson Jun 30 '17 at 13:04
1

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

| improve this answer | |
1

Your issue is in your controller, the line if(attach==null) will run the attach = new Attachment(Body=bodyvalue); line. I'm guessing this was a typo and that you actually meant for that if statement to run the bodyvalue='Test'; line, but when you commented it out it pushed the line below into the if statement. In general, this is why it is not recommended to leave out braces, even if there is only one statement. Adding braces should solve your issue:

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);
    }

}
| improve this answer | |

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.