Osama's Weblog

The place where you get updated!
  • rss
  • Home
  • Who am I?
  • RSS Feeds

Visualforce enhancements

Osama | June 26, 2010

I was studying the latest pages developers guide and found some components that have been added to salesforce.com.

These are

  • Apex Panel Bar
  • Apex Panel Grid
  • Apex Tab Panel

These controls can further enrich the user experience and the developer can now avoid to use external library components such as jQuery etc in visualforce.

I have created a simple demo which can show these working. Its almost same as in the guide.

  • Share/Bookmark
Comments
No Comments »
Categories
APEX, VisualForce, salesforce
Tags
APEX, paelbar, panelgrid, salesforce, tabpanel, VisualForce
Comments rss Comments rss
Trackback Trackback

Enforcing 1:1 relationship in Salesforce.com

Osama | June 3, 2010

I was wondering what if I have to create a one to one relationship between two objects. There is no declarative way where we can specify a one to one relationship between two objects.

We can write a trigger to enforce a one to one relationship between two objects. The trigger can be written on the child object that validates if there is any other record with same Parent record ID. If found then instead of inserting the record, it gives an alert.

The trigger will generate a list of child objects with specified parent ID. If the list’s length that is retrieved is greater than one, it will generate an alert.

List<ChildObject> co = [select name from ChildObject where parentId = ParentId]
If  (co.Size() < 2)
{
//insert
}
else
//generate alert

  • Share/Bookmark
Comments
3 Comments »
Categories
APEX, salesforce
Tags
APEX, salesforce, trigger
Comments rss Comments rss
Trackback Trackback

How to: Retrieve a list of sObjects in Apex

Osama | May 11, 2010

Some one asked me how to retrieve a list of sObjects in an SF organization using Apex and bind it to a picklist in a VisualForce page.

List of sObjects is returned when you hit the schema of your SF organization. You can get the Label of sObjects as well as API names.

The following code snippet shows how to do the above mentioned task

VisualForce page:

<apex:page controller="Schema2" >
<apex:form >
<apex:SelectList value="{!val}" size="1">
<apex:selectOptions value="{!Name}"></apex:selectOptions>
</apex:SelectList>
</apex:form>
</apex:page>

Apex Controller Code:

public class Schema2 {
public String val {get;set;}
public List<SelectOption> getName()
{
List<Schema.SObjectType> gd = Schema.getGlobalDescribe().Values(); //extracts the List of sObjects in an organization
List<SelectOption> options = new List<SelectOption>();
for(Schema.SObjectType f : gd)
{
options.add(new SelectOption(f.getDescribe().getLabel(), f.getDescribe().getLabel()));
}
return options;
}
}

Feel free to ask any questions

  • Share/Bookmark
Comments
No Comments »
Categories
APEX, General, salesforce
Tags
APEX, salesforce, sObjects
Comments rss Comments rss
Trackback Trackback

Apex InputField on Force.com site (contd..)

Osama | March 10, 2010

Few days ago I wrote a blog post in which I mentioned that I wasn’t able to access apex:input field on a force.com site. With the help of a my blog reader, I figured out what was the problem. Its pretty straight forward.

You cannot give extra permissions to standard objects on a force.com site. But  you can give Modify All Data and View All Data permissions to a custom object . That means you can use apex:inputField on a force.com site which is bound to a custom object field. Make sure about field level security.

Thanks to Jason.

  • Share/Bookmark
Comments
2 Comments »
Categories
APEX, VisualForce, salesforce
Tags
APEX, force.com, inputField
Comments rss Comments rss
Trackback Trackback

JQueryUI Datepicker in a Visualforce page

Osama | March 4, 2010

JQuery has always fascinates me. It makes the UI very attractive and user friendly. I had to implement a datepicker in a text field. I decided to use the JQuery UI datepicker. I am sharing the code that I did. Its easy to understand.

