Overlay Effect for VF form submit

Tuesday, December 13, 2011 by Aslam - The Alexendra
Hi All,
Overlay box is many times used whenever there is a need to show user a processing box and we dont need user do any activity on page while processing is going on. So i am sharing a simple vf component here which has css and js which gives overlay effect in your normal VF pages like below:

<c:overlay style="processing">

Here "style" attribute can have two values for now [classic, processing]. Classic style shows gray overlay, and Processing style shows animated image.



Here is the steps how you can use this component to achieve overlay effect:

1) put the component in head section of your page
<c:overlay style="classic">

2) put this div at the very bottom of your page, just before close of </body> tag or </apex:page> tag
<div id="overlay"></div>

3) put this <apex:actionstatus in your form
<apex:actionstatus id="overlayStatus" onstart="showOverlay();" onstop="hideOverlay();"></apex:actionstatus>

4) finally refer this action status in your action function or command button as below
<apex:commandbutton action="{!saveRecord}" status="overlayStatus" value="Save" rerender="pBlock">

Here is the complete code :


<apex:page controller="OverlayDemoController">
<head>
<c:Overlay style="processing" />
</head>
<apex:form>
<apex:actionStatus id="overlayStatus" onStart="showOverlay();" onstop="hideOverlay();"></apex:actionstatus>
<apex:sectionHeader title="Contact Save" subtitle="Overlay Demo" />
<apex:pageBlock id="pBlock">
<apex:pageMessages></apex:pageMessages>
<apex:pageBlockButtons>
<apex:commandButton action="{!saveRecord}" status="overlayStatus" value="Save" reRender="pBlock"/>
</apex:pageBlockButtons>
<apex:pageBlockSection>
<apex:inputField value="{!cnt.First_Name__c}" />
<apex:inputField value="{!cnt.Last_Name__c}" />
<apex:inputField value="{!cnt.Phone__c}" />
<apex:inputField value="{!cnt.Email__c}" />
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
<div id="overlay"></div>
</apex:page>


http://www.blogger.com/img/blank.gif
5) Here are the links for working online demo for the overlay :

http://labsprojects-developer-edition.ap1.force.com/OverlayDemo

http://labsprojects-developer-edition.ap1.force.com/OverlayDemoProcessing

6) You can download the complete code from here:
http://labs.aslambari.com/overlay.zip

Hope it will be useful for developers.

Thanks
Aslam Bari

Thursday, October 6, 2011 by Aslam - The Alexendra
Hi All,
Today I came to know about 'Stripe' APIs. Stripe is a simple, developer-friendly way to accept payments online. According to Stripe :-
"We believe that enabling transactions on the web is a problem rooted in code, not finance, and we want to help put more websites in business.
Complexity and opacity have traditionally been hallmarks of online payment processing. We want to fix that."


So, as usual, here I came up with the solution of Apex Wrapper for Stripe APIs. I developed this API library with my colleague Sanjay. The library gives basic api methods to call and use as follows:

//1) CreateCharge
Stripe request = new Stripe('y001LvDyqiGbNZclaaaxxxxttttOJk8w');
StripeResponseModel response
= request.createCharge('4242424242424242', '2013', '10', '123', '2000', 'usd', 'testing');
if(!response.isError){
system.debug(
'Transaction ID :: ' + response.id);
system.debug(
'Transaction Fee :: ' + response.fee);
}
else{
system.debug(
'Error Message :: ' + response.errorResponse.message);
}

//Create charge using customer
Stripe request = new Stripe('y001LvDyqiGbNZclaaaxxxxttttOJk8w');
StripeResponseModel response
= request.createCharge('cus_EPTWuvf7EXDb4g', '2000', 'usd', 'testing');
if(!response.isError){
system.debug(
'Transaction ID :: ' + response.id);
system.debug(
'Transaction Fee :: ' + response.fee);
}
else{
system.debug(
'Error Message :: ' + response.errorResponse.message);
}

