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
AbAb 

Add attachment to Quote if added to a linked object

Hello,

I have a custoobject1__C, it has related list "Notes and Attachment" to it.

this object has lookup to Accont and Quote.

I want that, when an "Attachment" is added to custoobject1__C, it also adds to the corresponding "Quote".

How can i acheive it ?
Best Answer chosen by Ab
v varaprasadv varaprasad
Hi Sandrine.

Through trigger we can achieve this.
trigger AttachmentTriggerDemo on Attachment (before insert) {    
    id contactID;   
    for(Attachment att : trigger.New){      
        if(att.ParentId.getSobjectType() == contact.SobjectType){             
            contactID = att.ParentId;            
            Id accountId = [select id,name,accountid from contact where id =: contactID].accountid;
            system.debug('==accountId==='+accountId);
            Attachment attachment = new Attachment();
            attachment.Body = att.Body;
            attachment.Name = att.Name;
            attachment.ParentId = accountId; 
            insert attachment;
        }
    }
       
}





===========================================

trigger AttachmentTriggerDemo on Attachment (before insert) {
    id quoteid;   
    for(Attachment att : trigger.New){      
        if(att.ParentId.getSobjectType() == custoobject1__C.SobjectType){             
            contactID = att.ParentId;            
            Id accountId = [select id,quoteid from custoobject1__C where id =: contactID].quoteid;
            system.debug('==accountId==='+quoteid);
            Attachment attachment = new Attachment();
            attachment.Body = att.Body;
            attachment.Name = att.Name;
            attachment.ParentId = quoteid; 
            insert attachment;
        }
    }
       
}


First one is example like contact and account in second one please use your quoteid.


Thanks
Varaprasad