I made a static resource with the name slider and uploaded all the css and js files of JQuery UI that are available for download here .

1) Include the css files in your page

<link type="text/css" href="{!URLFOR($Resource.Slider, 'themes/ui-lightness/ui.all.css')}" rel="stylesheet" />
<script type="text/javascript" src="{!URLFOR($Resource.Slider, 'jquery-1.3.2.js')}"></script>
 <script type="text/javascript" src="{!URLFOR($Resource.Slider, 'ui/ui.core.js')}"></script>
 <script type="text/javascript" src="{!URLFOR($Resource.Slider, 'ui/ui.datepicker.js')}"></script>

2) Make a input text field and create the following JQuery function as below

 <input id="datepicker" type="textbox" />
<script type="text/javascript">
    $(function() {
        $("#datepicker").datepicker({ showOn: 'button', buttonImageOnly: true, buttonImage: '{!$Resource.calendar}' });
    });
    </script>;;

3)   $(“#datepicker”).datepicker({ showOn: ‘button’, buttonImageOnly: true, buttonImage: ‘{!$Resource.calendar}’ });
The datepicker pops up when you click on the image button that is created using the above statement. If  you want to pop it up by clicking the textbox only, replace the above statement with the below one:

  $(“#datepicker”).datepicker();

I hope it helps for you as it did for me. Feel free to ask questions

  • Share/Bookmark
Comments
11 Comments »
Categories
APEX, General, VisualForce, salesforce
Tags
APEX, datepicker, jquery, VisualForce
Comments rss Comments rss
Trackback Trackback

Apex InputField on Force.com Site

Osama | February 17, 2010

I came across a strange situation today when I was trying to use a apex:inputField tag and call it on a visualforce page through force.com site. But the field wasn’t visible when seen on public force.com site.

This is what I did

 public class testClass
          {
               public Account acc {get; set;}
          }

and here is the visualforce page

<apex:page   controller=”testClass”>
<apex:inputfield value=”{!acc.Name}”/>
</apex:page>

I also changed the permissions in Force.com guest user profile. But still I wasn’t able to make the field visible. I think its a limitation from security point of view.

If someone else has experienced the same thing or has any recommendations, do let me know.

  • Share/Bookmark
Comments
1 Comment »
Categories
APEX, VisualForce, salesforce
Tags
APEX, force.com, inputField
Comments rss Comments rss
Trackback Trackback

Apex Scheduler – Automate your work

Osama | January 13, 2010

Have you ever made a custom logic to run an apex class or method synchronously? It is possible by using time based workflow and field updates and then call a trigger. But salesforce has simplify this thing by introducing Apex Scheduler which can run an apex class synchronously.

system.schedule(String JobName,String CronExpression,Object schedulable_class) does this work

Suppose there is a class called SchCls which needs to be scheduled. Make an object of the class and schedule it.

SchCls m = new SchCls();

String sch = ’20 30 8 10 2 *’;

system.schedule(‘Merge Job’, sch, m);

The CronExpression is the time when to schedule the job. It represents the time and date in following way:

Seconds Minutes Hours Day_of_month Month Day_of_week optional_year

Rest of the schedule method properties can be found here.

  • Share/Bookmark
Comments
No Comments »
Categories
APEX, General, salesforce
Tags
APEX, automate, schedule
Comments rss Comments rss
Trackback Trackback

Invoke APEX code from custom button

Osama | January 11, 2010

There is a really good example of invoking apex code through apex code here. But I have found another way to invoke the apex method without using visualforce pages.

This is how I did:

1) Suppose you want to place a button on the Account detail  page to call apex method. Clone the Account detail page by creating a new visualforce page.
<apex:page standardController=”Account” extensions=”newCOn” >
<apex:form >

<apex:commandButton value=”Convert” action=”{!getID}” ></apex:commandButton>
<apex:detail relatedList=”true” relatedListHover=”true” />
</apex:form>
</apex:page>
2) Create the method getID() in the extension class that has been defined above.
public class newCon()
{
public void getID()
{
//make your custom logic here
}
}
3) Override this page on VIEW page under Setup>Customize>Accounts.