//2) CreateCustomer
Stripe request = new Stripe('y001LvDyqiGbNZclaaaxxxxttttOJk8w');
StripeResponseModel response
= request.createCustomer('4242424242424242', '10','2013', '123', 'Aslam', '', '', '', '', 'IN', 'aslam.bari@gmail.com', 'test');
if(!response.isError){
system.debug(
'Customer ID :: ' + response.id);
}
else{
system.debug(
'Error Message :: ' + response.errorResponse.message);
}

//3) RetrieveCustomer
Stripe request = new Stripe('y001LvDyqiGbNZclaaaxxxxttttOJk8w');
StripeResponseModel response
= request.retrieveCustomer('cus_EPTWuvf7EXDb4g');
if(!response.isError){
system.debug(
'Customer Country :: ' + response.card.country);
system.debug(
'Customer Last4 CC :: ' + response.card.last4);
}
else{
system.debug(
'Error Message :: ' + response.errorResponse.message);
}

//4) CreateInvoice
Stripe request = new Stripe('y001LvDyqiGbNZclaaaxxxxttttOJk8w');
StripeResponseModel response
= request.createInvoice('cus_EPTWuvf7EXDb4g', '2000', 'usd', 'test');
if(!response.isError){
system.debug(
'Invoice Id :: ' + response.id);
}
else{
system.debug(
'Error Message :: ' + response.errorResponse.message);
}

//5) RetrieveInvoice
Stripe request = new Stripe('y001LvDyqiGbNZclaaaxxxxttttOJk8w');
StripeResponseModel response
= request.retreiveInvoice('ii_W19Xp6cxrqVABH');
if(!response.isError){
system.debug(
'Invoice Date :: ' + response.created);
}
else{
system.debug(
'Error Message :: ' + response.errorResponse.message);
}

//6) CreatePlan
Stripe request = new Stripe('y001LvDyqiGbNZclaaaxxxxttttOJk8w');
StripeResponseModel response
= request.createPlan('AMX_101', '2000', 'usd', 'month' , 'AMX Testing');
if(!response.isError){
system.debug(
'Plan Id :: ' + response.id);
}
else{
system.debug(
'Error Message :: ' + response.errorResponse.message);
}

//7) RetreivePlan
Stripe request = new Stripe('y001LvDyqiGbNZclaaaxxxxttttOJk8w');
StripeResponseModel response
= request.retrievePlan('AMX_101');
if(!response.isError){
system.debug(
'Plan Label :: ' + response.name);
}
else{
system.debug(
'Error Message :: ' + response.errorResponse.message);
}


You can download the code from github here:
https://github.com/aslambari/Stripe-Apex-Toolkit

Or from my website here:
http://www.aslambari.com/stripe_apex_toolkit.html

There are many more methods which are under development like CreateSubscription, ListAllCustomers etc, we are working on them. And will back soon with full set of methods. Till, try this library and let us know if that works fine for you and revert back us for enhancements.

Thanks
Aslam Bari

Showing Salesforce Images/Attachments on PHP/JSP page

Saturday, October 1, 2011 by Aslam - The Alexendra
Hi All,
Today I am going to demo you how to fetch images (attachments) from salesforce instance in to your PHP script and how you can show those imags on page as a portal or catalog way.

1) index.php script
The below code shows how you can query to attachment object in salesforce to fetch some attachment records. For example i have query the pics (attachment) for my two SFDC contact records in sample code. The concept is simple. You need to query the Id of attachment in your main page, then you will have one another php script which will fetch the SFDC attachment (image) binary data and show the image on screen as embedded. We can wrap/embed that image in <img /> tag directly.

<?php
error_reporting(E_ALL);
require_once ('soapclient/SforcePartnerClient.php');
require_once('connection.php');

$ids = array();
$ids[] = '00390000006IUVK';
$ids[] = '00390000001lwbd';
getPhotos(
$ids);


function getPhotos($contactids){
$mySforceConnection = getConnection();
$query = "SELECT Id, Name from Attachment Where ParentId in (";
foreach($contactids as $id){
$query .= "'". $id . "',";
}
$query = substr($query , 0, strlen($query) - 1);
$query .= ")";
$queryResult = $mySforceConnection->query($query);

$records = $queryResult->records;
if(count($records) > 0){
echo "<table cellspacing='5' cellpadding='5'><tr>";
foreach($records as $rec){
echo "<td style='text-align:center;font-size:12px;'>".
"<img src='image.php?id=".$rec->Id."' ".
"style='width:200px;height:200px;border:1px solid #ccc;padding:10px'/> <br/>"
. $rec->fields->Name.
"</td>";
}
echo "</table>";
}
return NULL;
}
?>


