Skip to content

Instantly share code, notes, and snippets.

@hasanthaera
Created April 23, 2015 06:34
Show Gist options
  • Save hasanthaera/61f95172b6324ecf3aa4 to your computer and use it in GitHub Desktop.
Save hasanthaera/61f95172b6324ecf3aa4 to your computer and use it in GitHub Desktop.
Salesforce - How to call an apex class with java script
// read the parameter values and get the values
var jobId ='{!$CurrentPage.parameters.jobId}';
// pass the values and retrive the URL
var urls = sforce.apex.execute("globalClass","getURL",{jobId:jobId});
document.getElementById("banner").src = urls;
/**
* Author: Hasantha Liyanage
* Created: 2014-12-10
* Summary: This is to get the URL according to the id and which reads a custom setting to retrive some values
**/
global class JobOrderControllerExtension {
webservice static String getLisenseeURL(String strJobId) {
if (String.isEmpty(strJobId)) { return ''; }
// capture first 15 chars of id only
// ensure only valid id characters
if(validateId(strJobId)){ return ''; }
Account acct = [
SELECT
Name
FROM Account
WHERE(Id = : strJobId)
LIMIT 1
];
// get the url from custom setting mapped with account name
return getUrl(acct.Name);
}
/**
* get the url by reading the custom setting
* parameter: licensee name retrived from account
**/
private static String getUrl(String name) {
Map < String, Banners__c > mapBanners =Banners__c.getAll();
for (Banners__c banner: mapBanners.values()) {
if (!String.isEmpty(banner.Name) && !String.isEmpty(name) && banner.Name.equals(name)) {
return banner.URL__c;
}
}
return null;
}
static public boolean validateId(String Idparam) {
String id = String.escapeSingleQuotes(Idparam);
if(Pattern.matches('[a-zA-Z0-9]{15}', id)) {
return true;
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment