Showing posts with label Salesforce. Show all posts
Showing posts with label Salesforce. Show all posts

Monday, June 24, 2013

Salesforce - Pass parameter value from one page to another page

Here i have provided a sample to understand how to pass parameter value from one page to another and retrieve it. I have used two pages named Books and SelectedBooks. Books page has a PageBlockTable it dispalyes only book names from the Book_c custom object. The book names are displayed as links using <apex:CommandLink>. When a user click the link the first page Book is navigate to second page SelectedBook with the parameter value that is passed using <apex:param>. The Second page then retrieves the parameter value and displays the book details in a PageBlockTable
Design code for Books page:

By default the PageBlockTable displays its value as OutputLabel but we need to click the value for navigation purpose. So here i used <apex:commandLink> to convert it as Link. Book name is passed as parameter using <apex:param>

<apex:page controller="BooksController">  
   <apex:form >  
   <apex:sectionHeader subtitle="All Books" title="Books"/>  
      <apex:pageBlock >  
       <apex:pageBlockTable value="{!allbooks}" var="a">  
           <apex:column headervalue="Name">  
                 <apex:commandLink value="{!a.Name}" action="/apex/SelectedBook?id={!a.Name}">  
                 <apex:param name="id" value="{!a.Name}"/>  
                 </apex:commandLink>  
           </apex:column>  
             </apex:pageBlockTable>  
        </apex:pageBlock>  
   </apex:form>  
 </apex:page>  

Apex Code for Books page controller:

Here the getAllBooks fuction retrive the values from the Book__c custom objects and bind it to the PageBlockTable

 public class BooksController
 {  
      public List<Book__c> allbooks;   
      public List<Book__c> getAllBooks()  
      {  
           if(allbooks==null)  
           {  
                allbooks=[SELECT Name,Book_Author__c,Book_ISBN__c from Book__c];  
           }  
      return allbooks;  
      }  
 }  

Apex Code for SelectedBook page controller:

Here in the constructor the parameter value is retrieved using its name. Using that value the getSbook function gets the specific book details and bind it to the PageBlockTable.

 public class SelectedBookController 
 {  
      public String selectedName {get;set;}  
      public Book__c sbook;  
      public SelectedBookController()  
      {  
           sbook=new Book__c();  
           selectedName = ApexPages.currentPage().getParameters().get('id');  
      }  
       
      public Book__c getSbook()  
      {  
           sbook=[Select Name,Book_ISBN__c,Book_Author__c from Book__c where Name=:selectedName];  
           return sbook;  
      } 
 } 

Design code for SelectedBook page:

<apex:page controller="SelectedBookController">  
 <apex:form >  
      <apex:pageBlock >  
           <apex:pageBlockTable value="{!sbook}" var="b">  
                <apex:column value="{!b.Name}"/>  
                <apex:column value="{!b.Book_Author__c }"/>  
               <apex:column value="{!b.Book_ISBN__c}"/>  
             </apex:pageBlockTable>  
      </apex:pageBlock>  
 </apex:form>  
 </apex:page>  

Thursday, June 20, 2013

Salesforce - Insert data into Custom object using Apex

To insert data to Custom object using Apex just understand the below code. This is the basic for all other complex apex programming. Here in the Save() function the data you have enter in the text box will be saved. After inserting the data i am refreshing the page to clear the values in the text boxes. You han find the code in that function.
Apex Design Code:
 <apex:page controller="AddUserController">  
   <apex:form >  
     <apex:sectionHeader subtitle="All Books" title="Books" />  
     <apex:pageBlock >  
       <apex:PageBlockButtons >  
         <apex:commandButton action="{!Save}" value="Save"/>  
       </apex:PageBlockButtons>  
       <apex:pageBlockSection >  
         <apex:InputField value="{!users.Name}"/><br/>  
         <apex:InputField value="{!users.Age}" /><br/>  
         <apex:InputField value="{!users.Class}" /><br/>  
       </apex:pageBlockSection>  
   </apex:pageBlock>  
   </apex:form>  
 </apex:page>  

Apex Controller Code:
public class AddUserController  
 {  
   public User__c users;  
   public AddUserController()  
   {  
     users=new User__c();  
   }  
   public User__c getUsers()  
   {  
     return users;  
   }  
   public PageReference Save()  
   {  
     try  
     {  
       insert(users);  
       PageReference pagr= Page.UsersPage;  
       pagr.setRedirect(true);  
       return pagr;  
     }  
     catch(System.DMLException e)  
     {  
       return null;  
     }  
   }  
 }  

Salesforce - Display data from Custom object to PageBlockTable using Apex

Here i have provided code sample to understand, how to display data from custom objects to PageBlockTable. I have chosen pageBlocktable because it has predefined style. Don't want to use any css styles for that.
Apex Design Code:
 <apex:page controller="ViewUserController">  
   <apex:form >  
     <apex:sectionHeader subtitle="All Users" title="Users" />  
     <apex:pageBlock >  
       <apex:pageBlockTable value="{!allusers}" var="a">  
         <apex:column headervalue="Name" value="{!a.Name__c}" />  
         <apex:column headervalue="Age" value="{!a.Age__c}" />  
         <apex:column headervalue="Class" value="{!a.Class__c}" />  
       </apex:pageBlockTable>  
     </apex:pageBlock>  
   </apex:form>  
 </apex:page>  
Apex Controller Code:
 
 public class ViewUserController  
 {  
     public List<User__c> allusers;    
     public List<User__c> getAllUsers()  
     {  
         if(allusers==null)  
         {  
             allusers=[SELECT Name__c,Age__c,Class__c from User__c];  
         }  

         return allusers;  
     }  
 }  

Monday, May 20, 2013

Simple Salesforce.com Apex programming sample to call apex webservice using CSharp (C#)


Here is a very simple code to call a apex web service in salesforce using C#. It's also return a "Contact" object with values.The returning value is based on the query used in the apex web service code. From this example you can understand how to query salesforce objects, how to call apex web service, how to connect to salesforce via code and also you can understand how to retrieve salesforce object value.You can also query the custom objects.
You need to add two web reference in your application.
     1. Enterprise WSDL web reference
     2. WSDL for the webservice class you have created.

To get the Enterprise WSDL navigate to

  • Your Name -> Setup -> App Setup -> Develop -> API
  • There you can get the  Enterprise WSDL

To get the WSDL for the webservice class you have created, navigate to

  • Your Name -> Setup -> App Setup -> Develop -> Apex Class
  • There select your class. 
  • Then click generate WSDL

In the following code ApexService_WSDL is Enterprise WSDL web reference and Enterprise_WSDL is WSDL web reference for the webservice class you have created.

Apex WebService Code in SalesForce:
global class MyCustomController  
 {  
    webservice static Contact[] ReturnObject()  
    {  
      Contact[] cont=[SELECT Firstname, Lastname FROM Contact];  
      return cont;  
    }  
 }  

C# Code in Client Side Application:
SforceService binding = new SforceService();  
 private LoginResult lr;  
 binding.Timeout = 10000;  
 lr = binding.login("YOUR LOGIN EMAIL ID", "PASSWORD + SECURITY TOKEN");  
 binding.Url = lr.serverUrl;  
 binding.SessionHeaderValue = new Enterprise_WSDL.SessionHeader();  
 binding.SessionHeaderValue.sessionId = lr.sessionId;
 
 using (ApexService_WSDL.MyCustomControllerService client = new MyCustomControllerService())  
 {  
    client.SessionHeaderValue = new ApexService_WSDL.SessionHeader();   
    client.SessionHeaderValue.sessionId = lr.sessionId;                   
    ApexService_WSDL.Contact[] m = client.ReturnObject();  
 }  

Don't forget to replace you salesforce email id , password + security token ;). To get or reset your security token, navigate to the following path.

  •      Your Name -> Setup -> My Personal Information -> Reset Security Token, and click the Reset My Security Token button
  • And you will get a mail with security token.