function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Andrew Hoban 6Andrew Hoban 6 

Requirement on Adding Attachment when Record Field is met

Hi all,

Is it possible to add a requirement/validation on a record to make sure that an atttachment is uplodaed before it is saved?

Thanks
Best Answer chosen by Andrew Hoban 6
bob_buzzardbob_buzzard
You'd do this via a trigger, so the recId would come from the records passed into the trigger, however you'd also want to make sure the trigger could handle multiple records, so you'd need be better off using an aggregate query to pull the attachment counts and group by the parent id. You can then iterate the trigger records and mark those with no attachments as being in error. Sounds complicated, but its actually quite straightforward - here's an example based on the Account sobject.  Caveat emptor, I haven't tried the trigger so there might be the odd typo, but the internal logic works fine in my dev org.
 
trigger account_bu on Account (before update)
{
    List<AggregateResult> ars=
        [select ParentId, count(id) from Attachment 
        where ParentId in :Trigger.new group by ParentId];

    Map<Id, Integer> attCountsByAccId=new Map<Id, Integer>();
    for (AggregateResult ar : ars) 
    {
        Id parentId=(Id)ar.get('ParentId');
        Integer count=(Integer) ar.get('expr0');
        attCountsByAccId.put(parentId, count);
    }

    for (Account acc : trigger.new)
    {
        Integer attCount=attCountsByAccId.get(acc.id);
        if ( (null==attCount) || (0==attCount) )
        {
            acc.addError('Record has no attachments');
        }
   }
}


 

All Answers

bob_buzzardbob_buzzard
You can't add an attachment to a record before it is saved, as the attachment is associated with the parent record via the parent record's ID.  If you want to check that a record has an attachment before it is updated into a particular state, you will need a trigger that queries the attachments associated with the record. There's an example of this (written by me, it turns out!) at :

https://developer.salesforce.com/forums?id=906F00000008zkXIAQ
Andrew Hoban 6Andrew Hoban 6
Thank you. So for recid, would you need to create a class to define that vaiable?
bob_buzzardbob_buzzard
You'd do this via a trigger, so the recId would come from the records passed into the trigger, however you'd also want to make sure the trigger could handle multiple records, so you'd need be better off using an aggregate query to pull the attachment counts and group by the parent id. You can then iterate the trigger records and mark those with no attachments as being in error. Sounds complicated, but its actually quite straightforward - here's an example based on the Account sobject.  Caveat emptor, I haven't tried the trigger so there might be the odd typo, but the internal logic works fine in my dev org.
 
trigger account_bu on Account (before update)
{
    List<AggregateResult> ars=
        [select ParentId, count(id) from Attachment 
        where ParentId in :Trigger.new group by ParentId];

    Map<Id, Integer> attCountsByAccId=new Map<Id, Integer>();
    for (AggregateResult ar : ars) 
    {
        Id parentId=(Id)ar.get('ParentId');
        Integer count=(Integer) ar.get('expr0');
        attCountsByAccId.put(parentId, count);
    }

    for (Account acc : trigger.new)
    {
        Integer attCount=attCountsByAccId.get(acc.id);
        if ( (null==attCount) || (0==attCount) )
        {
            acc.addError('Record has no attachments');
        }
   }
}


 
This was selected as the best answer
Andrew Hoban 6Andrew Hoban 6
Thank you, this has provided he validation error message when i attempt to save a record. Is there a way of showing this error message once the record has been saved and the user goes back to edit the record at all?
bob_buzzardbob_buzzard
Not easily. The best that you could do is a formula field that displays a message, but the trigger count is derived dynamically rather than stored on the record, so you'd another set of triggers that updated the attachment count on the record as users added or removed attachments.