function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Anji P 9Anji P 9 

I'm trying to create task and attachment for Account object but here problem is when i'm trying to add second attachment for that its overriding i mean its taking first body value what i gave in first attachment can any one help me how to solve it

Vf page:
--------------------------


<apex:page controller="Account_Tasks">
    <apex:form>
    <apex:pageBlock id="pb">
        <apex:pageBlockButtons location="top">
        <apex:commandButton value="Save" action="{!save}" rerender="pb"/>
        </apex:pageBlockButtons>
        <apex:pageBlockSection title="Account Details">
        <apex:inputField value="{!acc.Name}"/>
            <apex:inputField value="{!acc.Industry}"/>
            <apex:inputField value="{!acc.Phone}"/>
            <apex:inputField value="{!acc.ownership}"/>
            <apex:inputField value="{!acc.rating}"/>
        </apex:pageBlockSection>
        <apex:pageBlockSection title="Task Details" columns="1" id="pbs">
            <apex:pageBlockTable value="{!tsks}" var="t">
                <apex:column headerValue="Priority">
        <apex:inputField value="{!t.Priority}"/>
                    </apex:column>
                <apex:column headerValue="Status">
            <apex:inputField value="{!t.status}"/>
                    </apex:column>
                <apex:column headerValue="Status">
            <apex:inputField value="{!t.Tasksubtype}"/>
                    </apex:column>
                    <apex:column headerValue="Status">
            <apex:inputField value="{!t.calltype}"/>
                        </apex:column>
                        <apex:column headerValue="Status">
            <apex:inputField value="{!t.createdDate}"/>
                            </apex:column>
                <apex:column >
                <apex:commandButton value="ADD" action="{!AddTask}" rerender="pbs"/>
                </apex:column>
                </apex:pageBlockTable>
        </apex:pageBlockSection>
        <apex:pageBlockSection columns="1" id="pbs1">
            <apex:pageBlockTable value="{!attch}" var="att">
                <apex:column headerValue="File Name">
            <apex:inputField value="{!att.name}"/>
                    </apex:column>
                                <apex:column headerValue="Body">
            <apex:inputtext value="{!str}"/>
                    </apex:column>
                <apex:column>
                <apex:commandButton value="Add Attachment" action="{!AddAttachmnt}" rerender="pbs1"/>
                </apex:column>
         </apex:pageBlockTable>
        </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>



Apex code:
----------------------------

public class Account_Tasks {
    public account acc{set;get;}
    public task tsk{set;get;}
    public string str{set;get;}
    public list<attachment> attch{set;get;}
    public list<task>tsks{set;get;}
    public Account_Tasks(){
        attch=new List<attachment>();
        tsks=new list<task>();
        acc=new account();
        tsk=new Task();
        AddTask();
        AddAttachmnt();
    }
    public void AddTask(){
        tsk=new task();
        tsks.add(tsk);
    }
    public void AddAttachmnt(){
        str=null;
        attachment a=new attachment();
        attch.add(a);
    }
    
    public PageReference save(){
        insert acc;
       list<task> tasks=new list<task>();
        for(task t:tsks){
            t.whatid=acc.id;
            tasks.add(t);
        }
        insert tasks;
        list<attachment> attaches=new list<attachment>();
        for(attachment at:attch){
        at.ParentId=acc.Id;
        at.Name=acc.Name+'.pdf';
        at.Body=blob.toPdf(str);
            attaches.add(at);
     }
        insert attaches;
        Pagereference p=new Pagereference('/'+acc.id);
        return p;
        }
    
}
 
Akshay ShrivastavaAkshay Shrivastava
---- Apex class-----
public class Account_Tasks {
    public account acc{get;set;}
    public task tsk{get;set;}
    public string str{get;set;}
    public list<attachmentWrapper> attachmentWrapperList{get;set;}
    public list<attachment> attch{get;set;}
    public list<task>tsks{get;set;}
    public Account_Tasks(){
        attch=new List<attachment>();
        attachmentWrapperList=new List<attachmentWrapper>();
        tsks=new list<task>();
        acc=new account();
        tsk=new Task();
        AddTask();
        AddAttachmnt();
    }
    public void AddTask(){
        tsk=new task();
        tsks.add(tsk);
    }
    public PageReference AddAttachmnt(){
        attachmentWrapper wrapperObj = new attachmentWrapper('','');
        attachmentWrapperList.add(wrapperObj);
        return null;
    }
    
    public PageReference save(){
        insert acc;
        list<task> tasks=new list<task>();
        for(task t:tsks){
            t.whatid=acc.id;
            tasks.add(t);
        }
        insert tasks;
        list<attachment> attaches=new list<attachment>();
        for(attachmentWrapper obj: attachmentWrapperList){
            attachment at = new attachment();
            at.ParentId=acc.Id;
            at.Name=acc.Name+'.pdf';
            at.Body=blob.toPdf(obj.bodyValue);
            attaches.add(at);
        }
        insert attaches;
        Pagereference p=new Pagereference('/'+acc.id);
        return p;
    }
    public class attachmentWrapper
    {
        public String nameValue {get;set;}
        public String bodyValue {get;set;}
        public attachmentWrapper(String nameValue1, String bodyValue1) {
            nameValue = nameValue1;
            bodyValue = bodyValue1;
        }
    }
}

----Vf Page-----

<apex:page controller="Account_Tasks">
    <apex:form>
        <apex:pageBlock id="pb">
            <apex:pageBlockButtons location="top">
                <apex:commandButton value="Save" action="{!save}" rerender="pb"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection title="Account Details">
                <apex:inputField value="{!acc.Name}"/>
                <apex:inputField value="{!acc.Industry}"/>
                <apex:inputField value="{!acc.Phone}"/>
                <apex:inputField value="{!acc.ownership}"/>
                <apex:inputField value="{!acc.rating}"/>
            </apex:pageBlockSection>
            <apex:pageBlockSection title="Task Details" columns="1" id="pbs">
                <apex:pageBlockTable value="{!tsks}" var="t">
                    <apex:column headerValue="Priority">
                        <apex:inputField value="{!t.Priority}"/>
                    </apex:column>
                    <apex:column headerValue="Status">
                        <apex:inputField value="{!t.status}"/>
                    </apex:column>
                    <apex:column headerValue="Status">
                        <apex:inputField value="{!t.Tasksubtype}"/>
                    </apex:column>
                    <apex:column headerValue="Status">
                        <apex:inputField value="{!t.calltype}"/>
                    </apex:column>
                    <apex:column headerValue="Status">
                        <apex:inputField value="{!t.createdDate}"/>
                    </apex:column>
                    <apex:column >
                        <apex:commandButton value="ADD" action="{!AddTask}" rerender="pbs"/>
                    </apex:column>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
            <apex:pageBlockSection columns="1" id="pbs1">
                <apex:pageBlockTable value="{!attachmentWrapperList}" var="wrapperObj">
                    <apex:column headerValue="File Name">
                        <apex:inputText value="{!wrapperObj.nameValue}"/>
                    </apex:column>
                    <apex:column headerValue="Body">
                        <apex:inputText value="{!wrapperObj.bodyValue}"/>
                    </apex:column>
                    <apex:column>
                        <apex:commandButton value="Add Attachment" action="{!AddAttachmnt}" rerender="pbs1"/>
                    </apex:column>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>


I have use this and it worked perfectly.

If your error is resolved then mark this as Best Answer