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
KruviKruvi 

Sending an email on a click of a button

Hi

 

I have an Invoice object and I want to create a button that on-click will send an email with the related Invoice information to the customer.

 

I have created an email template for this but I'm not sure how to write the APEX code such that when I mark an Invoice and press the button an email will be sent to the contact designated in the invoice including the rest of the detailes in the invoice.

 

If not APEX is there any other way to achieve the same?

 

Thanks

 

 

Best Answer chosen by Admin (Salesforce Developers) 
b-Forceb-Force
{!REQUIRESCRIPT("/soap/ajax/10.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")} 
{!REQUIRESCRIPT("/js/functions.js")} 

sforce.debug.trace=true; 

var records = {!GETRECORDIDS( $ObjectType.Invoice__c )}; 

if(records[0] == null)
{ 
alert("Please select at least one record.") 
} 
else
{ 

try
{ 
for(var i=0; i<records.length; i++) 
{ 
var objInvoice = new sforce.SObject("Invoice__c"); 
objInvoice.Id = records[i]; 
objInvoice.Email_Invoice__c=true; 
var saveRes=sforce.connection.update([objInvoice]); 
} 
if (saveRes[0].success=='false')
{ 
alert(saveRes[0].errors.message); 
} 
} 
catch(error)
{ 
alert("Error while Sending Email:=="+error); 
}
}

 

try this one. It will work

Let me know if you need any more help

 

Cheers,

Bala

All Answers

sfdcfoxsfdcfox

I would recommend a "Custom Link", since you're just using basic information. Apex Code can also send the email, but that's akin to using an elephant gun on a mouse. Setup > Create > Objects > Inventory > Custom Buttons & Links > New

 

The URL is:

 

 

/_ui/core/email/author/EmailAuthor?p2_lkid={!Inventory__c.Contact__c}&p3_lkid={!Inventory__c.Id}&retURL=/{!Inventory__c.Id}&template_id=00XX0000000XXXX&save=1

Where 00XX0000000XXXX is the email template to use.

 

anil 007anil 007

hiii

 

it may possible with vf page and apex class

 

try it

KruviKruvi

Cool, I'll try it now using a custom link.

 

Will this method with a custom link also allow me to mark multiple invoices and send them?

 

Many Thanks

 

 

KruviKruvi

Thank you for the idea, I got the EmailAuthor to work but this takes me to a page where I have to put additional email addresses and press send.

I have many such invoices to email. Is there any way to automate this?

 

Thanks

b-Forceb-Force

ok,

we will automate your functionality with just one click. we can achive this by Workflow + Email Alert Combination

 

Here is approach

  1. Create the Boolean Field on Invoice object [let us say SendEmail__c]
  2. Dont place these field on any page Layout
  3. Create Workflow with Rule Entry Criteria = Every Time Record created/Edited and SendEmail__c=true
  4. Add Email alert as action for above rule
  5. you can specify Email template and Additional Email address here
  6. Add one more field Update to set SendEmail__c=false [This will useful to send email multiple times]
  7. Now create Detail Page Button with with Content Source as javascript [we need to update above field in javascript]

 

{!REQUIRESCRIPT("/soap/ajax/10.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")}
{!REQUIRESCRIPT("/js/functions.js")}
try
{
var objInvoice = new sforce.SObject("Invoice__c");
objInvoice.Id = "{!Invoice__c.Id}";
objInvoice.SendEmail__c=true;
var saveRes=sforce.connection.update([objInvoice]);
if (saveRes[0].success=='false') 
{
     alert(saveRes[0].errors.message);
}
}
catch(error)
{
alert("Error while Sending Email:=="+error);
}

 

 

**** be careful   --

replace  Invoice__c by your custom Invoice object name

Create new boolean field with same API name SendEmail__c

 

 

This will send Email every single Invoice information to particulars

If you want to send Emails to multiple invoice at one click , we need to embed same logic for multiple records

 

 

Hope This will help you

 

Thanks,

Bala

 

 

 

 

 

 


KruviKruvi

Thank you, cool idea, I got it to work.

 

But, when I try to write a for loop for it I get an "Unexpected end of input" message and I do not know why.

Any idea?

 

Thanks

 

 

b-Forceb-Force

Nice to heard that first part works for you.

 

Are you trying to implement same for multiple records ? (by using List Button )

 

If so please post the script code. we will check out  

 

Cheers,

Bala

 

KruviKruvi

I think my problem is in the for loop but I'm not sure

This is what I wrote:


{!REQUIRESCRIPT("/soap/ajax/10.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")} 
{!REQUIRESCRIPT("/js/functions.js")} 

sforce.debug.trace=true; 

var records = {!GETRECORDIDS( $ObjectType.Invoice__c )}; 

if(records[0] == null){ 
alert("Please select at least one record.") 

else{ 

try{ 

for(var i=0; i<records.length; i++) { 
var objInvoice = new sforce.SObject("Invoice__c"); 

objInvoice.Id = records[i].Id; 
objInvoice.Email_Invoice__c=true; 

var saveRes=sforce.connection.update([objInvoice]); 

if (saveRes[0].success=='false'){ 
alert(saveRes[0].errors.message); 


catch(error){ 
alert("Error while Sending Email:=="+error); 
}

b-Forceb-Force
{!REQUIRESCRIPT("/soap/ajax/10.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")} 
{!REQUIRESCRIPT("/js/functions.js")} 

sforce.debug.trace=true; 

var records = {!GETRECORDIDS( $ObjectType.Invoice__c )}; 

if(records[0] == null)
{ 
alert("Please select at least one record.") 
} 
else
{ 

try
{ 
for(var i=0; i<records.length; i++) 
{ 
var objInvoice = new sforce.SObject("Invoice__c"); 
objInvoice.Id = records[i]; 
objInvoice.Email_Invoice__c=true; 
var saveRes=sforce.connection.update([objInvoice]); 
} 
if (saveRes[0].success=='false')
{ 
alert(saveRes[0].errors.message); 
} 
} 
catch(error)
{ 
alert("Error while Sending Email:=="+error); 
}
}

 

try this one. It will work

Let me know if you need any more help

 

Cheers,

Bala

This was selected as the best answer
KruviKruvi

Many thanks

 

It works perfectlly

 

 

 

 

SayaSaya

Hi guys,

I know this is a very old post but since it has helped me a big deal, i would like to ask for help on this. I have implemented something close to this and it works fine.However, after clicking my Custom List button to send the email, an AJAX Toolkit Shell box pops up.How do i get to prevent it from popping up or hide it entirely?

Thanks in advance.

 

KruviKruvi

comment this line out like this:

 

//sforce.debug.trace=true;

 

 

SayaSaya

Hi Kruvi,

Thanks alot,that worked. One more last thing though,how do i make it show its processing or proof that its sending since there is no any form of processing taking place shown to the user after clicking the send button either by atleast loading the page again with the selected records unselected or atleast something to show the user that the sending has taken place.As well as having the sent mail shown in the Activity History of the Record.

 

Thanks in advance.

KruviKruvi

Well, I guess there are a few ways to do that.

 

You can save the result of the operation and based on that you can alert the user:

 

var saveRes=sforce.connection.update([objInvoice]); 

if (saveRes =='false')
{ 
    alert("problem with update"); 
}
else{
    alert("successful update"); 
} 

 

You can refresh the window by adding this to the JS:

 

window.location.reload(); 

 

DevNVDevNV

Nice solution sfdcfox, very clean and simple for one-off templated emails.

 

Niki

www.vankerksolutions.com