Thursday, July 24, 2014

How to use sObject in Apex salesforce

Hi so today I was searching around the net to get some example to use sObjects of salesforce but I was not able to find some, so I decided to make one here with very easy approach.


  • Declaring a sObject - sOject objAcc = new sObject (); // without type
                                     sOject objAcc = new Account (); // with type
  • Getting field values - sOject objAcc = new Account ();
                                    Id oId = objAcc.Id;
                                    String nameAcc = objAcc.get('Name'); // "Name" is field's API name
  • Setting field values - sOject objAcc = new Account ();
                                    objAcc.put('Name','TestName');
Hope this helps someone.

How to solve cross domain parent URL access

So you are here because you were trying to access parent page's url with the child iframe and ran into this error:

"SecurityError: Blocked a frame with origin https://c.ap1.visual.force.com from accessing a frame with origin https://ap1.salesforce.com. Protocols, domains, and ports must match."

In simple words what this means that the site is not allowing you to gain access to the parent frame because it can be used in many hack attacks and is trying to safe guard the application. But if you still need this there is an alternative approach insted of doing this
window.parent.location.href()
You can do this:
var hurl = document.referrer;
So now you can access the cross domain parent url.
Hope this helps some one.

Wednesday, July 23, 2014

How to use Rest Api services in Salesforce

Ok so I am finding many post in which people want to learn Rest api or they already know the fundamental but are unable to create an example for some reason. So I will be showing you that how we can hit a REST GET service and retrieve data from salesforce.

We need the following things to do so:
  1. A rest api class definition in our saleforce org.  
  2. A rest callout tool.
  3. "Authorization" some kind of token so we can get in.
So first we need our Rest api class:


Now we need a rest callout tool so download a chrome extension called "Advance rest client" Link.

And now we need authorization so there are many ways for quick I will be showing the simplest way just to test it.
Open "https://login.salesforce.com/"> Login > Open Developer Console > Debug > Open Execute .. >
Type : system.debug('**'+userinfo.getSessionId()); > Execute.
Now search the debug log for "**" and copy the whole token eg: 00D90000000eCBG!AGHYT.BrtWEQdym9cDsUGcvPYEVKr4aEuNHSxqpMH_YQHS53.24x.HZxKDh1XGbNpjopkdylqvTv5tnFUOIUH.Epzxxxx

Now open the advance rest client tool and tupe this:
  • In the URL : https://<server>.salesforce.com/services/apexrest/showAcc?setquery=select Id,Name from Account limit 10
  • In the Header: Authorization: OAuth 00D90000000eCBG!AGHYT.BrtWEQdym9cDsUGcvPYEVKr4aEuNHSxqpMH_YQHS53.24x.HZxKDh1XGbNpjopkdylqvTv5tnFUOIUH.Epzxxxx
  • Click send.
That is it you will receive a response some thing like this:

Note: OAuth flow here: https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/intro_understanding_web_server_oauth_flow.htm


Hope this help you.

Tuesday, July 22, 2014

How to make dynamic reference in apex code

So we are used to develop apex code with static reference. What it means is the code is referencing the object directly and when this code is moved in a package then the referenced object will automatically be included in it. But it might happen that you only need the code to be included in the package as the object is already present in the install org.

Static reference code:

Dynamic reference code:

Thursday, July 10, 2014

How to create a baisc table to display the list of data in Visualforce

So this is a quick example to bind list of data from the controller or extension to the visualforce page. Below is the visualforce page and extensions:

<apex:page standardController="Account" extensions="VFBasic3"> <!-- Give the name for extension -->
<apex:form >

<!-- Declare a page block to contain every thing in  -->
    <apex:pageBlock title="Account List">

<!-- Declare a page block section for formating  -->
<apex:pageBlockSection columns="1">

<!-- Declare a page block table to display the data in  -->
            <apex:PageBlockTable var="row" value="{!accList}">
            <!-- {!accList} is our list in the extension and row is our variable for each record -->
<!-- Declare a column  -->
                <apex:column headerValue="Name">

<!-- Declare a field to bind the values with-->
                    <apex:outputField value="{!row.Name}"/>
                </apex:column>
                <apex:column headerValue="Phone">
                    <apex:outputField value="{!row.Phone}"/>
                </apex:column>
                <apex:column headerValue="Fax" >
                    <apex:outputField value="{!row.Fax}"/>
                </apex:column>
            </apex:PageBlockTable>
            <hr/>
            <apex:dataTable var="row" value="{!accList}">
                <apex:column headerValue="Name" width="200">
                    <apex:outputField value="{!row.Name}"/>
                </apex:column>
                <apex:column headerValue="Phone" width="200">
                    <apex:outputField value="{!row.Phone}"/>
                </apex:column>
                <apex:column headerValue="Fax" width="100">
                    <apex:outputField value="{!row.Fax}"/>
                </apex:column>
            </apex:dataTable>
            <hr/>
            <apex:repeat var="row" value="{!accList}">
                <apex:outputField value="{!row.Name}"/>
            </apex:repeat>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>
</apex:page>
public class VFBasic3 {
public List<Account> accList{get;set;}
    //Initialize the list to avoid null pointers
    public VFBasic3(ApexPages.StandardController controller) {
        accList=getListAcc();
    }
    //Use SOQL to get records
    public List<Account> getListAcc(){
        List<Account> acc= [select Id,Name,Phone,Fax from Account limit 10];
        return acc;
    }
}
The above code is mostly self explanatory if you have any questions please post a comment.

Hope this helped you!!