2) image.php script
This code simply accepts one Id of the attachment record and fetch the binary body of attachment, then it shows that image on the screen as image content. To view this image in other page you simply need to wrap/embed its call into <img /> html tag. Like below:

<img src="image.php?id=00P9000000185ioEAA" />


<?php
error_reporting(E_ALL);
header('content-type: image/jpeg');
require_once ('soapclient/SforcePartnerClient.php');
require_once('connection.php');

getPhotos(
$_GET['id']);


function getPhotos($id){
$mySforceConnection = getConnection();
$query = "SELECT Id, Name, Body from Attachment Where Id ='" .$id ."'";
$queryResult = $mySforceConnection->query($query);

$records = $queryResult->records;

print_r(base64_decode($records[0]->fields->Body));
return NULL;
}

?>



Here is the demo of the scripts where i am fetching two contact's images from Salesforce and showing on simple PHP page.

http://labs.aslambari.com/sfdc_photos_demo/


Thanks
Aslam Bari

SF Uploadify: File Uploading Utility for Salesforce

Tuesday, September 13, 2011 by Aslam - The Alexendra
Hi All,
I have created a very cool file uploading utility which can be integrated with any system, but mainly I focused on Salesforce. This utility is built using Uploadify Tool and PHP integration. You can find more info about Uploadify here. It supports multiple file uploading with progress bar and uploads the file to your external server (php server where your site or this utility is hosted.) Then it makes the Attachment record in salesforce and links to actual file on server. So you can directly (natively) access your files.


How it works:

  1. User opens any record in salesforce , lets say "Account" record.
  2. There will be a custom button (given in below code), user clicks on button
  3. One file uploading screen opens, click on 'Select Files" button and select multiple files at same time (using control button)
  4. As soon as, user select and close dialog, it starts uploading files with progress bar
  5. The files will be stored on the Server where this utility exists, for now it saves to my server.
  6. It will show finally a screen with close button.
  7. Click on close button , it will refresh the parent window (if not, manually refresh the parent record)
  8. Open the attachments section, you will find the new files (attachments created, which actually a link for actual files on the server)
  9. Click on any file and click on "View" link.
  10. And you will see it opens your uploaded actual file :)

More Details and Updates on my site:
http://www.aslambari.com/sf_uploadify.html

Installation:
1) Button code:
Make one custom button (detail screen button), on any object, for example , account object, see below code.
a. Behavior must be "Open in new window"
b. Content Source will be "URL"
The url for the button will be as below

http://labs.aslambari.com/sf_file_uploader/sf_setup_uploader.php?sessionid={!$Api.Session_ID}&serverurl={!$Api.Partner_Server_URL_220}&recordid={!Account.Id}

2) Utility Code:
The utility is on my server for now, it is in BETA, once all is tested and working , i will make it public :)

Note: For commercial use this tool, you first need to go to Uploadify site and check its license type if any. Because my tool uses the Uploadify internally.

See the screen cast how it works:
http://screencast.com/t/ibDqgMBbfQ

Hope you will like it. :)

Thanks
Aslam Bari

Heat Map using Google Map in Salesforce

Saturday, September 10, 2011 by Aslam - The Alexendra
Hi All,
Recently i came across for one requirement with my client where he wants to see density of his shops across US. For example he has a lot of branches of his coffee shops across US like in CA, TX, MB etc. He want to see a good way on map to see density of his branches. I found the way how to show this, this type of maps are called HeatMaps.

Heat Maps are used to show density of your prouduct, services and any useful information on Geaographical area, based on your addresses.




The another example may be like, i want to know Starbucks shops across the US and want to know which area has density (number of shops) more.
So, i created one example in Salesforce VF page to show, how we can make it.

1. Copy paste below code in your VF page

