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
Diwakar GDiwakar G 

Trigger on quote

The records of quote object contain notes and attachments. Trigger should be executed after a new attachment is added for a record. I don't want to create trigger on attachment object. I need to create on Quote object. How can I do this.
NitishNitish
Hi Diwakar,

In this scenario you can write a trigger to count the number of Attachments on records and then whenever the No of Attachment will increase fire your trigger.

Let me know in case of any query.

Thanks,
Nitish 
Diwakar GDiwakar G
Can you please elaborate, how to do this.
NitishNitish
Hi Diwakar,

If you dont want to write trigger then you can use this code to count no of attachment on Quote. For this you need one custom Number field of number type and you can remove it from Page Layout.
trigger QuoteAttachmentsTrigger on Quote (before update,after update) {

    if(Trigger.isUpdate && Trigger.isBefore){
        
        Set<String> parentId=new Set<String>();
        for(Quote q:Trigger.new){
            parentId.add(q.Id);
        }
        
        List<Quote> quoteList=[SELECT Id,No_of_Attachemnts__c,(SELECT Id,ParentId FROM Attachments) From Quote WHERE Id in : parentId];
        Map<Id,Integer> quoteMap=new Map<Id,Integer>();
        List<Quote> updateQuote=new List<Quote>();
        for(Quote q1:quoteList){
           quoteMap.put(q1.Id,q1.attachments.size()); 
        }
        
        for(Quote q2:Trigger.new){
            q2.No_of_Attachemnts__c=quoteMap.get(q2.Id);
            
        }
       
    }
}

But the problem with this approch is it will only count the Attachment when this perticular Quote will get edit. If you will write this trigger on Attachment then whenever you will insert any attachment it will update the Parent Quote object and then you can write your Trigger logic in Quote trigger on No of Attchamnet Field change.  

Thanks,
Nitish