+ Start a Discussion
SharathChandraSharathChandra 

Creating Attachment with every case

In a visual force page i need to create mutiple cases with associated attachment with each case is it possible????

Please reply asap.

 

Best Answer chosen by Admin (Salesforce Developers) 
SimonJaiSimonJai

You want to assign different attachments to different cases?

I load the page and it has one Case A and I attach Attachment A. Then I want to add a new Case B and attach Attachment B to it.

When I save, I will have 2 cases, Case A with Attachment A and Case B with Attachment B.

Is that what you are trying to achieve?

 

 

If that is the case then I could create a custom class which contains a case object and an attachment object, then use that list to pass between VF and the controller.

 

public List<newCase> listOfCases { get; set; }

public class newCase() { public Case currentCase { get; set; } public Attachment currentAttach { get; set; } public newCase() {
            currentCase = new Case();
            currentAttach = new Attachment();
        } }

 

All Answers

SimonJaiSimonJai

Visual force is the view layer, what are you after is the controller.

 

You will have to write a custom class with a custom function that creates the attachments for each case.

 

http://boards.developerforce.com/t5/Apex-Code-Development/Creating-an-attachment-object-from-an-Apex-class/td-p/84746

SharathChandraSharathChandra

Yeah i'm using controller.

 

page

====

<apex:page controller="CaseListToCreateWithAtt">
<apex:form id="myForm" enctype="multipart/form-data">
<apex:pageBlock title="Web Intake Form" mode="edit" >
<apex:pageBlockSection columns="1" >
    <!--<apex:inputText value="{!contactName}" label="Contact Name"/>
    <apex:inputText value="{!contactEmail}" label="Email Id"/>-->
    <apex:repeat value="{!myListOfRecords}" var="record">
        <apex:inputField value="{!record.SuppliedName}"/>
        <apex:inputField value="{!record.SuppliedEmail}"/>
        <apex:inputField value="{!record.SuppliedPhone}"/>
        <apex:inputField value="{!record.Product_Family__c}"/>
        <apex:inputField value="{!record.Request_Category__c}"/>
        <apex:inputField value="{!record.Request_Type__c}"/>
        <!--<input type="file" value="{!myAttachment.Body}" name="inputFile"/>-->
        <apex:outputPanel >
        <apex:outputLabel for="inputFile"  style="margin-left:100px" >File Input</apex:outputLabel>
        <apex:inputFile value="{!myAttachment.Body}" fileName="{!myAttachment.Name}" id="inputFile" style="margin-left:20px"></apex:inputFile>
        </apex:outputPanel>    
        <br/><br/>
    </apex:repeat>
<apex:outputPanel >
<apex:commandLink value="Add Another Issue" action="{!createNewIssue}" reRender="mtForm"/>
<apex:outputText rendered="{!myListOfRecords.size>1}">&nbsp;|&nbsp;</apex:outputText>
<apex:commandLink value="Remove Issue" action="{!removeIssue}" rendered="{!myListOfRecords.size>1}" />    
</apex:outputPanel>
</apex:pageBlockSection>
<apex:pageBlockButtons location="bottom" >
<apex:commandButton value="Save" action="{!insertCases}"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>

 

 

controller

=======

 

public class CaseListToCreateWithAtt {
   
    public Attachment myAttachment {get; set;}
    
    public List<Case> myListOfRecords { get; set; }
    
    //public List<Attachment> myAttachment;    
    //Constructor
    
    public CaseListToCreateWithAtt(){

        myListOfRecords = new List<Case>();
        myListOfRecords.add(new Case(Origin = 'Web'));
        //myAttachment = new List<Attachment>();
        //myAttachment.add(new Attachment(body = blob.valueOf('')));
        myAttachment = new Attachment();
              

    }
   
    
   
                         public PageReference insertCases() {

                             for(Integer i = 0; i<myListOfRecords.size();i++){
                                 insert myListOfRecords[i];
                                 myAttachment.ParentId = myListOfRecords[i].id;
                                 insert myAttachment;
                             }
                            myAttachment.clear();
                            return null;
                        }
                        
    
    //Add case
    public PageReference createNewIssue() {
        if(myAttachment != null){
           myAttachment.clear();
        }
        myListOfRecords.add(new Case(Origin = 'Web'));
        return null;
    }
    
    //remove inserted case
    public PageReference removeIssue() {
        Integer size = myListOfRecords.size();
        Integer lastIndex = size-1;
        myListOfRecords.remove(lastIndex);
        return null;
    }

}

 

Here i'm not able to create new instance for each attachment could you help on this.

 

 

