Useful Apex Functions Solution "SFApexUtilFunctions"

Thursday, March 31, 2011 by Aslam - The Alexendra
Hi All,
Most of time while devleoping Apex Code, we face many business requirements for which we develop our own logic and methods. Most of us written many useful methods so far in their development. So, my idea is simple. I welcome all of you to contribute your common useful apex methods with me and complete my "SFApexUtilFunctions" class. I have started this class with some of my reused methods. If we all contribute our useful methods then i am sure we will have a good library written which will be useful for all of us.

The format for contributing your methods will be like given as per below with the proper comments on method top describing method and author. You can post in blog itself or mail me. I will inform the newly added methods to all and update the actual class also. One can download the latest version of class from this url:

http://www.aslambari.com/SFApexUtilFunctions.cls




/**
@ Name SFApexUtilFunctions
@ Author Aslam Bari
@ Created Date 31 March, 2011
@ Description A utility methods class for helping developers community
*/
public class SFApexUtilFunctions{

/**
@ Name join
@ Author Aslam Bari
@ Created Date 31 March, 2011
@ Desctiption Join one list of String to a full string
@ Param List<String>: list of string data
@ Param separator: the string by which the list is joined
*/
public static string join(List<String> lstArg, String separator){
String fullString
= '';
for(String item: lstArg){
fullString
+= item + separator;
}
if(fullString.length() > 0)
fullString
= fullString.substring(0, fullString.length() - 1);

return fullString;
}

/**
@ Name getUrlContents
@ Author Aslam Bari
@ Created Date 31 March, 2011
@ Desctiption Fetch the contents of remote url
@ Param url: remote url with all parameters attached to url for GET call
*/
public static string getUrlContents(String url){
HttpRequest request
= new HttpRequest();
request.setEndPoint(url);
request.setMethod(
'GET');
Http call
= new Http();
HttpResponse response
= call.send(request);
return response.getBody();
}
}



So start now and lets contribute :)

Thanks
Aslam Bari

Simple SF Tree for VF Page

Friday, March 25, 2011 by Aslam - The Alexendra
Hi All,
Many times people need one solution for using Tree structure on thier VF page. There are many solution using JS/JQuery. Here i used jquery treeview plugin and created one apex component which can be used directly in you VF page. You simply need to use following syntax to embed this tree in your VF page:

<c:sftree />





Here is a working demo for this SF Tree.
http://labsprojects-developer-edition.ap1.force.com/sftreedemo

The component provides two JS method on click of parent and child node and send id of the node. You can define those methods in our VF page and do process whatever you need.


Mail me if you face any issue. Hope its helpful for some people.

Thanks
Aslam Bari

Using Map In Your VF Pages

Wednesday, March 16, 2011 by Aslam - The Alexendra
Hi All,
Most of you aware of this but i am sure most of you (like me) un aware about that we can use Map directly on VF pages. Atleast for me, it is a news :) . Anyway, i am going to show you how to use simple map on your VF page so that you can avoid Model class (If you making only because of thinking that VF does not support Map). Here I am going to show a simple program which counts duplicate accounts by name and shows on VF screen. Here Account Name is "key" of Map and the "value" part is count of duplicate account.





Class Code:
public class TestMapController{

public map<string,integer> data {get;set;}

public TestMapController(){
data
= new map<string,integer>();
for(Account acc: [Select Id, Name, (Select Id, Name, Email from Contacts), Phone from Account]){
integer count
= data.get(acc.name);
if(count != null)
count
++;
else
count
= 1;
data.put(acc.name, count);
}
}
}




VF Page Code:
<apex:page controller="TestMapController">
<apex:pageblock title="Map Usage On VF">
<apex:pageBlockTable value="{!data}" var="d">
<apex:column headerValue="Account Name">
{!d}
</apex:column>
<apex:column headerValue="Duplicate Count">
{!data[d]}
</apex:column>
</apex:pageBlockTable>
</apex:pageblock>
</apex:page>



Hope this will help new developers who struggle to manage and show their data model on pages.

Thanks
Aslam Bari

Rounded corner box using easy JS