And in this way you have called an apex method without redirecting it to another visualforce page.

Feel free to ask questions.

  • Share/Bookmark
Comments
No Comments »
Categories
APEX, VisualForce, salesforce
Tags
APEX, button, custom, VisualForce
Comments rss Comments rss
Trackback Trackback

Reading URL parameters in apex

Osama | January 5, 2010

In my  this post i mentioned how to access url paramerts using javascript. But there is another Apex Method that can be utilized to read the URL parameters.

Here are some examples:

ApexPages.CurrentPage().getParameters().get('id')

This can be used to get the ID of current record. There are several use cases of this.

another one:

ApexPages.CurrentPage().getHeaders().get('host');

this can be used to get the current host in the url for e.g: “cs5.salesforce.com”. There are server use cases for this as well.

Feel free to ask questions/concerns

  • Share/Bookmark
Comments
8 Comments »
Categories
APEX
Tags
APEX, parameters, query string, url
Comments rss Comments rss
Trackback Trackback

Paypal integration in Apex

Osama | December 23, 2009

Payment gateways are parimary requiremnt in most of the business application these days. Applications building up on force.com platform require payment gateways.

Integration of paypal seems to be a very common issue. One of my readers asked me to write the code to integrate paypal with apex.

Below I am writing the code which I wrote for a project. I hope this will help many of the apex developers. This particular method is for DoDirectPayment method.

