Send Quote Pdf to Contact Person and Save the Quote PDF in attachement

Requirement: Need to create the save and send Quote Pdf custom button on Quote detail page and when user click on this button quote pdf as attachment send to quote contact person and pdf attached in attachment under quote

Solution:

1) Need to create web service class

 2 ) Create a custom button as a Detail page button and  Behavior as Execute Java Script and content source is as onClick Java Script and call the web service class.

WebServiceClass Name:

global class clsSaveQuotePDFInAttachmentUnderQuote
{
     webservice static String AttachPDFToQuote(string Id){
      
        string strResponse = '';
        try
        {
            PageReference pageRef = new PageReference('/apex/VFPageName?Id='+Id);
            
            Blob content;
            if(!Test.isRunningTest())
                content = pageRef.getContent();
            else
                content = Blob.valueOf( EncodingUtil.urlEncode('abc', 'UTF-8'));
                
            QuoteDocument doc = new QuoteDocument(Document = content, QuoteId = Id);
            insert doc;

            Quote ObjQuote = [select id,Name,Contact.name,email from quote where id = : Id limit 1];
            
            if(ObjQuote!=null) {   
                if(ObjQuote.email!=null){
                    Messaging.SingleEmailMessage Objemail = new Messaging.SingleEmailMessage();
                    Messaging.EmailFileAttachment ObjEmailFile = new Messaging.EmailFileAttachment();
                    list<String> LstEmailAddress = new list<String>();
                    string strEmailBody='';
                    ObjEmailFile.setFileName(ObjQuote.Name+'Quotation.pdf');
                    ObjEmailFile.setBody(content);                           
                    LstEmailAddress.add(ObjQuote.email);   
                    strResponse = ObjQuote.email;
                    if(ObjQuote.Contact.name!=null){
                        strEmailBody = 'Hi '+ ObjQuote.Contact.name+',';
                    }else{
                        strEmailBody = 'Hi,';
                    }
                    
                    strEmailBody = strEmailBody + '\n' + '\n'+' Below is the quotation suggested by Team regarding to Quote : '+ObjQuote.name+ '\n' + '\n';
                    strEmailBody = +'\n'+'\n'+strEmailBody +'\n' + 'Thank you.';
                    Objemail.setSubject(ObjQuote.Name+' Quotation');
                    Objemail.setToAddresses(LstEmailAddress);
                    Objemail.setPlainTextBody(strEmailBody);
                    Objemail.setFileAttachments(new Messaging.EmailFileAttachment[] {ObjEmailFile});
            
                    Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {Objemail});
                }
                
            }
         
            return strResponse;
        }
        catch(exception ex) {
           System.debug('--- @@@@@Error ----------'+ ex);
           strResponse = 'Error while sending Quote!!!';
           return strResponse;
        }
    }
}



Custom Button Code



{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")} 
var emails="{!Quote.Email}"; 
if(emails!=''){ 
if(confirm("Do you want to save and send Quote to: " +emails)){ 

if("{!Quote.Status}"!="Pending") 

if("{!Quote.Email}"!="") {

var result = sforce.apex.execute('clsSaveQuotePDFInAttachmentUnderQuote','AttachPDFToQuote',{Id:"{!Quote.Id}"}); 
//var result = sforce.apex.execute('webserviceClassName','MethodName',{parameterName:"{!Quote.Id}"}); 
if(result=="Error While Sending Quote.") 

alert( result ); 
window.location.reload(); 

else { 
window.location.reload(); 


else { 
alert("Please update Email id Of Contact Person"); 
window.location.reload(); 

}

// add condition according to requirement 
else if("{!Quote.Status}"=="Finalize") { 

alert("Quote has been sent already"); 
window.location.reload(); 
}



}

else{ 
alert('Please update Email Address Of Contact Person.'); 
}


                                       Thanks,
                                    Sumit  Shukla

                                   Mob:9711055997
                                  Skype: shuklasumit1
                                  Email:   sumitshukla.mca@gmail.com

Comments

Popular posts from this blog

Salesforce Spring 16 Release Exam (Maintenance Exam Q&A) for Developer 201 Admin

Show Hyper Link On Add Error in Salesforce Trigger

Show the Success Message before Redirecting the Detail page on Visualforce Page