[SalesForce] Link upload file to Lead Record

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

Best Answer

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.

Related Topic