public class PayPalApi
{
public static String result {set;get;}
public static String makeCallout(String amount, String shipToName, String shipToStreet1, String shipToStreet2, String shipToCity, String shipToState, String shipToPostalCode, String shipToCountry, String creditCardType, String creditCardNumber, String expMonth, String expYear, String payerFName, String payerLName, String payerCountry, String payerStreet1, String payerStreet2, String payerCity, String payerState, String payerPostalCode, String CVV2Number)
{ // Instantiate a new http object
Http h = new Http();
// Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
HttpRequest req = new HttpRequest();
// The webservice url - Pass in the endpoint to be used using the string url
String url = 'https://api-3t.sandbox.paypal.com/2.0/';
String soapXML;

soapXML =  '<soap:Envelope xmlns:soap=' + '\'' + 'http://schemas.xmlsoap.org/soap/envelope/'  + '\'' + ' xmlns:xsi=' + '\''+ 'http://www.w3.org/2001/XMLSchema-instance'  + '\'' + ' xmlns:xsd=' + '\''+ 'http://www.w3.org/2001/XMLSchema' + '\'' + '>';
soapXML += '<soap:Header><RequesterCredentials xmlns="urn:ebay:api:PayPalAPI"><Credentials xmlns="urn:ebay:apis:eBLBaseComponents">';
soapXML += '<Username>PAYPAL_USER_NAME</Username><ebl:Password xmlns:ebl="urn:ebay:apis:eBLBaseComponents">';
soapXML += 'PAYPAL_PASSWORD</ebl:Password><Signature>PAYPAL_SIGNATURE</Signature>';
soapXML += '</Credentials></RequesterCredentials></soap:Header><soap:Body><DoDirectPaymentReq xmlns="urn:ebay:api:PayPalAPI">';
soapXML += '<DoDirectPaymentRequest><Version xmlns="urn:ebay:apis:eBLBaseComponents">1.00</Version>';
soapXML += '<DoDirectPaymentRequestDetails xmlns="urn:ebay:apis:eBLBaseComponents">';
soapXML += '<PaymentAction>Sale</PaymentAction><PaymentDetails><OrderTotal currencyID="USD">' + amount + '</OrderTotal>';
soapXML += '<ShipToAddress><Name>' + shipToName + '</Name><Street1>' + shipToStreet1 + '</Street1><Street2>' +shipToStreet2 + '</Street2>';
soapXML += '<CityName>' + shipToCity + '</CityName><StateOrProvince>' + shipToState + '</StateOrProvince><PostalCode>' + shipToPostalCode + '</PostalCode>';
soapXML += '<Country>' + shipToCountry + '</Country></ShipToAddress>';
soapXML += '</PaymentDetails><CreditCard><CreditCardType>' + creditCardType + '</CreditCardType><CreditCardNumber>' + creditCardNumber + '</CreditCardNumber>';
soapXML += '<ExpMonth>' + expMonth + '</ExpMonth><ExpYear>' + expYear + '</ExpYear><CardOwner><PayerStatus>verified</PayerStatus><Payer>bgiles@ddd.com</Payer>';
soapXML += '<PayerName><FirstName>' + payerFName+ '</FirstName><LastName>' + payerLName + '</LastName></PayerName><PayerCountry>' + payerCountry + '</PayerCountry>';
soapXML += '<Address><Street1>' + payerStreet1 + '</Street1><Street2>' + payerStreet2 + '</Street2><CityName>' + payerCity + '</CityName>';
soapXML += '<StateOrProvince>' + payerState + '</StateOrProvince><Country>' + payerCountry + '</Country><PostalCode>' + payerPostalCode + '</PostalCode></Address>';
soapXML += '</CardOwner><CVV2>' + CVV2Number + '</CVV2></CreditCard></DoDirectPaymentRequestDetails>';
soapXML += '</DoDirectPaymentRequest></DoDirectPaymentReq></soap:Body></soap:Envelope>';

req.setBody(soapXML);
req.setEndpoint(url);
req.setMethod('POST');
req.setHeader('Content-length', '1753' );
req.setHeader('Content-Type', 'text/xml;charset=UTF-8'); req.setHeader('SOAPAction','');
req.setHeader('Host','api-aa.sandbox.paypal.com');

HttpResponse res = h.send(req);
String xml = res.getBody();

XmlStreamReader reader = res.getXmlStreamReader();
result = readXMLResponse(reader,'Ack');
return result;
}
public static String readXMLResponse(XmlStreamReader reader, String sxmltag)
{
string retValue; // Read through the XML
while(reader.hasNext())
{
if (reader.getEventType() == XmlTag.START_ELEMENT)
{
if (reader.getLocalName() == sxmltag) {
reader.next();
if (reader.getEventType() == XmlTag.characters)
{ retValue = reader.getText();
}
}
} reader.next();
}
return retValue;
}
}

Other paypal methods dont require this type of coding because they are mostly redirected to paypal website. If you need further help, feel free to ask questions.

P.S: This code is completely working. You just have to replace with your credentials.

  • Share/Bookmark
Comments
No Comments »
Categories
APEX
Tags
APEX, DoDirectPayment, paypal, VisualForce, web service
Comments rss Comments rss
Trackback Trackback

« Previous Entries

My status

Categories

  • .NET
  • APEX
  • consulting
  • General
  • Oracle CRM on Demand
  • salesforce
  • Uncategorized
  • VisualForce

MM Did You Know?

MM Did You Know? Wordpress plugin by Milan Milosevic. More...
Plugin by mmilan

Its all about cloud

Salesforce

.NET .NET 4.0 beta actionFunction actionSupport administrator android APEX Apex variable API button certification cloud computing consulting controller custom DoDirectPayment force.com GET goggles google html images input inputField JavaScript jquery master-detail matcher Parallel Programming parameters pattern paypal query string REGEX regular expression salesforce search url variable Visual Force VisualForce Visual Studio vmware web service XML PARSING DOM SAX STAX

WP Cumulus Flash tag cloud by Roy Tanck and Luke Morton requires Flash Player 9 or better.

My Tweets

Error: Please make sure the Twitter account is public.

Get Adobe Flash playerPlugin by wpburn.com wordpress themes
rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox