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
Nathan Prats 22Nathan Prats 22 

Trigger Push AttachmentId To ParentId Custom Field

Hi, 

My need is quite simple. I need to create a button on the Quote object to send an email with the last attachment attached. 
I already created the button: 

/_ui/core/email/author/EmailAuthor?
p2_lkid=0032X000023Oapf&
rtype=003
&p3_lkid={!QuoteId}&
doc_id=AttachmentId&
&retURL=%2F0{!QuoteId}


I need to Push the AttachmentId To ParentId Custom Field after the Attachement is inserted with a Trigger. 
trigger PushAttachmentIdToParentIdCustomField on Attachment (After Insert) {
    
    

}
Only problem is... I have no idea where to start. 
Any help is more than welcome as I'm fairly new to Apex Development.

Nathan
 
Best Answer chosen by Nathan Prats 22
v varaprasadv varaprasad
Hi Nathan,

Please check following sample code : 
 
trigger PushAttachmentIdToParentIdCustomField on Attachment (After Insert) {
  List<account> lstAcc = new list<account>();
	
    for(Attachment a:trigger.new) {
        if(a.ParentId!=NULL){
		     account ac = new account();
			 ac.id = a.ParentId;
			 ac.customfield__c = a.id; //attachment id assigning to customfield//
		     lstAcc.add(ac);
           
        }
    }
	
	if(lstAcc.size()>0){
	  update lstAcc;
	}
}

Hope this helps you!


Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
 

All Answers

Nathan Prats 22Nathan Prats 22
Tried something like this. But it doesn't work.
 
trigger PushAttachmentIdToParentIdCustomField on Attachment (After Insert) {
    
    for(Attachment a:trigger.new) {
        if(a.ParentId!=NULL){
            a.ParentId.Last_Attachement_ID__c == a.Id;
        }
    }
}

 
v varaprasadv varaprasad
Hi Nathan,

Please check following sample code : 
 
trigger PushAttachmentIdToParentIdCustomField on Attachment (After Insert) {
  List<account> lstAcc = new list<account>();
	
    for(Attachment a:trigger.new) {
        if(a.ParentId!=NULL){
		     account ac = new account();
			 ac.id = a.ParentId;
			 ac.customfield__c = a.id; //attachment id assigning to customfield//
		     lstAcc.add(ac);
           
        }
    }
	
	if(lstAcc.size()>0){
	  update lstAcc;
	}
}

Hope this helps you!


Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
 
This was selected as the best answer
v varaprasadv varaprasad
trigger PushAttachmentIdToParentIdCustomField on Attachment (After Insert) {
  List<Quote> lstAcc = new list<Quote>();
    
    for(Attachment a:trigger.new) {
        if(a.ParentId!=NULL){
             Quote  ac = new Quote();
             ac.id = a.ParentId;
             ac.customfield__c = a.id; //attachment id assigning to customfield//
             lstAcc.add(ac);
           
        }
    }
    
    if(lstAcc.size()>0){
      update lstAcc;
    }
}
Nathan Prats 22Nathan Prats 22
Replaced it by Quote instead of accounts. Works great now thanks !!
 
trigger PushAttachmentIdToParentIdCustomField on Attachment (After Insert) {
  List<Quote> lstAcc = new list<Quote>();
	
    for(Attachment a:trigger.new) {
        if(a.ParentId!=NULL){
		     Quote Q = new Quote();
			 Q.id = a.ParentId;
			 Q.Last_Attachement_ID__c = a.id;
		     lstAcc.add(Q);
           
        }
    }
	
	if(lstAcc.size()>0){
	  update lstAcc;
	}
}