3

I want to update some case fields and populate an email template via a custom JavaScript button. Thanks to your answers to questions in the stackexchange, I created buttons that do that but send out the email at the same time. My question: is it possible to first update everything, and then letting the agent confirm by clicking on send email (so instead of send email just update)? One of my current buttons:

{!REQUIRESCRIPT("/soap/ajax/28.0/connection.js")} 

(function() { 
var caseObj = new sforce.SObject("Case"); 
caseObj.Id = '{!Case.Id}'; 
caseObj.Status = 'Closed'; 
caseObj.Reason = 'Example Reason'; 
var result = sforce.connection.update([caseObj]); 
sforce.connection.sessionId = "{!$Api.Session_ID}"; 
var message = new sforce.SingleEmailMessage(); 
message.replyTo = "[email protected]";
message.senderDisplayName = "[email protected]";
message.targetObjectId = "{!Contact.Id}"; 
message.templateId = "template ID"; 
var result = sforce.connection.**sendEmail**([message]); 
if(result[0].success) { 
alert("You have sent an email regarding Example Reason"); 
} else { 
alert("Your email was not sent. Please contact XYZ"); 
} 
window.location.href=window.location.href; 
}());

Thanks in advance!!

1
  • You can add a confirm prompt to your JavaScript and do the email part depending on the outcome of that. Is that good enough?
    – Keith C
    Nov 25, 2014 at 17:52

1 Answer 1

3

If a window.confirm() prompt is sufficient, it could look like this:

{!REQUIRESCRIPT("/soap/ajax/28.0/connection.js")} 

(function() {

    // update the case
    var caseObj = new sforce.SObject("Case");
    caseObj.Id = '{!Case.Id}';
    caseObj.Status = 'Closed';
    caseObj.Reason = 'Example Reason';
    var result = sforce.connection.update([caseObj]);

    // default the email send to false
    var sendEmail = false;
    if (result[0].getBoolean('success')) {
        sendEmail = confirm('Case update successful. Do you want to send the email?');
    } else {
        alert('Case update unsuccessful.');
    }

    // send the email if the user instructed is to do so
    if (sendEmail) {

        sforce.connection.sessionId = "{!$Api.Session_ID}";
        var message = new sforce.SingleEmailMessage();
        message.replyTo = "[email protected]";
        message.senderDisplayName = "[email protected]";
        message.targetObjectId = "{!Contact.Id}";
        message.templateId = "template ID";
        var result = sforce.connection.* * sendEmail * * ([message]);
        if (result[0].success) {
            alert("You have sent an email regarding Example Reason");
        } else {
            alert("Your email was not sent. Please contact XYZ");
        }
        window.location.href = window.location.href;
    }
}());

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .