0

Good day how does one link an inputFile upload to a Lead record? I have created a visual force form with some text fields and an input upload field to save on to the notes and attachment section. On saving the data is posted but I cannot get the attachment. Please kindly assist as I am still new to salesforce development.

Please find below what I had initially tried.
Thank you very much for the assistance:

VF Page:

<apex:page standardController="Lead" extensions="attachmentsample">
    <apex:form>
        <apex:sectionHeader title="Upload attachment document." />
        <apex:pageblock>
            <apex:pageblocksection columns="1">
                <li>
                    <input id="00N2C000000Fj2x" maxlength="255" name="00N2C000000Fj2x" size="20" type="text" class="field-style field-split align-left" required="required" placeholder="Registered Name" />
                    <input id="00N2C000000Fj32" maxlength="255" name="00N2C000000Fj32" size="20" type="text" class="field-style field-split align-right" required="required" placeholder="Trading As" />
                </li>
                <apex:inputfile value="{!myfile.body}" filename="{!myfile.Name}" />
                <apex:commandbutton value="Save" action="{!Savedoc}" />
            </apex:pageblocksection>
        </apex:pageblock>
    </apex:form>
</apex:page>

Controller

public class attachmentsample {
    public attachmentsample(Lead controller) {}
    public attachmentsample(ApexPages.StandardController controller) {}
    Public Attachment myfile;
    Public Attachment getmyfile() {
        myfile = new Attachment();
        return myfile;
    }
    Public Pagereference Savedoc() {
        String accid = System.currentPagereference().getParameters().get('id');
        Attachment a = new Attachment(parentId = accid, name = myfile.name, body = myfile.body);
        /* insert the attachment */
        insert a;
        return NULL;
    }
}
  • 1
    In order to link the attachment to a Lead record, you need to put the Lead Id in the parentId field of the attachment. You are currently putting 'accid' in there, which you fetch from 'Id' page parameter. Depending on how the page is loaded, this might work or not. Can you share how you load the page? E.g. custom button calling visualforce or url. – Guy Clairbois Jul 1 '16 at 11:13
  • Hi Guy thanks for your valued response. On the test that I did for the upload functionality I was passing the LeadID parameter and the attachment wouod appear on the notes and attachment section but when I incorporate it with my form with the other fields the attachment does not go through. – user23 Jul 1 '16 at 13:35
  • Yes so that's why I need to know how the form is loaded (e.g. screenshot of the custom button definition). That defines whether or not the right parameters are set. – Guy Clairbois Jul 1 '16 at 14:16
  • I am using a custom button that's defined on the code above. I did not make any definitions on to the button. – user23 Jul 1 '16 at 15:13
  • Guy is asking for the URL to brings up the page: \apex\mypage?id=????? The ID needs to be the ID of an existing Lead. Since your controller uses the variable accId - this looks like the code was copy-pasted from some other VF controller dealing with accounts and hence our confusion. Also, use apex:inputField instead of input as best practice. – cropredy Jul 2 '16 at 0:48
0

Considering the comment of cropredy, if you call your visualforce page upon clicking on custom button or link with a URL like this:

 /apex/mypage?id=<leadId> 

it will give you expected result. The file will be attached to the Lead record.

Also suggest you to declare myFile as private like this:

private Attachment myfile;

Follow proper naming convention and best practices.

  • thank you very much Santanu for your response. . Thanks for the advice on using best practices I will follow them. My current url page is as shown below: /apex/attachmentform?id=00Q2C000000XXXX It seems like my controller is not getting the respective leadID of the reference page. If I append a known leadID into the url then on that lead record I do get the attachment. – user23 Jul 4 '16 at 9:33

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.