0

I am currently working on an Inforce based integration between Salesforce and InforLN.

As part of this there are custom invoice objects: InForceEW__invoice__c

I am looking to attach PDF copies of invoices to each of these records (they're only about 20kb each so storage is no problem).

I have a flag on the invoice record (Attachment__c) to confirm that a PDF has been attached. This allows me to filter out records that already have attachments and avoid re-uploading an invoice.

I originally coded a trigger that worked by updating the Attachment__c when an attachment was added to an invoice record:

trigger PDFattach on Attachment (before insert) {
    //1 Create a set for IDs of parent objects related to attachments
    Set<ID> AttachIds = new Set<ID>();

    //2 Iterate through the attachments
    for (Attachment a : trigger.new){
        AttachIds.add(a.parentID);
    }

    //3 Use a SOQL query to extract relevant invoice records
    List<InForceEW__invoice__c> invos = [select id, Attachment__c from InForceEW__invoice__c where id in : AttachIds];

    //4 Iterate through the invoice records
    for (InForceEW__invoice__c i: invos){
        //4a update the Attachment field to True
        i.Attachment__c = true;
    }    
    //5 Update the list of invoice records
    update invos;
}

Unfortunately though this works well, navigating the invoice and then to attachments is a bit cumbersome on the Salesforce1 app.

I therefore added a field "PDF__c" on the invoice object which is a formula field for a hyperlink to the attached invoice.

An example of a hardcoded formula is:

HYPERLINK("https://c.ap1.content.force.com/servlet/servlet.FileDownload?file=00P90000008lazb","Invoice PDF")

The problem is that I'm having trouble working out how to store the ID of the attachment and replace the formula field with a hyperlink based upon the attachment ID.

Am I best looking at storing the ID in a field on the invoice object and referencing that field value in the hyperlink formula... is that possible? I've created a field for this: AttachID__c

I've tried to learn the use of maps to store the attachment ID and the parentID but I'm struggling. I'm having a lot of difficulty working out how to update the value of the AttachID field in the list of Invoice records from the attachment map.

This is where I got up to:

trigger PDFInvoice on Attachment (before insert) {
        //1 Create a Map for IDs of parent objects related to attachments and the value of the link to the attachment
    Map<String,ID> AttachIds = new Map<String, ID>();

    //2 Iterate through the attachments
    for (Attachment a : trigger.new){
        AttachIds.put(a.ID,a.parentID);
    }

    //3 Use a SOQL query to extract relevant invoice records
    List<InForceEW__invoice__c> invos = [select id, Attachment__c, AttachID__c from InForceEW__invoice__c where id in : AttachIds.ID];

    //4 Iterate through the invoice records
    for (InForceEW__invoice__c i: invos){
        //4a update the Attachment field to True
        i.Attachment__c = true;
        i.AttachID__c =  //DON'T KNOW HOW TO UPDATE THIS WITH THE RELEVANT MAP VALUE!
    }    
    //5 Update the list of invoice records
    update invos;

}

If anyone can provide any advice I'd greatly appreciate it - I'm determined to learn Apex and was really pleased to get the original trigger working, but right now I'm really struggling!

  • Can you tell me Which is parent and which is child Object...If I am correct you are trying to update "AttachID__c" with id ?? – user40138 Oct 7 '14 at 0:20
  • Hi user40138, I managed to solve the problem in the end (below). Thanks for looking into it though. Hope the solution clarifies what I was trying to achieve. Cheers – Cerbera Oct 7 '14 at 0:38
1

I had a couple of epiphanies over the last hour and have managed to get the code working!

I ended up creating the AttachID__c field on the invoice object (which is the parent of the attachment for invoice attachments). This field holds the ID of the attachment.

I then created a PDF__c formula field with a hyperlink to the attachment:

HYPERLINK("https://c.cs5.content.force.com/servlet/servlet.FileDownload?file="& AttachID__c ,"Invoice PDF")

In my trigger I was able to flag the parent object as having an object by setting a checkbox to true (Attachment__c), however I was having trouble passing the ID of the attachment to the parent invoice record.

I managed to get the maps to work and the attachment flag was been checked, but the attachment ID wasn't being populated. I eventually realised that was because I needed to switch to "After Insert".

So the final code for the trigger is:

trigger PDFInvoice on Attachment (after insert, after update) {
        //1 Create a Map for IDs of parent objects related to attachments and the value of the link to the attachment
    Set<ID> AttachIds = new Set<ID>();

    //2 Iterate through the attachments
    for (Attachment a : trigger.new){
        AttachIds.add(a.parentID);
    }

    //3 Use a SOQL query to extract relevant invoice records and Map the Invoice
    Map <Id, InForceEW__invoice__c> matchingInvoicesMap = new Map <Id, InForceEW__invoice__c> ();

    for (InForceEW__invoice__c invoice : [Select Id, Name From InForceEW__invoice__c Where Id IN :AttachIds])
    {
        matchingInvoicesMap.put(invoice.Id, invoice);
    }
    List <InForceEW__invoice__c> InvoicesToUpdate = new List <InForceEW__invoice__c> ();

    // go through the records in the trigger again and check whether we have found a matching invoice
    for(Attachment a: trigger.New)
    {
        if (matchingInvoicesMap.get(a.ParentId) != null)
        {
            // we found a matching one
            matchingInvoicesMap.get(a.ParentId) .AttachID__c = a.Id;
            matchingInvoicesMap.get(a.ParentId). Attachment__c = true;
            // add it to a separate list and update it
            InvoicesToUpdate.add(matchingInvoicesMap.get(a.ParentId));

        }
    }     
update InvoicesToUpdate;
}

Thanks for the support, it's great to have somewhere to come to :)

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.