SimonJaiSimonJai
                         public PageReference insertCases() {

                             for(Integer i = 0; i<myListOfRecords.size();i++){

Everything below this should be [i] NOT [0], because you are traversing through the array.

insert myListOfRecords[i]; Attachment myAttachment = new Attachment(); myAttachment.ParentId = myListOfRecords[i].id; insert myAttachment; } myAttachment.clear(); return null; }

 

SharathChandraSharathChandra

coding mistake

it was i only and if i create instance in the for loop i wont be able to use that instance in the page.

After browsing for the file we need to assign it to body of attachment object so we need to create instances before. i'm in the consturctor itself.

But dynamic creation is not available.

Count you help on this.

 

 

 

SimonJaiSimonJai

You want to assign different attachments to different cases?

I load the page and it has one Case A and I attach Attachment A. Then I want to add a new Case B and attach Attachment B to it.

When I save, I will have 2 cases, Case A with Attachment A and Case B with Attachment B.

Is that what you are trying to achieve?

 

 

If that is the case then I could create a custom class which contains a case object and an attachment object, then use that list to pass between VF and the controller.

 

public List<newCase> listOfCases { get; set; }

public class newCase() { public Case currentCase { get; set; } public Attachment currentAttach { get; set; } public newCase() {
            currentCase = new Case();
            currentAttach = new Attachment();
        } }

 

This was selected as the best answer
SharathChandraSharathChandra

Thanks a lot

 

SharathChandraSharathChandra

I Have one more doubt.

refer the doc

in the 1st picture i was not able to render the page because of input file

i want functionality similar to second picture.

Please help.

 

https://docs.google.com/document/d/1zeCtp7loHXi08mTV0q1tMwe0_QjUWXTOPD0bpVbYMJQ/edit

SimonJaiSimonJai
So you are only attaching one file for all cases? or is it one file per case?

What do you mean you are not able to render the page? What error do you get?
SharathChandraSharathChandra

Each case is having one attachment.

 

Visualforce Error


apex:inputFile can not be used in conjunction with an action component, apex:commandButton or apex:commandLink that specifies a rerender or oncomplete attribute.

 


im also getting error like max (view state)size of the page limit is exceeded which is 135 kb if i add the attachment for the 1st case and then click on add issue, here i'm not  rendering.

SharathChandraSharathChandra

Add issue is a action component which is command link

SimonJaiSimonJai
Can you paste your VF code?
SharathChandraSharathChandra

<apex:page controller="CaseWithAttachment">

<apex:form id="fid">
<apex:pageBlock mode="edit" title="Web Intake Form" id="pb">
<apex:pageMessage summary="Whenever you create new issue you need to attach the file again for the previous issue<br/>Email will be sent for each issue" severity="info" strength="2" escape="false"></apex:pageMessage>
<apex:pageBlockSection columns="1">
<apex:repeat value="{!caseList}" var="record">
    <apex:inputField value="{!record.currentCase.SuppliedName}"  />
    <apex:inputField value="{!record.currentCase.SuppliedEmail}" />
    <apex:inputField value="{!record.currentCase.Product_Family__c}" />
    <apex:inputField value="{!record.currentCase.Request_Category__c}" />
    <apex:inputField value="{!record.currentCase.Request_Type__c}"/>
    <apex:outputPanel >
    <apex:outputlabel for="inputFile" style="margin-left:146px">File Input</apex:outputlabel>
    <apex:inputFile value="{!record.currentAttach.Body}" fileName="{!record.currentAttach.Name}" id="inputFile" style="margin-left:20px" ></apex:inputFile>
    </apex:outputPanel>
    <br/><br/>
</apex:repeat>
<apex:outputPanel >
    <apex:commandLink value="Add Issue" action="{!createIssue}" reRender="fid"/>
    <apex:outputText rendered="{!caseList.size>1}">&nbsp;|&nbsp;</apex:outputText>
    <apex:commandLink value="Remove Issue" action="{!removeIssue}" rendered="{!caseList.size>1}"/>
</apex:outputPanel>
<apex:commandButton action="{!saveCaseAttachment}" value="Submit"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>

</apex:page>

 

 

im also getting error like max (view state)size of the page limit is exceeded which is 135 kb if i add the attachment for the 1st case and then click on add issue, here i'm not  rendering.

SimonJaiSimonJai
Seems you like you can't use inputfile when rerendering.

This post mentions work arounds but I'e never tried it so I can't help you.

http://boards.developerforce.com/t5/Visualforce-Development/apex-inputFile-cannot-be-used-with-that-specifies-a-rerender-or/td-p/98161
Team WorksTeam Works

Hi