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