<apex:page showHeader="false" sidebar="false">
<script src="http://www.heatmapapi.com/javascript/HeatmapAPI.aspx?k=86123b76-2190-4a4b-b182-86e7199b0406" type="text/javascript"></script>

<script src="http://maps.google.com/maps?file=api&v=2&key=ABQIAAAA09SA9e7gW5A4Mpy3VCJQjxQfiV3Wc4AiuMkI_RwI0dk2dVyy7hQWusr2PE1VCubWNkTco2njprfSxg" type="text/javascript"></script>

<script src="http://www.heatmapapi.com/javascript/HeatmapAPIGoogle.js" type="text/javascript"></script>
<script type="text/javascript"
src
="http://maps.google.com/maps/api/js?sensor=false">
</script>
<script type="text/javascript">
var m = null;
var batch = "";
var myHM = null;
var request = GXmlHttp.create();
var TOTAL_COUNT = 0;
var INDEX = 0;
function loadGoogle()
{
if (GBrowserIsCompatible())
{
m
= new GMap2(document.getElementById("map"));
m.setCenter(
new GLatLng(38.5, -95.8), 3);
m.setUIToDefault();
initHeatmap();
}
else
alert(
'Your Internet browser is not compatible with this website.');
}



function initHeatmap()
{
// Heatmap Scripts
try
{
myHM
= new GEOHeatmap();
myHM.Init(
400, 400); // Must be same as map size, or same ratio
addMarkersFromXml();





}
catch(e)
{
alert(e);
}
}

function addMarkersFromXml(){

request.open(
'GET', '/apex/data', true);
request.onreadystatechange
= function() {
//alert(request.readyState);
if(request.readyState == 4) {
var xmlDoc = request.responseXML;
var markers = xmlDoc.documentElement.getElementsByTagName("store");
TOTAL_COUNT
= markers.length;
INDEX
= 0;
for(var i=0; i < markers.length; i++){
var state = markers[i].getAttribute("state");
var density = "1";//markers[i].getAttribute("density");
geocoder = new GClientGeocoder();
var address = state;
geocoder.getLatLng(address,
function(point) {
if (!point) {

}
else {
fullVal
= point.lat().toFixed(5) + "," + point.lng().toFixed(5) + "," + density + ",";
batch
+= fullVal;
INDEX
++;
if(INDEX >= TOTAL_COUNT){
startMap();
}
}
});
}

}
}


function startMap(){
document.getElementById(
"HMMapdata").value=batch;
myHM.SetData(batch);
myHM.SetBoost(.
5); // Optional, see documentation
myHM.SetDecay(0); // Optional, see documentation
//var proxyURL = "http://localhost/proxy.php";
//myHM.SetProxyURL(proxyURL);
var preUrl = myHM.GetURL();

// Now render in our Google Map
var heatmapOverlay = new HMGoogleOverlay(preUrl);
m.addOverlay(heatmapOverlay);
}
request.send(
null);

}





</script>

<apex:sectionHeader subtitle="Heat Map using Google Map" title="Aslam Bari (http://www.aslambari.com)"/>
<body onload="loadGoogle();">
<input type="hidden" id="HMMapdata" />
<input type="hidden" id="HMImageURL" name="HMImageURL"/>
<div id="map" style="width:400px;height:400px;"></div>
</body>
</apex:page>



2. Create another page called "data" and copy paste below code in that. Note: this page contains the addresses of your shops, offices etc which we want to see as density. So, change accordingly

<apex:page sidebar="false" showHeader="false" contentType="text/xml">
<stores>
<store state="IN,US"></store>
<store state="KY,US"></store>
<store state="WA,US"></store>
<store state="MB,US"></store>
</stores>
</apex:page>


Note: you need to go http://www.heatmapapi.com to get new Free API key and also for google free api key to get your code work.

Here is the working example of this. Click to see.
http://labsprojects-developer-edition.ap1.force.com/HeatMapExample

Hope you will need this sometime in your project.

Thanks
Aslam Bari

Make Firefox wise to remember Salesforce Credentials

Saturday, September 3, 2011 by Aslam - The Alexendra
Hi All,
I am back after a long break :). Today i want to share a good trick which help me to save my time in many ways. Thanks to my friend (Abdul Vahid) who given me hint for that.

