Salesforce number to Word conversion

Apex Class;


public with sharing class NumberToWord {
static String[] to_19 = new string[]{ 'zero', 'one',  'two', 'three', 'four',  'five',  'six','seven', 'eight', 'nine', 'ten',  'eleven', 'twelve', 'thirteen','fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen' };
static String[] tens = new string[]{ 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'};
static String[] denom = new string[]{ '','thousand',   'million',     'billion',    'trillion',    'quadrillion','quintillion', 's!xtillion',   'septillion',  'octillion',   'nonillion', 'decillion',  'undecillion',   'duodecillion', 'tredecillion',  'quattuordecillion',  's!xdecillion', 'septendecillion', 'octodecillion', 'novemdecillion', 'vigintillion' };
// convert a value < 100 to English. 
public static String convert_nn(integer val) {
if (val < 20)
return to_19[val];
if(val == 100)
return 'One Hundred';
for (integer v = 0; v < tens.size(); v++) {
String dcap = tens[v];
integer dval = 20 + 10 * v;
if (dval + 10 > val) {
  if (Math.Mod(val,10) != 0)
return dcap + ' ' + to_19[Math.Mod(val,10)];
return dcap;
}   
}
return 'Should never get here, less than 100 failure';
}
// convert a value < 1000 to english, special cased because it is the level that kicks 
// off the < 100 special case. The rest are more general. This also allows you to
// get strings in the form of "forty-five hundred" if called directly.
public static String convert_nnn(integer val) {
String word = '';
integer rem = val / 100;
integer mod = Math.mod(val,100);
if (rem > 0) {
word = to_19[rem] + ' hundred';
if (mod > 0) {
  word += ' ';
}
}
if (mod > 0) {
word += convert_nn(mod);
}
return word;
}
public static String english_number(long val) {
if (val < 100) {
return convert_nn(val.intValue());
}
if (val < 1000) {
return convert_nnn(val.intValue());
}
for (integer v = 0; v < denom.size(); v++) {
integer didx = v - 1;
integer dval = (integer)Math.pow(1000, v);
if (dval > val) {
integer mod = (integer)Math.pow(1000, didx);
integer l = (integer) val / mod;
integer r = (integer) val - (l * mod);
String ret = convert_nnn(l) + ' ' + denom[didx];
if (r > 0) {
ret += ', ' + english_number(r);
}
return ret;
}
}
return 'Should never get here, bottomed out in english_number';
}
  }

Trigger :

if(itr.SecurityDeposit__c>0){
                    Long n = itr.SecurityDeposit__c.longValue();
                    securityDeposit  = NumberToWord.english_number(n);
                }

show popup when custom button clicked on salesforce standard page

Javascript code :

{!REQUIRESCRIPT("https://code.jquery.com/jquery-1.10.2.js")} 
{!REQUIRESCRIPT("https://code.jquery.com/ui/1.10.4/jquery-ui.js")} 
{!REQUIRESCRIPT("/resource/mainjs")} 

j$("#modalDiv").dialog('option','title','{!Lead.Name}').dialog('open');
\

/*@description: This script will insert a modal div in the standard page.
                the modal will contain a VF page. This file should be located in static resource.*/
var j$ = jQuery.noConflict();
var currentUrl = window.location.href;
var hostIndex = currentUrl.indexOf(window.location.host+'/')+(window.location.host+'/').length;
var accountId = currentUrl.substring(hostIndex,hostIndex+15); 
j$(function(){
    /*Insert the jQuery style sheets in the Head.*/
    /*Insert the Modal dialog along with the VF as an iframe inside the div.*/
    j$("head").after(
        j$("<link>",{rel:"stylesheet",
                    href:"https://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css"}));
    j$("body").after(
        j$("<div>",{id:"modalDiv",
                    style:"display:none;"
           }).append(
            j$("<iframe>",{id:"vfFrame",
                         src:"/apex/popup4page?id="+accountId,
                         height:200,
                         width:550,
                         frameBorder:0})
           ));
    /*Initialize the Dialog window.*/
    j$("#modalDiv").dialog({
        autoOpen: false,
        height: 210,
        width: 605,
        modal:true
    });
});

visualforce email template with attachment

Create the Email template

<messaging:emailTemplate subject="offer Process" recipientType="Offer_Process__c"
 relatedToType="Offer_Process__c" replyTo="noreply@difc.com">
    <messaging:attachment renderAs="PDF" filename="LeadOfferUnitPDFTemplate.pdf">
        <c:offerProcessAttachment offerId="{!relatedTo.Id}"/>
    </messaging:attachment>
    <messaging:htmlEmailBody >
    <html>
        Please find your invoice attached.
    </html>
    </messaging:plainTextEmailBody>
</messaging:emailTemplate>

Create the component

<apex:component controller="offerUnitAttachmentController" access="global">
    <apex:attribute name="offerId" description="Contact Id" assignTo="{!leadObjectId}" type="Id" />
    <apex:outputText value="{!PageContents}" escape="false" />
</apex:component>

Controller

global class offerUnitAttachmentController {

  global String PageContents{ get; set; }
  global String leadObjectId{
    get;
    set {
        UpdateContents(value);
    }
  }

  public void UpdateContents(String contactObjectID) {
    try {
        PageReference pageRef = Page.LeadOfferUnitPDFTemplate;
        pageRef.getParameters().put('id',leadObjectId);
        PageContents = pageRef.getContent().toString().replace('<html style="display:none !important;">', '<html>');
    } catch(exception ex) {
        PageContents = 'An error has occurred while trying to generate this invoice.  Please contact customer service.' +
                       '\n\nError Message:' + ex.getMessage();
    }
  }
}

PDF page Template  LeadOfferUnitPDFTemplate

<apex:page controller="ReceiptController" applyHtmlTag="false" applyBodyTag="false" showHeader="false" sidebar="false">
<html>
<span>Dynamic PDF data:  <apex:outputText value="{!ContactName}" escape="false"/></span>
</html>

</apex:page>

Progress bar for custom button

Progress bar for custom button on click salesforce

{!REQUIRESCRIPT("/soap/ajax/36.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/36.0/apex.js")} 
var CaseId = '{!Case.Id}'; 
var sta= '{!Case.status}'; 
if(sta){ 
var result=sforce.apex.execute("ApexClassName","Method",{CaseId:CaseId}); 

var overlay = document.createElement('overlay'); 
overlay.style.background = '#000'; 
overlay.style.position = 'absolute'; 
overlay.style.top='0px'; 
overlay.style.bottom = '0px'; 
overlay.style.right = '0px'; 
overlay.style.left = '0px'; 
overlay.style.zIndex = '1000'; 
overlay.style.opacity='0.7'; 

var loading = document.createElement('img'); 
loading.src = 'https://c.na5050.content.force.com/servlet/servlet.ImageServer?id=014446000001yJpw&oid=00De2323340uNx0'; 
loading.style.position = 'absolute'; 
loading.style.top='50%'; 
loading.style.left = '50%'; 
loading.style.zIndex = '1000'; 

overlay.appendChild(loading); 
document.body.appendChild(overlay); 

if(result!=null && result!='success'){ 

document.body.removeChild(overlay); 
var outerdiv = document.createElement('div'); 
outerdiv.innerHTML = result; 
outerdiv.style.display = 'inline-block'; 
outerdiv.style.fontSize ='14px'; 
outerdiv.style.color = 'red'; 
//outerdiv.style.border = '1px solid grey'; 
outerdiv.style.padding = '10px'; 


var mainDiv = document.createElement('div'); 
mainDiv.style.marginLeft = '30%'; 
mainDiv.style.marginTop = '10px'; 
mainDiv.appendChild(outerdiv); 

document.getElementById('errorDiv_ep').innerHTML = result; 
document.getElementById('errorDiv_ep').style.display=''; 
setTimeout(function(){ 
document.getElementById('errorDiv_ep').innerHTML = ''; 
document.getElementById('errorDiv_ep').style.display='none'; 
},5000); 


}else{ 
document.location.reload(true); 

}else{ 
alert('You can not revert back the case to Queue.'); 
document.getElementById('errorDiv_ep').innerHTML = 'You can not close the case.'; 
document.getElementById('errorDiv_ep').style.display=''; 
setTimeout(function(){ 
document.getElementById('errorDiv_ep').innerHTML = ''; 
document.getElementById('errorDiv_ep').style.display='none'; 
},5000); 
}