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
Olanre AwolowoOlanre Awolowo 

How to create a trigger if a i have made an attachment on an account record, make the checkbox ticked automatially with Apex trigger urgent

Hi i have been given a task at work which requires me coding with apex trigger
Please see below:

The Request:
“We need a notification to be sent to the fraud department whenever an attachment gets edited on the Account record. Please can you set this up?”
 I would suggest keeping the following in mind when you approach this scenario;
--------------------------------------
The task looks straight forward. I have created a checkbox that will return true if an attachment have been uploaded on the account record, later on i will create a workflow that if the checkbox ticked = TRUE, send email to the users. 
I have start to code this but i keep top get error such as:


trigger Attachmentchangenotif on Account (before update, before insert) {

/**    for(Account c: trigger.new){
   If  (label.attachments ==  True)

That is just thebit i know. I have created a checkbox called attachment trigger. 

I need help please
Best Answer chosen by Olanre Awolowo
Amit Singh 1Amit Singh 1
Yeah you are right.

Thanks

All Answers

Amit Singh 1Amit Singh 1
Ok, try below code
trigger attachmentChangeOnAccount on Account(After Update){
        Set<Id> accountIdSet = new Set<Id>();
        List<Account> accountList = new List<Account>();
        for(Attachment att: Trigger.New){
           if(att.ParentId!=null && att.ParentId.startsWith('001')){
                if(att.Name!=Trigger.oldMap.get(att.id).Name || att.body!=Trigger.oldMap.get(att.id).body){
                    accountIdSet.add(att.ParentId);
                }
             }
        }
        accountList = [Select Id, Name, Attachment_Trigger__c From Account Where Id In :accountIdSet];
        for(Account a: accountList){
            a.Attachment_Trigger__c = true;
        }
        if(accountList!=null && accountList.size()>0)update accountList;
        
}


Let me know if this helps :)

Thanks,
Amit Singh
Olanre AwolowoOlanre Awolowo
Hi Amit, 
Thanks for the response 

I have used the code you gave me and I inserted it. The only thing I change is the "Attachment_Trigger__c "  to "AccountTrigger which is the right field for this (This is a checkbox field type)
Please see error and advise where i did mistake 
Thanks 

User-added image
 
Olanre AwolowoOlanre Awolowo
Still gives the error> where do you think i am making mistake
Amit Singh 1Amit Singh 1
Hi Olane,

Do you mean, if someone changes any attachment like Name or Body then make that checkbox to true? Use the below code trigger should be written in Attachment.
 
trigger attachmentChangeOnAccount on Attachment(After Update){
        Set<Id> accountIdSet = new Set<Id>();
        List<Account> accountList = new List<Account>();
        for(Attachment att: Trigger.New){
           if(att.ParentId!=null && att.ParentId.startsWith('001')){
                if(att.Name!=Trigger.oldMap.get(att.id).Name || att.body!=Trigger.oldMap.get(att.id).body){
                    accountIdSet.add(att.ParentId);
                }
             }
        }
        accountList = [Select Id, Name, Attachment_Trigger__c From Account Where Id In :accountIdSet];
        for(Account a: accountList){
            a.Attachment_Trigger__c = true;
        }
        if(accountList!=null && accountList.size()>0)update accountList;
        
}

Believe that It will do the trick :)

Thanks,
Amit Singh
Olanre AwolowoOlanre Awolowo
Hi Amit, 
Thanks for the quick response, really pleased. What i meant is if someone will change the attachment file. Such as if a user changes the attachment on the account record 
Amit Singh 1Amit Singh 1
Use the above code that should be work :)

Thanks
Olanre AwolowoOlanre Awolowo
Hi Amit, 
Thanks for the quick response, really pleased. What I meant is if someone changes the attachment file, Such as if a user changes the attachment on the account record. In this case, if this has been changed, i want the checkbox field TriggerAccount_c to be Ticked automatically

 
Olanre AwolowoOlanre Awolowo


Still gives me error :( . I think we almost there now.. Just the Line 5 behaving tricky
User-added image
Amit Singh 1Amit Singh 1
Fixed the bug, use below code.
 
trigger attachmentChangeOnAccount on Attachment(After Update){
        Set<Id> accountIdSet = new Set<Id>();
        List<Account> accountList = new List<Account>();
        for(Attachment att: Trigger.New){
           if(att.ParentId!=null && String.valueOf(att.ParentId).startsWith('001')){
                if(att.Name!=Trigger.oldMap.get(att.id).Name || att.body!=Trigger.oldMap.get(att.id).body){
                    accountIdSet.add(att.ParentId);
                }
             }
        }
        accountList = [Select Id, Name, Attachment_Trigger__c From Account Where Id In :accountIdSet];
        for(Account a: accountList){
            a.Attachment_Trigger__c = true;
        }
        if(accountList!=null && accountList.size()>0)update accountList;
        
}
Thanks,
Amit
 
Olanre AwolowoOlanre Awolowo
User-added image

Still error, it regarding the body
Amit Singh 1Amit Singh 1
You have written trigger on Account it should be Attachment.

Thanks,
Olanre AwolowoOlanre Awolowo
WOW, this is just amazing unbelievable. Many thanks for the help. I really appreciate. 
Just a last question then i leave you, What of if i want to make it to be possible if i upload a new attachment the checkbox should be ticked automatically for example. 

That my last question. What isyour email by the way, will like to have it 
Thanks 
Olanre 
 
Olanre AwolowoOlanre Awolowo
What i am trying to do is if that AccountTrigger_c checkbox is ticked, I want to send an email notification with workflow rule email to some roles that the attachment has been changed
Thanks 
Amit Singh 1Amit Singh 1
If you want to make checkbox true after insert and after the update then use below code.
trigger attachmentChangeOnAccount on Attachment(After Update, After Insert){
        Set<Id> accountIdSet = new Set<Id>();
        List<Account> accountList = new List<Account>();
        If(Trigger.IsInsert){
            for(Attachment att: Trigger.New){
               if(att.ParentId!=null && String.valueOf(att.ParentId).startsWith('001')){
                    accountIdSet.add(att.ParentId);
                 }
            }
        }
        if(Trigger.IsUpdate){
            for(Attachment att: Trigger.New){
               if(att.ParentId!=null && String.valueOf(att.ParentId).startsWith('001')){
                    if(att.Name!=Trigger.oldMap.get(att.id).Name || att.body!=Trigger.oldMap.get(att.id).body){
                        accountIdSet.add(att.ParentId);
                    }
                }
             }
        }
        accountList = [Select Id, Name, Attachment_Trigger__c From Account Where Id In :accountIdSet];
        for(Account a: accountList){
            a.Attachment_Trigger__c = true;
        }
    if(accountList!=null && accountList.size()>0)update accountList; 
}
Let me know if this works :)
cse.amitallenhouse@gmail.com

Mark as best answer if this works.

Thanks,
Amit Singh
Olanre AwolowoOlanre Awolowo
It works perfectly, Many thanks. I am in the process of creating the workflow rule and email template after this has been ticked.
after that I will hide the checkbox field.
So for this apex code, now each time I insert a new attachment or make an update a new one the checkbox will be ticked. what about if the name of the attachment gets edited?
Many thanks 
Olanre 
 
Amit Singh 1Amit Singh 1
If checkbox will be checked then it will not update account if not it will update the account.
accountList = [Select Id, Name, Attachment_Trigger__c From Account Where Id In :accountIdSet];
Boolean isUpdate = false;
for(Account a: accountList){
    If(a.Attachment_Trigger__c==false){
        a.Attachment_Trigger__c = true;
        isupdate = true;
    }
}

if(accountList!=null && accountList.size()>0 && isupdate)update accountList;

Make this small adjustment in your trigger. :)

Thanks,
Amit
Olanre AwolowoOlanre Awolowo
Many Thanks.
Appreciate
Olanre AwolowoOlanre Awolowo
So with this piece of code you gave me, to understand it:
each time an update is made, email will fire with workflow, Each time i edit the name of the attachment with edit email will be sent?

Many thanks 
Olanre
Amit Singh 1Amit Singh 1
Yeah you are right.

Thanks
This was selected as the best answer
Olanre AwolowoOlanre Awolowo
Hi again, sorry for the disturb, 
What of if someone deletes the attachment, is possible to make another checkbox ticked? 
I will have to create another custom checkbox to make it ticked if the attachment is deteleted to tick the checkbox automatically. 

I will like the piece of code. 
Ps are you on LinkedIn i want to endorse you 

Thanks 
Olanre 
Olanre AwolowoOlanre Awolowo
Also Looking at the code the email fires just once. if I upload another attachment the email does not fire, it is definitely sure that is because of the checkbox being ticked already.

How do you think i can improve this . 
I just want to make that if there is updates such as new attachment/attachment has been deleted, an email should be sent  and this is each time

Thanks
Olanre AwolowoOlanre Awolowo
HiAmit, 
Dont worry about the question i asked you, Last help i will need will be if you can code the delete Query for me. 
I want when an attachment is deleted, checkbox should be ticked automatically.
Thanks 
Olanre AwolowoOlanre Awolowo
All sorted and all resolved. Managed to code the delete apex
Thanks for the help