Getting Salesforce Organization Server Url

Tuesday, February 23, 2010 by Aslam - The Alexendra
Hi All,
Recently i faced a situation where i need to get Current Org Server URL (Host Url) in Trigger. I know we can get it easily in VF or Apex Class, but what about in Trigger or some future method? One of my colleague Mr. Yogesh Rankawat suggested me a very good way to do this easily (Thanks to him). So here i am going to tell you this method.

1) Create a simple class with following code:-
public class HostUrl{
public string url {get;set;}
public HostUrl(){
url = ApexPages.currentPage().getHeaders().get('Host');
}
}


2) Create a even simple VF page with following code:-
<apex:page sidebar="false" showHeader="false" controller="HostUrl" contentType="text">{!url}</apex:page>

3) Now just simply use below code to get the Server URL

PageReference pg = Page.HostUrl;
String SERVER_URL = pg.getContent().toString();

Isn't it too simple!!! If you know even better way, then please share.

Thanks
Aslam Bari
Posted in | 2 Comments »

Latest From My Blogs

Tuesday, February 16, 2010 by Aslam - The Alexendra
Hi Friends,
Here are my some latest blogs on my projects related to Salesforce:-

1. Salesforce Pinger
Link

2. Salesforce Bot
Link

Thanks
Posted in | 0 Comments »

Python Integration with Salesforce (Windows)

Thursday, January 28, 2010 by Aslam - The Alexendra
Hi,

Recently I was challenged to access Salesforce from a Python script. Like any Python beginner I had some problems setting the Python environment up correctly and then connecting to Salesforce. However, after much trial and error I succeeded, so I am here to share my Python-Salesforce journey. Lets walk through it together :)

Step 1: Get the latest Python code (I used Python2.6) and install it on your disk at "c:\Python26"

Step 2: Download the suds from https://fedorahosted.org/suds Extract suds in a folder such as "C:\Python26\suds"

Step 3: Download the latest setuptools for your python(2.6) from here http://pypi.python.org/pypi/setuptools#downloads. Choose the "setuptools-0.6c11.win32-py2.6.exe (md5)" for windows and install it in your python directory (Default C:\Python26)

Step 4: Open the command prompt, go to the suds directory and issue this command:-

c:\Python26\suds> C:\Python26\python.exe ./setup.py install

Suds will install successfully

Step 5: Now install Apache or use your existing apache server if you have one. The only important thing here is the httpd.conf file. So do the following main changes in your apache httpd.conf file:-

a) Find the line "Options Indexes FollowSymLinks" and replace it with

Options Indexes FollowSymLinks +ExecCGI


b) Find the line "AddHandler cgi-script" and replace it with

AddHandler cgi-script .cgi .py


I think that's it. If you still have issues setting up your Apache server properly then email me and I'll be happy to provide you my httpd.conf.

Step 5: Now start your Apache server and test a simple python script. Create a file test.py and put it on your server such as at "C:\wamp\www".

-------------------------------------------------------------
#!c:/Python26/python.exe -u
import datetime
import re
import string
import sys
import unittest
import logging
import test_config


logging.basicConfig(level=logging.INFO)
logging.getLogger('suds.client').setLevel(logging.DEBUG)



print "Content-type: text/html"
print

print "Hello1"


------------------------------------------------------------


open browser and hit the url like
http://localhost/test.py

Hurray!!! you have seen "Hello1". (If not :(, try again being careful with each step)


Step 6: Download your Enterprise WSDL from your Salesforce organization and put it on your server folder where you can access it easily

http://localhost/pythontest/enterprise.wsdl.xml

Step 7: Download "salesforce-python-toolkit" from http://code.google.com/p/salesforce-python-toolkit/downloads/list .

Extract it to a folder like "sforce". You must put it in your classpath for python. However, as I was new I didn't spend much time on this and for the sake of expediency I put this "sforce" folder into "C:\Python26\Lib".

Open the test_config.py file from this folder and put your organization's Username and Password properly. Save this file.

Open your test.py file again and now let's perform some basic query examples:

---------------------------------------------------------------------------

#!c:/Python26/python.exe -u
import datetime
import re
import string
import sys
import unittest
import logging
import test_config


logging.basicConfig(level=logging.INFO)
logging.getLogger('suds.client').setLevel(logging.DEBUG)

from sforce.enterprise import SforceEnterpriseClient

from suds import WebFault

print "Content-type: text/html"
print

h = SforceEnterpriseClient('http://localhost/pythontest/enterprise.wsdl.xml')
h.login(test_config.USERNAME, test_config.PASSWORD, test_config.TOKEN)

result = h.queryAll('SELECT Account.Name, FirstName, LastName FROM Contact LIMIT 2')
for record in result.records:
print record.FirstName, record.LastName
print record.Account.Name

result = h.logout()
---------------------------------------------------------------------------------------

In the above example, we queryed the Contact object. The important thing to note here is that the enterprise wsdl xml is referenced in your script. Python accepts the filename in full url format, so please specify it as either http:// or file:/// format.

Open browser and run the code. The results will appear on your screen.

Hope you enjoyed our little journey....

For any further questions email me aslam.bari@gmail.com


Thanks
Aslam Bari
Posted in | 2 Comments »

IE6 Select Ovelapping Dynamic popup Div Solution

Tuesday, January 19, 2010 by Aslam - The Alexendra
Hi All,
Many times i found the problem with Dynamic popup on js event showing on page in IE6. If you have any Select element on the page, your div will not come on top of this Select. So here is the IFrame hack for this problem:-

// Outer Div -->
<div id="popupParent" style="background-color:#fefdb9;
width:346px;
position:absolute;
display:none;
top:0px;
overflow:hidden;
z-index:20;
">

// IFRAME HACK -
<iframe id="menu4iframe" src="javascript:'';" marginwidth="0" marginheight="0" align="bottom" scrolling="no" frameborder="0" style="position:absolute; left:0; top:0px; display:block; filter:alpha(opacity=0);height:300px"></iframe>

// INNER DIV FOR YOUR COntents -->
<div id="popup" style="border:thin solid #ffa500;padding:2px;overflow:hidden;background-color:#fefdb9;"/>
</div>

On any js event you just need to set your inner html in 'popup' and set the display property of 'popupParent' div.
Thats It.

Thanks
Posted in | 0 Comments »

Now Playing with APIs is Easy

Thursday, December 17, 2009 by Aslam - The Alexendra
If you wanna love to extract flicker, youtube, twitter, rss feeds etc using apis and wanna a easy way to check what results coming in a starndard way....here is the solution YQL :-

See this for more info:-
http://24ways.org/2009/the-web-is-your-cms

happy YQLing.... :)
Posted in | 2 Comments »

Salesforce Bot

Thursday, November 12, 2009 by Aslam - The Alexendra
Visit Here for a cool Salesforce Bot for you GTalk:-
http://salesforcebot.blogspot.com/
Posted in | 0 Comments »

Apex:commandButton and onClick issue...

Friday, October 2, 2009 by Aslam - The Alexendra
Hi,
Sometimes we find issues when we use <apex:commandButton> with both onClick and Action. We try to return false / true from our javascript method and expect things to be happen properly but sometimes it not happens. See below:-

<script>
function confirm(){
if(something condition){
return true;
}
return false;
}
</script>

<apex:commandButton value="Submit" action="{!CallAction}" onClick="return confirm();"/>

The above code does not call action sometimes..(Dont know why)...so if you change it with this, it will work:-
<apex:commandButton value="Submit" action="{!CallAction}" onClick="if(!confirm()) return false;"/>

The above code will work....Please don't ask why........


Thanks
Posted in | 6 Comments »