Sunday, March 6, 2011 by Aslam - The Alexendra
Hi All,
I think most of us many times faced the situation when they need to make some rounded corner boxes (div) on their web page. Generally most of us go for two ways:
1) Use of -moz-border-radius , but this works in Mozilla browsers only not in IE
2) If one want to do that for support all browsers then he needs to achieve that using combination of 3-4 divs , setting some border images which already rounded. It means extra divs in your existing logic. So far i also follow that.

But recently i found a useful library which make any div available in your webpage to rounded corner with single line of code. So you just need to focus on your web design and just need to call one method to make rounded corner for whichever div you need. The library is DD_roundies. This gives same output in all browsers so you no need to take any extra actions. Here is the sample code and working demo i used.

The single line of code which you need to call is:
DD_roundies.addRule('#yet_another', '10px', true);

Here "#yet_another" is the id for the div which you need to make round, '10px' is the radius for border for roundness, and 'true' is a flag for support all browser.

Complete Code:
<style>
#yet_another
{
border
:1px solid #000;
background-color
:#ccc;
padding
:5px;
height
:100px;
width
:400px;
text-align
:center;
}
#up_another
{
border
:1px solid #000;
background-color
:#ccc;
padding
:5px;
height
:100px;
width
:400px;;
text-align
:center;
}

#capsule_round
{
width
:100px; height:100px; background:#FA0;
padding
:10px;
text-align
:center;
padding-top
:100px;
}
#totally_round
{
width
:100px; height:100px; background:#FA0;
padding
:10px;
text-align
:center;

}
</style>
<script src="DD_roundies_0.0.2a-min.js"></script>
<script>
/* EXAMPLES */
/* varying radii, "all" browsers */
DD_roundies.addRule('#yet_another', '10px', true);
DD_roundies.addRule('#up_another', '10px 10px 0px 0px', true);
DD_roundies.addRule('#capsule_round', '10000px', true);
DD_roundies.addRule('#totally_round', '10000px', true);
</script>

<body>

<div id="yet_another">
Rounding box with 10px radius
</div>
<br/>
<br/>
<div id="up_another">
Rounding box with upper round 10px radius
</div>
<br/>
<br/>
<div id="capsule_round">
Capsule Round
</div>
<br/>
<br/>
<div id="totally_round">
Total Round
</div>

Here is the link for working demo. You can check it in Firefox as well as in IE.
https://labsprojects-developer-edition.ap1.force.com/RoundCorners


Hope it will be useful for somebody who searching for that support :)

Thanks
Aslam Bari

Salesforce, Talend (tMap), Data Loading...Avoid VLookup

Saturday, February 26, 2011 by Aslam - The Alexendra
Hi All,
Today I will give you some demonstration about data loading with Talend. I am taking one special case in which generally people use VLookup function from excel to match the lookup ids and fill the records csv column and then upload. For example, you have one CSV file for Contacts which you want to insert into salesforce, that CSV contains 'Account Name'. But you need AccountId to successfully insert contact records. For solving this people generally do this approach.

1) Fetch all account names and ids from salesforce and make one csv
2) Use the VLookup function to match Account Name from csv2 to CSV1 and set the extra column in CSV1 for AccountId (generally people struggle here :) )
3) Then use final CSV1 to upload the data

So, I am here to tell you easy upload approach using Talend tMAP to avoid any VLookup headache and simply upload data successfully.




Follow this approach.
1) Create a new Job
2) Select File->tFileInputDelimited and set your CSV input file here
3) Select Business->Salesforce->tSalesforceOutputBulkExec, set your salesforce credentials where you want to upload the data
4) Select Business->Salesforce->tSalesforceInput, set your salesforce credentials from where you want to fetch the Account Ids and Account Names data, Set your query here
5) Select Processing->tMap, this is the heart of whole process
6) Double click on tMap, set the mapping here, CSV1->AccountName TO tSalesforceInput->Name. Drag relavent columns to your salesforce output file, Set the ID from tSalesforceInput to tSalesforceOutputBulkExec->AccountId
7) Now save your complete Job
8) Run the job and it will give you output as results.

Here is the Screencast to see complete process as vedio.
http://screencast.com/t/J8tUcer963u4

Here are more links which can help you to run and use Talend
http://techblog.appirio.com/2009/08/using-talend-to-export-data-from.html
http://techblog.appirio.com/search/label/Talend
http://techblog.appirio.com/2011/01/loading-data-to-salesforcecom-with-bulk.html
http://techblog.appirio.com/2009/12/performing-lookups-and-transformations.html

Hopefully this will help some people to try Talend and finding its benifits.

Thanks
Aslam Bari

Barcode, Salesforce and PDF

Wednesday, February 16, 2011 by Aslam - The Alexendra
Hi All,
Recently I faced one issue where i need to generate Barcode on the fly in VF and also render that as PDF. I searched on Net and community, found some people have done Barcode generation using Javascript. But in my case i need to render it as PDF and VF does not support Javascript when renderAs PDF. So i decided to go the way to generate Barcode image. The first thing came in my mind is PHP/GD way to generate Barcode image and use that url in VF to show image. Then i found this site useful and very useful PHP script which fits my need
http://www.sid6581.net/cs/php-scripts/barcode/

I used this script, done some modification in this and hosted on my site And used in my VF page as below:-

<apex:image value="http://www.aslambari.com/barcode.php?barcode=CS1001&width=200&text=1&height=100" />


Here is some details about how you use the url and parameters to generate barcode:
URL: http://www.aslambari.com/barcode.php
Params:
barcode: [any text to render as barcode]
text: [0 or 1]
format: [jpeg/png/gif]
width: [the width you want]
height: [the height you want]



It works great if my VF render as normal HTML page, but as soon as i make it renderAs="PDF", VF again not works, it does not allow dynamic generated images. So again i got disappointed.
Then i thought again and did apex code to fetch the dynamic binary data generated my PHP site url and save it into attachment and use that url in my VF page. I was thinking this time i will win for sure, but again it did not work. HttpResponse does not allow Blob response, it always gives response as String and that mingled my image data. Disappointed again.

Then finally i have decided to create dynamic generated barcode on PHP and save as temprary images and use that url in my img tag and renderAs="PDF", this time i got success :)

Here is the working demo for my work:-
https://labsprojects-developer-edition.ap1.force.com/BarCodeSample

If you need complete code for above demo site, here is the link:
http://www.aslambari.com/SF_Barcode.zip

Enjoy barcoding :)

Thanks
Aslam Bari

ApexDoc Eclipse Plugin For Force.com IDE

Tuesday, February 1, 2011 by Aslam - The Alexendra
Hi All,
I am very happy to announce that now ApexDoc is available as Native "Eclipse Plugin". So no more need to go command line and provide parameters. You can simply go to your Force.IDE, select your classes folder, right click on that and Generate your ApexDoc, all in one place. Isn't it cool :)


Ok, so here is the guidelines, how to install the plugin and use it.

1) Go to http://www.aslambari.com/apexdocplugin.html

2) Download the plugin from there. It will give you one plugins.zip file. Unzip that file.

3) You will get one "com.apex.doc_1.0.0.jar" file. Copy this file.

4) Go to your eclipse installation directory and find your plugins folder there, For example D:\Eclipse\plugins

5) Paster the copied jar file into this directory.

6) Now restart (if eclispe is already running) your eclipse

7) Now go to any Force.com Project you have setup in your eclipse. Open the classes folder.

8) Right click on that folder, from shortcut menu select "Force.com", a sub menu will appear, you can see "Generate ApexDoc..." menu item in your submenu.



9) Now click on "Generate ApexDoc...". One ApexDoc dialog box will open.



10) All options here are self explanatory. The "Source Directory" is already filled up your selection. The "Target Directory" is where you want to generate output Documentation generated. "Home HTML File" is your sample home file for index page. "Author File" is for header areas for documentation.

11) Select appropriate options and Click on "Generate" button. It will generate documentation on your selected target directory and you will be prompted one message dialog that says "DONE"

12) If there is any issue, not generated your documentation, you can find one log file generated in your eclipse installation with name "apex_doc_log.txt".

So, this is very useful for Force.com developers and non programmers to use this plugin tool. You can also check one screenshot here: http://www.aslambari.com/apexdocplugin.html

For learning how comments syntax will be in your code refer my previous blog:
http://techsahre.blogspot.com/2011/01/apexdoc-salesforce-code-documentation.html

The plugin is in pre-mature stage and may contains bugs. Feel free to report me all bugs or enhancement you find, on my email. I will try to fix those.

So enjoy documenting :)

Thanks
Aslam Bari