+ Start a Discussion
HTANIRSHTANIRS 

Trigger on Attachment to Insert Quote

Hi Friends,

Please help on below Trigger. I am trying to insert Quote when the attachment is added to Opporunity and the file name contains PDF.

trigger oppattachment on Attachment (after insert, after update) {
    
    Set <Id> oppIds = new Set <Id>();    
    for(Attachment attach : Trigger.new) {
        if(attach.ParentId.getSobjectType() == Opportunity.SobjectType) {
            if(string.valueOf(attach.Name).contains('.pdf')) {
                oppIds.add(attach.ParentId);
            }
        }
    }
    List <Quote> quoteList = [SELECT id, name, opportunityid FROM Quote WHERE Id IN: oppIds];
    for(Attachment attach : Trigger.new) {
        if(quoteList !=null && quoteList.size()>0){
            for(quote q : quotelist){    
                q.name = attach.name;       
            }
            insert quoteList;
        }
    }                
}

Thanks.
Best Answer chosen by HTANIRS
ManojjenaManojjena
Hi HTANIRS,

You want to create a new Quote under that opportunity what I understood ,here you are querying from existing quote and you are again inserting .
Please clarify your requirment ,if you are adding new quote what will be default field values for the new quote .

Let us know your requirmnet with more clarity .Check with below code it may help !!
 
trigger oppattachment on Attachment (after insert, after update) {
    List<Quote> qtListToInsert= new List<Quote>();
    Map<Id,Id> attidWithOppIdMap= new Map<Id,Id>();    
    for(Attachment attach : Trigger.new) {
        if(attach.ParentId.getSobjectType() == Opportunity.SobjectType) {
            if(string.valueOf(attach.Name).contains('.pdf')) {
                attidWithOppIdMap.put(attach.id,attach.ParentId);
            }
        }
    }
	if(!attidWithOppIdMap.isEmpty()){
		for(Attachment attach : Trigger.new) {
		    //Needs to add other fields below
			Quote qt = new Quote();   
		    qt.name = attach.name;
			qt.OpportunityId=attidWithOppIdMap.get(attach.id);
            qtListToInsert.add(qt);					
		}
		if(!qtListToInsert.isEmpty()){
		   try{
		      insert qtListToInsert;
		   }catch(DmlException de ){
		      Syste.debug(de.getMessage());
		   }
		}
    }                
}

Please let me know if it helps !!!
Thanks 
Manoj 

 

All Answers

ManojjenaManojjena
Hi HTANIRS,

You want to create a new Quote under that opportunity what I understood ,here you are querying from existing quote and you are again inserting .
Please clarify your requirment ,if you are adding new quote what will be default field values for the new quote .

Let us know your requirmnet with more clarity .Check with below code it may help !!
 
trigger oppattachment on Attachment (after insert, after update) {
    List<Quote> qtListToInsert= new List<Quote>();
    Map<Id,Id> attidWithOppIdMap= new Map<Id,Id>();    
    for(Attachment attach : Trigger.new) {
        if(attach.ParentId.getSobjectType() == Opportunity.SobjectType) {
            if(string.valueOf(attach.Name).contains('.pdf')) {
                attidWithOppIdMap.put(attach.id,attach.ParentId);
            }
        }
    }
	if(!attidWithOppIdMap.isEmpty()){
		for(Attachment attach : Trigger.new) {
		    //Needs to add other fields below
			Quote qt = new Quote();   
		    qt.name = attach.name;
			qt.OpportunityId=attidWithOppIdMap.get(attach.id);
            qtListToInsert.add(qt);					
		}
		if(!qtListToInsert.isEmpty()){
		   try{
		      insert qtListToInsert;
		   }catch(DmlException de ){
		      Syste.debug(de.getMessage());
		   }
		}
    }                
}

Please let me know if it helps !!!
Thanks 
Manoj 

 
This was selected as the best answer
Sumeet_ForceSumeet_Force
Quote SOQL needs to query using opportunityId in where clause.Also if you are inserting quotelist, you cannot set ID on that field.
HTANIRSHTANIRS
Hi Manoj,
  
Thanks for your reply.
My requirement is : When I add the attachment in Opportunity I need to create a New Quote with the Attachment name which has extension .pdf. If it has 3 PDF I need to create 3 Quote.
Also, I need to copy all the attachments to Quote that are related to that opportunity.

Your Code works perfect.

For my 2nd requirement I have written in Quote Object to copy Attachments from Opportunity to Quote.

Please let me know if you have any suugestions to handle this requirement.

Regards,
Srinath.