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
Leonard Silon 8Leonard Silon 8 

How to check a box if there is a specific attachment on a contact

On the contact if a user attaches a file ot the notes and attachments with a file name that begins with "Opt-Up Professional" that a check box named Opt-Up Attached gets checked. I have read enough to know a trigger is needed. However, I can not find anythign specific to my needs. Does anyone have code they can share with me - - please note while i can basically read code, I am not a developer. We also have no developer on site so any help would be greatly appreciated!!
Best Answer chosen by Leonard Silon 8
Pratima Shrivastav 3Pratima Shrivastav 3
I think you just updated your existing trigger to Attachment. Try creating a new trigger on Attachment object.

All Answers

Pratima Shrivastav 3Pratima Shrivastav 3
Hi Leonard,
Something like this should help.
trigger attachmentInserted on Attachment (after insert, after delete) {
    Schema.DescribeSObjectResult  r = Contact.sObjectType.getDescribe();
    String keyPrefix = r.getKeyPrefix();
    
    if(trigger.isInsert){
        for(Attachment att : trigger.New){
            if(keyPrefix == String.valueOf(att.ParentId).left(3)){
                Contact con =  [SELECT Id, Document_Attached__c from Contact WHERE Id = :att.ParentId LIMIT 1];
                con.Document_Attached__c = True;
                update con;
            }
        }
    }
    
    

}

It might not be following all the standards but it works. Also, you can add a if(trigger.isDelete) so that the checkbox gets unchecked if someone deletes the attachments.
Let me know if it helps.

Thank you
Pratima Shrivastav 3Pratima Shrivastav 3
My bad. I didnt read your complete question. You are looking for the name of attachment.
Pratima Shrivastav 3Pratima Shrivastav 3
Try this, added a check to see if the filename starts with Test, modify it accordingly.
trigger attachmentInserted on Attachment (after insert, after delete) {
    Schema.DescribeSObjectResult  r = Contact.sObjectType.getDescribe();
    String keyPrefix = r.getKeyPrefix();
    
    if(trigger.isInsert){
        for(Attachment att : trigger.New){
            if(keyPrefix == String.valueOf(att.ParentId).left(3) && (att.Name).startsWith('Test') ){
                Contact con =  [SELECT Id, Document_Attached__c from Contact WHERE Id = :att.ParentId LIMIT 1];
                con.Document_Attached__c = True;
                update con;
            }
        }
    }
    
    

}

 
Leonard Silon 8Leonard Silon 8
I will try – thank you Thank you Leonard Silon Senior Salesforce.com Manager [cid:image001.png@01D34406.70AFFA70] [cid:image002.png@01D34406.70AFFA70] [cid:image005.png@01D34406.70C0C350] Create a Retail Salesforce Support Case Create an Institutional Salesforce Support Case
Leonard Silon 8Leonard Silon 8
@Pratima Shrivastav 3 I tried your code and substsituted my API field name and file name. I am getting an error I am not sure how to fix. It seems to ber saying the trigger should be on the contact but you have suggested it be on the attachment. Can you decipher please?

User-added image
Leonard Silon 8Leonard Silon 8
Bigger font for error message

Trigger name, checkboxifattached, exists on different SObject type: Contact
Pratima Shrivastav 3Pratima Shrivastav 3
Hi Leonard,
Looks like you have a trigger with the same name on Contact object also. Can you please check if thats the case?
Leonard Silon 8Leonard Silon 8
I just created this trigger with this unique name. No other trigger exists with this unique name Thank you Leonard Silon Senior Salesforce.com Manager [cid:image001.png@01D3440B.9D0B2A40] [cid:image002.png@01D3440B.9D0B2A40] [cid:image005.png@01D3440B.9D1BF320] Create a Retail Salesforce Support Case Create an Institutional Salesforce Support Case
Pratima Shrivastav 3Pratima Shrivastav 3
I think you just updated your existing trigger to Attachment. Try creating a new trigger on Attachment object.
This was selected as the best answer
Leonard Silon 8Leonard Silon 8
My fault. I fixed it. Now let me test and let you know – BTW I REALLY appreciate the help Thank you Leonard Silon Senior Salesforce.com Manager [cid:image001.png@01D3440B.F17CE6E0] [cid:image002.png@01D3440B.F17CE6E0] [cid:image005.png@01D3440B.F19291C0] Create a Retail Salesforce Support Case Create an Institutional Salesforce Support Case
Leonard Silon 8Leonard Silon 8
it worked
Pratima Shrivastav 3Pratima Shrivastav 3
Great.. Now you will need to make sure if you want this to be unchecked if the attachment is deleted, try adding another block just like this. use trigger.isDelete and set the value to false instead. This is if you need this criteria.But you will definitely need a test class in order to deploy this to production.
Let me know if you need help with this and mark it as best answer so that it can be helpful to others.
Thank you
 
Leonard Silon 8Leonard Silon 8
I marked yours as best answer already and yes I would need that test class please Thank you Leonard Silon Senior Salesforce.com Manager [cid:image001.png@01D34410.2F90F170] [cid:image002.png@01D34410.2F90F170] [cid:image005.png@01D34410.2FA0CFF0] Create a Retail Salesforce Support Case Create an Institutional Salesforce Support Case
Pratima Shrivastav 3Pratima Shrivastav 3
Hi, Below is the test class for insert case. This will run in admin level since I didnt insert a test user. Male sure to read my comments to match it up to your case.
@isTest
public class TestAttachmentInserted {
    static testMethod void testTriggerAttch(){
        //insert a test contact record with all the required fields in your org otherwise the test will fail. 
        Contact c = new Contact(FirstName = 'Test', LastName = 'Testing');
        insert c;
        
        //insert attachment. make sure the attachment file Name is same as what you used in trigger
        Attachment attach = new Attachment();     
        attach.Name='Test Attachment';
        Blob bodyBlob=Blob.valueOf('Unit Test Attachment Body');
        attach.body=bodyBlob;
        attach.parentId = c.id;
        insert attach;
        
    }
}

After saving your test class, click on Run Test and then go back your trigger to see if it is 100% covered.
Leonard Silon 8Leonard Silon 8
I wish we had enough money to hire you. Test class worked too. So very much appreciated
Pratima Shrivastav 3Pratima Shrivastav 3
Haha.. Glad I could help! Will be happy to help as long as time permits.I try to answer questions if I know solutions to them or if I know I can find a solution. It's all about learning. Recently started a blog. http://trail.withneha.com