Problem:
Most of you (developers) are aware that these days salesforce has disabled the remember password functionality. It does not save or prompt to save any password when you login to the org. It is very hectic to maintain some external doc or sheet for passowrd , for a developer who work and develop in many orgs. For example i work in 20 orgs (sandbox, production) some times. I maintain a separate doc to look and find password whenever i need to work in a org. That takes time and sometimes not easy.

Solution:
So here is the solution trick :)
  • Find out where you installed your firefox and go to Components folder. My location is like this "D:\Mozilla Firefox\components".
  • Find the "nsLoginManager.js" file and open this in any editor. Before doing this you need to close all instances of firefox.
  • Find out following line of code and comment it. I found it near line #805.

Before comment
_isAutocompleteDisabled : function (element) {
if (element && element.hasAttribute("autocomplete") &&
element.getAttribute(
"autocomplete").toLowerCase() == "off")
return true;

return false;
},




After comment
_isAutocompleteDisabled : function (element) {
//if (element && element.hasAttribute("autocomplete") &&
// element.getAttribute("autocomplete").toLowerCase() == "off")
//return true;

return false;
},



Save the file and start your firefox again.

(Note: The above steps given for Firefox 3.6 version. In other version it may differ)

Now see, whenever you try to login with username/pwd. Firefox will ask (prompt) you about "remember" password feature. Once you click on "Remember". It will save your username/password for your org. Next time when ever you try to login using your username, it will auto fill password like old days :)

Now, i do not need to maintain any external password doc. Isn't it cool :)


Thanks
Aslam Bari

Apex Toolkit for Salesforce

Saturday, July 16, 2011 by Aslam - The Alexendra
Hi All,
Its a long time since I last posted :). So i am back with something interesting and useful contents again.
Most of us known that whenever we want to connect with any salesforce org and do query the data with any langugae, salesforce gives us some toolkit like PHP toolkit, Java toolkit, Ruby Toolkit, Flex Toolkit etc. But what when we want to connect with a remote Salesforce instance within salesforce only, using native Apex language. People use many alternate to achieve it, some uses partner wsdl to generate apex or some uses javascript toolkit. But I find them not easy to use and not easy to setup and they did not fit in my need.
So I come up with my own Apex Toolkit for Salesforce :)




Its easy to install package. You can install it from here:
https://login.salesforce.com/packaging/installPackage.apexp?p0=04t90000000Priq

For successful connectivity to any remote instance one entry for that salesforce server TYPE must be there in your remote site setting. I have by default included in package (NA1 to NA7) server url for salesforce. You can add more like (CS1 - CS9, NA1 - NA11, AP1 - AP10). For example see the below screen shot how i added most likely used server instances in my remote site settings. There must be 20-25 possible entries at most in my opinion.



It is very easy to use with only simple some lines of code to login and do a query to remote instance as below:

SFConnectionController controller = new SFConnectionController();
LoginResult loginResult
= controller.login(username, password, securityToken);
Response response
= controller.query('select Name, Email, Phone, MailingCity, MailingCountry from Contact', loginResult);
for(Record row: response.data.records){
string Name
= row.getFieldValue('Name');
string Email
= row.getFieldValue('Email');
string Phone
= row.getFieldValue('Phone');
string MailingCity
= row.getFieldValue('MailingCity');
string MailingCountry
= row.getFieldValue('MailingCountry');
}


There is one demo page and controller in package which shows how to use this functionality.
SFConnectionDemo
SFConnectionDemoController

The toolkit is just a start and gives users facility to do login and do simple queries on objects. One can easily extend this for better version. If you want to contribute in this, i have shared this project as open source here:

http://code.google.com/p/apex-toolkit-for-salesforce/

Here is one running online demo which shows how this package works. Just give your developer/production org's username, password (or securityToken as well), and click on search button. It will fetch the contact records from your organization and will display in table below.

http://labsprojects-developer-edition.ap1.force.com/SFConnectionDemo

Hope you will like it :)
Mail me for any configuration or setup issues and for feedback.

Thanks
Aslam Bari