Salesforce Custom button with Javascript & jQuery model dialogue window for sending email to customers

jQuery Code Snippet :

{!REQUIRESCRIPT('//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js')}
{!REQUIRESCRIPT('//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js')}
{!REQUIRESCRIPT('/soap/ajax/15.0/connection.js')}
{!REQUIRESCRIPT('/soap/ajax/15.0/apex.js')}
var emailFinalText;
var descriptionFinalText;
try{
  jQuery(function(){
    /*Append the jQuery CSS CDN Link to the Head tag.*/
    jQuery('head').append('<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/themes/start/jquery-ui.css" type="text/css" />');  
    /*Create the HTML(DIV Tag) for the Dialog.*/
    var html = '<form id="dialog" title="Send Email">'
+'<table style="width:100%">'
+'<tr><th>Email</th></tr><tr><td><input style="width: -webkit-fill-available !important;" id="email" type="email" name="email"></td></tr>'
+'<tr><th>Message</th></tr><tr><td><textarea style="width: -webkit-fill-available;" id="messageDescription" name="comment" rows="13" cols="45"></textarea></td></tr>'
+'</table>'
+'</form>';  
    /*Check if the Dialog(DIV Tag) already exists if not then Append the same to the Body tag.*/
    if(!jQuery('[id=dialog]').size()){
      jQuery('body').append(html);
    }
    /*Open the jQuery Dialog.*/
    jQuery( "#dialog" ).dialog({
   bgiframe: true,
autoOpen: true,
resizable: false,
maxWidth:500,
maxHeight:400,
width: 500,
height: 400,
modal: true,
      show:{
        duration: 1000
      },
      hide:{
        duration: 1000
      },
      /*Adding value to display*/
      open: function( event, ui ){
      var emailId = "{!Lead.Email}";
      var leadDesc ="{!Lead.FirstName}";
      emailFinalText = document.getElementById("email").value = emailId;
      descriptionFinalText = document.getElementById("messageDescription").value = "Hello "+leadDesc+"\n\nPlease confirm if you are interested in buying the product ?\n\nRegards,\n{!$User.FirstName}";
      },
      buttons:{
        "Send": function(){        
           /*alert("call Apex method.");*/
                var contextUser = sforce.apex.execute("sendEmailToUser_Cls", "sendEmail", {emailFinalTextApex:$('#email').val(),descriptionFinalTextApex:$('#messageDescription').val()});
                alert(contextUser);
                jQuery( this ).dialog( "close" );
        },
        Cancel: function(){
          jQuery( this ).dialog( "close" );
        }
      }
    });
  });
}catch(e){
    alert('An Error has Occured. Error: ' + e);
}

Apex Soap code snippet:

global class sendEmailToUser_Cls{
   webService static String sendEmail(String emailFinalTextApex,string descriptionFinalTextApex) {
       List<Messaging.SingleEmailMessage> mails =  new List<Messaging.SingleEmailMessage>();
       if(String.isNotBlank(emailFinalTextApex) && String.isNotBlank(descriptionFinalTextApex)){
           Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
           List<String> sendTo = new List<String>();
           sendTo.add(emailFinalTextApex);
           mail.setToAddresses(sendTo);
           mail.setSenderDisplayName('Official Bank of Nigeria');
           mail.setReplyTo('fgangana@cisco.com');
           mail.setHtmlBody(descriptionFinalTextApex);
           mails.add(mail);
           Messaging.sendEmail(mails);
           return 'Success: Mail has been sent';
       }else{
           return 'Error:Please fillout Email and Description fields';
       }
   }
}

Sample Screenshot:




No comments:

Featured

What is Cryptography in salesforce and what are all the algorithms provided by them ?

A). It is a security protocal between two systems. Lets say we are integration two systems without any encrytion mechanism then hackers wil...

Popular