How to attach a file to Opportunity Lines

Some days ago, I received an email with a simple question. “How can I get some attachments related to my object?”

I know what you are thinking. The answer is easy. Actually we have 2 options, and both with #clicksnotcode:

  1. Add Attachment and Notes related list to your object.
  2. Enable Chatter on our object.

But her question didn’t end there. Actually she needed these files attached to a certain Standard object, Opportunity Product or Opportunity Line Item one. Ok, in that case above options do not work because we find some restrictions on this object.

Having this information, the only solution that came to my mind was to have a custom object as container where we can have the list of Attachments that we need to link to Opportunity Line.

  • New Custom Object – Attachment Container

Bellow example shows the easiest object that you can create with only Standard fields, and no custom field at all. But you can customize as much as you want.

  • Add Note and Attachments: 

Double check that Note and Attachments related list is in the page layout, so after creating a record, you can attach a new file.

  • New lookup field:

Now we would need to create a lookup field on Opportunity Line Item Standard object, so that, we can link each record to a single Attachment Container record, and its list of files.

Captura de pantalla 2015-06-06 a las 23.22.54

Having these objects structure, we would be able to use Apex code an access to all files like they were attached to Opportunity Lines directly.

We can only find a single issue. How can we avoid that the same Attachment Container record could be related to a single Opportunity record?

  • Create an Opportunity Line trigger:

Via a trigger, we could make sure that 2 Opportunity Lines will not try to use the same Attachment Container record, and be sure that files would be ONLY related to a single Opportunity Line record. In this way we can enforce a relationship of 1 to1 using lookup fields.

I would also like to raise the fact that we are doing validations on after actions. Why? What about if the end user decide to create its own trigger and modify the record during the before? Having our validations at the end, we will be sure that we would check final values. Please, find more information about validations on triggers on this blog entry.

trigger OpportunityLineTrigger on OpportunityLineItem (after insert, 
                                                       after update)
{
    //Retrieve attachments that already exist
    Map<Id, Id> currentAttachmentAndOppLines = new Map<Id, Id>();
    for(OpportunityLineItem currentOppLine : [Select Id, Attachment_Container__c
                                              From OpportunityLineItem])
    {
         currentAttachmentAndOppLines.put(currentOppLine.Attachment_Container__c,
                                          currentOppLine.Id);
    }

    if(Trigger.isAfter)
    {
        for(OpportunityLineItem oppLine : Trigger.new)
        {
            Id attachmentContainerId = oppLine.Attachment_Container__c;
            if(currentAttachmentAndOppLines.containsKey(attachmentContainerId))
            {
                if(Trigger.isUpdate)
                {
                    Id oppLineId = oppLine.Id;
                    if(currentAttachmentAndOppLines.get(attachmentContainerId) != oppLineId)
                    {
                        oppLine.addError('This Attachment Container is already in used.');
                    }
                }
            }
        }
     }
}

Do you have any other idea? Don’t be shy and share. I’m looking forward to knowing it !!

One thought on “How to attach a file to Opportunity Lines

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s