I recently came across a request in which the user wanted me to create a button to send a email when a button is clicked .
The email will contain the few fields
Code for the custom button in java
{!REQUIRESCRIPT("/soap/ajax/24.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/24.0/apex.js")}
var newQuoteId2 = '{!Quotes__c.Id}';
alert(newQuoteId2);
var pageUrl = sforce.apex.execute("makeOrder_SendEmail", "sendEmail",{newQuoteID:newQuoteId2});
Code for the class
global with sharing class {
webservice static void sendEmail(id newQuoteID){
//Creating a new email
List mails = new List();
profile pro = [SELECT id,Name FROM Profile where id = :UserInfo.getProfileId()];
system.debug('**** pro id='+ pro.Id + ' *** Name--'+ pro.Name);
Quotes__c custQuotesUpdateNew = new Quotes__c();
custQuotesUpdateNew = [SELECT id,Name,Quote_Name__c,Account_Customer__c,Phone_customer__c From Quotes__c WHERE Id =:newQuoteID LIMIT 1];
if(null != custQuotesUpdateNew){
Account relAcc = [SELECT Name,Website,Country__c,CS_Cust_No__c,Country_Calling_Code__c,Street_Name__c,Building_Name__c,City_or_Town__c,Fax FROM Account WHERE ID =: custQuotesUpdateNew.Account_Customer__c];
system.debug('**** relAcc --'+relAcc);
system.debug('**** custQuotesUpdate --'+custQuotesUpdateNew);
if(relAcc.CS_Cust_No__c == null && pro!= null && pro.name =='C/S UK'){
//new email
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
//send to
List sendTo = new List();
sendTo.add('piyushs.algo@gmail.com');
mail.setToAddresses(sendTo);
//sent from
mail.setReplyTo('next email');
mail.setSenderDisplayName('Info');
//The email
mail.setSubject('Quote ' +custQuotesUpdateNew.Name+' created without Customer Info');
String body = 'A quote with number : ' + custQuotesUpdateNew.Name+;
//body +='Account name' + custQuotesUpdateNew.Account_Customer__c;
body +='Quote created without the customer number on the Account ;
body +='Below are the details';
body +='Account Detail';
body +='Country -'+relAcc.Country__c+';
body +='Account Name -'+relAcc.Name +;
body +='Address Information';
mail.setHtmlBody(body);
//adding the email to the list
mails.add(mail);
system.debug('*******************'+mail);
}
Messaging.sendEmail(mails);
//sending the email
system.debug('*******************'+mails);
}
}
}