Thursday, November 20, 2014

How to Create Pivot Table in force.com Visualforce Page and Export as PDF

How to Create Pivot Table in force.com Visualforce Page 

Apex Class

Public with sharing class pivot
{
    public string pivotContent{get;set;}
    public string ReturnValue{get;set;}
    public string getPivot{get;set;}
    public boolean exportPdf{get;set;}

  public string getData()
  {    List<PivotData> PivotDataList=new List<PivotData>();
       List<Opportunity> OppList=[Select Account.Name,StageName,CloseDate,Amount from opportunity];
       for(Opportunity o :OppList)
       {
           PivotData p=new PivotData();
           p.AccountName=o.Account.Name;
           p.SageName=o.StageName;
           p.Year=string.valueof(o.CloseDate.Year());
           p.Month=string.valueof(o.CloseDate.month())  ;
           p.Amount=o.Amount;
           PivotDataList.add(p);
           
       }
       getPivot='visibility: visible';
       exportPdf=false;
       return JSON.serialize(PivotDataList);
  }
   public void Result()  
      {  
          getPivot='visibility: hidden';
         exportPdf=true;
             ReturnValue = 'Save successfully  '; 
      } 
   Public PageReference ViewPdf()
   {
        PageReference pageRef= new PageReference('/apex/ViewPivot');
          pageRef.setredirect(false);       
          return pageRef;
   }


  public class PivotData
  {
     public string AccountName{get;set;}
     public string SageName{get;set;}
     public string Year{get;set;}
     public string Month{get;set;}
     public decimal Amount{get;set;}
  }
}

Visualforce Page for Pivot

<apex:page controller="pivot">
    <head>
        <title>Pivot in Visualforce</title>
        <apex:stylesheet value="{!$Resource.Pivot}" />
        <script type="text/javascript" src="{!$Resource.jquery183min}" ></script>
        <script type="text/javascript" src="{!$Resource.jqueryui192custommin}" ></script>
        <script type="text/javascript" src="{!$Resource.PivotJS}" ></script>
    </head>
    <style>
        * {font-family: Verdana;}
    </style>
    <script type="text/javascript">
       function savedata()
       {
          var pivotContent=document.getElementById("output").innerHTML;
          TestAF(pivotContent);
         return true;
       }
 </script>
      <apex:form >
      <span class="btn" onclick="return savedata()"   > Get Pivot Data</span> 
      <apex:commandlink action="{!ViewPdf}"  target="_blank"   >
        <apex:commandButton value="View Pdf" />
      </apex:commandLink>
   
     <apex:actionfunction action="{!Result}" name="TestAF" rerender="resultPanel" status="TestStatus">
   
   
     <apex:param assignto="{!pivotContent}" name="FirstParameter" value=""> </apex:param>
    </apex:actionfunction>
    <apex:outputpanel id="resultPanel">
    <apex:actionstatus id="TestStatus" starttext="Processing..." stoptext="">
     <b></b>
     </apex:actionstatus>
   
        <script type="text/javascript">
        var InputData={!Data};
            $(function(){
                        $("#output").pivot(
                        InputData
           ,
        {
            rows: ["AccountName","Year","Month"],
            cols: ["SageName"]
        }
    );
             });
        </script>

     
        <div id="output" style="margin: 10px;"></div>
</apex:outputpanel>
    </apex:form>

</apex:page>

Visualforce Page for Pivot Dynamic

<apex:page controller="pivot">
 <html>
    <head>
        <title>Pivot Dynamic  in Visualforce</title>
        <apex:stylesheet value="{!$Resource.Pivot}" />
        <script type="text/javascript" src="{!$Resource.jquery183min}" ></script>
        <script type="text/javascript" src="{!$Resource.jqueryui192custommin}" ></script>
        <script type="text/javascript" src="{!$Resource.PivotJS}" ></script>
    </head>
    <style>
        * {font-family: Verdana;}
    </style>
    <body>
        <script type="text/javascript">
        var derivers =     $.pivotUtilities.derivers;
        var tpl =          $.pivotUtilities.aggregatorTemplates;
        var InputData={!Data};
            $(function(){
                        $("#output").pivotUI(
                        InputData
           ,
        {aggregators: {
                            "Count":      function() { return tpl.count()() },
                            "Sum Of Amount": function() { return tpl.sum()(["Amount"])},
                        }}
    );
             });
        </script>

     
        <div id="output" style="margin: 10px;"></div>

    </body>
</html>
</apex:page>

Visualforce Page for Pivot Pdf

<apex:page controller="pivot" renderAs="PDF"  contentType="text/pdf#SamplePivot.pdf">
 <apex:stylesheet value="{!$Resource.Pivot}" />
 <apex:outputText value="{!pivotContent}" escape="false"></apex:outputText> 
</apex:page>

more information Pivot js

Output 

Pivot Normal


Pivot Dynamic 



Tuesday, November 11, 2014

How to enable SOAP, SSL , cURL, OpenSSL in PHP

Open PHP.ini file in our PHP directory

SOAP Enabled

extension=php_soap.dll
[soap]
; Enables or disables WSDL caching feature.
; http://php.net/soap.wsdl-cache-enabled
soap.wsdl_cache_enabled=1
; Sets the directory name where SOAP extension will put cache files.
; http://php.net/soap.wsdl-cache-dir
soap.wsdl_cache_dir="/tmp"
; (time to live) Sets the number of second while cached file will be used
; instead of original one.
; http://php.net/soap.wsdl-cache-ttl
soap.wsdl_cache_ttl=86400
; Sets the size of the cache limit. (Max. number of WSDL files to cache)
soap.wsdl_cache_limit = 5

OpenSSL Enabled

      extension=php_openssl.dll 

cURL enable

extension=php_curl.dll



JAVA Salesforce integration

How to integrate Salesforce with Java 


1.       Generate WSDL from Salesforce organization.
2.       Download Java library Link
3.       Create WSDL jar file following command

"C:\Program Files\Java\jdk1.7.0_65\bin\java.exe" -Dpackage-prefix=wsc -classpath force-wsc-29.0.0.Jar;js-1.7R2.jar;stringtemplate-4.0.2.jar;antlr-complete-3.5.1.jar com.sforce.ws.tools.wsdlc enterprise.wsdl enterprise.jar

[WSC][Generator.generate:130]Generating Java files from schema...
[WSC][Generator.generate:130]Generated 529 java files.
[WSC][Generator.compileTypes:126]Compiling to target 1.6...
warning: [options] bootstrap class path not set in conjunction with -source 1.6
1 warning
[WSC][Generator.compileTypes:126]Compiled 532 java files.
[WSC][wsdlc.run:111]Generating jar file ... enterprise.jar
[WSC][wsdlc.run:111]Generated jar file enterprise.jar

4.       Create Java Project
5.       Includes Java library for Salesforce
a.       antlr-complete-3.5.1.jar
b.      stringtemplate-4.0.2.jar
c.       js-1.7R2.jar
d.      force-wsc-29.0.0.jar
6.       Java code for Create ,update, delete
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package sfjava;

import com.sforce.soap.enterprise.wsc.Connector;
import com.sforce.soap.enterprise.wsc.DeleteResult;
import com.sforce.soap.enterprise.wsc.EnterpriseConnection;
import com.sforce.soap.enterprise.wsc.Error;
import com.sforce.soap.enterprise.wsc.QueryResult;
import com.sforce.soap.enterprise.wsc.SaveResult;
import com.sforce.soap.enterprise.sobject.wsc.Account;
import com.sforce.soap.enterprise.sobject.wsc.Contact;
import com.sforce.ws.ConnectionException;
import com.sforce.ws.ConnectorConfig;

public class SFJava {

static final String USERNAME = "gouranga@force.com";
static final String PASSWORD = "watermelon21JGzfzbaD9a24aUrIodyOVK9cN";
static EnterpriseConnection connection;
       
    public static void main(String[] args)
    {
       ConnectorConfig config = new ConnectorConfig();
                    config.setUsername(USERNAME);
                    config.setPassword(PASSWORD);
                    //config.setTraceMessage(true);
              System.out.println("Password" + PASSWORD );
                      System.out.println("USERNAME" + USERNAME );
                    try {
                      
                      connection = Connector.newConnection(config);
                      
                      // display some current settings
                      System.out.println("Auth EndPoint: "+config.getAuthEndpoint());
                      System.out.println("Service EndPoint: "+config.getServiceEndpoint());
                      System.out.println("Username: "+config.getUsername());
                      System.out.println("SessionId: "+config.getSessionId());
                      
                      // run the different examples
                      queryContacts();
                      createAccounts();
                      updateAccounts();
                      deleteAccounts();
                      
                      
                    } catch (ConnectionException e1) {
                        e1.printStackTrace();
                    }  
    }
     // queries and displays the 5 newest contacts
                  private static void queryContacts() {
                    
                    System.out.println("Querying for the 5 newest Contacts...");
                    
                    try {
                       
                      // query for the 5 newest contacts    
                      QueryResult queryResults = connection.query("SELECT Id, FirstName, LastName, Account.Name " +
                            "FROM Contact WHERE AccountId != NULL ORDER BY CreatedDate DESC LIMIT 5");
                      if (queryResults.getSize() > 0) {
                        for (int i=0;i<queryResults.getRecords().length;i++) {
                          // cast the SObject to a strongly-typed Contact
                          Contact c = (Contact)queryResults.getRecords()[i];
                          System.out.println("Id: " + c.getId() + " - Name: "+c.getFirstName()+" "+
                              c.getLastName()+" - Account: "+c.getAccount().getName());
                        }
                      }
                      
                    } catch (Exception e) {
                      e.printStackTrace();
                    }  
                    
                  }
                  
                  // create 5 test Accounts
                  private static void createAccounts() {
                    
                    System.out.println("Creating 5 new test Accounts...");
                    Account[] records = new Account[5];
                    
                    try {
                       
                      // create 5 test accounts
                      for (int i=0;i<5;i++) {
                        Account a = new Account();
                        a.setName("Test Account "+i);
                        records[i] = a;
                      }
                      
                      // create the records in Salesforce.com
                      SaveResult[] saveResults = connection.create(records);
                      
                      // check the returned results for any errors
                      for (int i=0; i< saveResults.length; i++) {
                        if (saveResults[i].isSuccess()) {
                          System.out.println(i+". Successfully created record - Id: " + saveResults[i].getId());
                        } else {
                          Error[] errors = saveResults[i].getErrors();
                          for (int j=0; j< errors.length; j++) {
                            System.out.println("ERROR creating record: " + errors[j].getMessage());
                          }
                        }  
                      }
                      
                    } catch (Exception e) {
                      e.printStackTrace();
                    }  
                    
                  }
                  
                  // updates the 5 newly created Accounts
                  private static void updateAccounts() {
                    
                    System.out.println("Update the 5 new test Accounts...");
                    Account[] records = new Account[5];
                    
                    try {
                       
                      QueryResult queryResults = connection.query("SELECT Id, Name FROM Account ORDER BY " +
                            "CreatedDate DESC LIMIT 5");
                      if (queryResults.getSize() > 0) {
                        for (int i=0;i<queryResults.getRecords().length;i++) {
                          // cast the SObject to a strongly-typed Account
                          Account a = (Account)queryResults.getRecords()[i];
                          System.out.println("Updating Id: " + a.getId() + " - Name: "+a.getName());
                          // modify the name of the Account
                          a.setName(a.getName()+" -- UPDATED");
                          records[i] = a;
                        }
                      }
                      
                      // update the records in Salesforce.com
                      SaveResult[] saveResults = connection.update(records);
                      
                      // check the returned results for any errors
                      for (int i=0; i< saveResults.length; i++) {
                        if (saveResults[i].isSuccess()) {
                          System.out.println(i+". Successfully updated record - Id: " + saveResults[i].getId());
                        } else {
                          Error[] errors = saveResults[i].getErrors();
                          for (int j=0; j< errors.length; j++) {
                            System.out.println("ERROR updating record: " + errors[j].getMessage());
                          }
                        }  
                      }
                      
                    } catch (Exception e) {
                      e.printStackTrace();
                    }  
                    
                  }
                  
                  // delete the 5 newly created Account
                private static void deleteAccounts() {
                    
                    System.out.println("Deleting the 5 new test Accounts...");
                    String[] ids = new String[5];
                    
                    try {
                       
                      QueryResult queryResults = connection.query("SELECT Id, Name FROM Account ORDER BY CreatedDate DESC LIMIT 5");
                      if (queryResults.getSize() > 0) {
                        for (int i=0;i<queryResults.getRecords().length;i++) {
                          // cast the SObject to a strongly-typed Account
                          Account a = (Account)queryResults.getRecords()[i];
                          // add the Account Id to the array to be deleted
                          ids[i] = a.getId();
                          System.out.println("Deleting Id: " + a.getId() + " - Name: "+a.getName());
                        }
                      }
                      
                      // delete the records in Salesforce.com by passing an array of Ids
                      DeleteResult[] deleteResults = connection.delete(ids);
                      
                      // check the results for any errors
                      for (int i=0; i< deleteResults.length; i++) {
                        if (deleteResults[i].isSuccess()) {
                          System.out.println(i+". Successfully deleted record - Id: " + deleteResults[i].getId());
                        } else {
                          Error[] errors = deleteResults[i].getErrors();
                          for (int j=0; j< errors.length; j++) {
                            System.out.println("ERROR deleting record: " + errors[j].getMessage());
          }
                        }  
                      }
                      
                    } catch (Exception e) {
                      e.printStackTrace();
                    }  
                    
                  }
   
}

Result
run:
Passwordwatermelon21JGzfzbaD9a24aUrIodyOVK9cN
USERNAMEgouranga@force.com
Auth EndPoint: https://login.salesforce.com/services/Soap/c/32.0
Service EndPoint: https://ap1.salesforce.com/services/Soap/c/32.0/00D90000000xqwN
Username: gouranga@force.com
SessionId: 00D90000000xqwN!ARUAQPxajvZ29zWyWR15egV7r_.mbtVuYW.vkQKTsZoiIjLdnK0GKxSFs5jgjej9O6IcuHmWPdrIbdL3AuxcShlQcU_BBARH
Querying for the 5 newest Contacts...
Id: 0039000001BsyyIAAR - Name: Gouranga Sadhukhan - Account: Primalink
Id: 00390000016M9I7AAK - Name: Edna Frank - Account: GenePoint
Id: 00390000016M9I5AAK - Name: Tom Ripley - Account: United Oil & Gas, Singapore
Id: 00390000016M9I6AAK - Name: Liz D'Cruz - Account: United Oil & Gas, Singapore
Id: 00390000016M9HsAAK - Name: Sean Forbes - Account: Edge Communications
Creating 5 new test Accounts...
0. Successfully created record - Id: 00190000018SZ2zAAG
1. Successfully created record - Id: 00190000018SZ30AAG
2. Successfully created record - Id: 00190000018SZ31AAG
3. Successfully created record - Id: 00190000018SZ32AAG
4. Successfully created record - Id: 00190000018SZ33AAG
Update the 5 new test Accounts...
Updating Id: 00190000018SZ2zAAG - Name: Test Account 0
Updating Id: 00190000018SZ33AAG - Name: Test Account 4
Updating Id: 00190000018SZ32AAG - Name: Test Account 3
Updating Id: 00190000018SZ31AAG - Name: Test Account 2
Updating Id: 00190000018SZ30AAG - Name: Test Account 1
0. Successfully updated record - Id: 00190000018SZ2zAAG
1. Successfully updated record - Id: 00190000018SZ33AAG
2. Successfully updated record - Id: 00190000018SZ32AAG
3. Successfully updated record - Id: 00190000018SZ31AAG
4. Successfully updated record - Id: 00190000018SZ30AAG
Deleting the 5 new test Accounts...
Deleting Id: 00190000018SZ2zAAG - Name: Test Account 0 -- UPDATED
Deleting Id: 00190000018SZ33AAG - Name: Test Account 4 -- UPDATED
Deleting Id: 00190000018SZ32AAG - Name: Test Account 3 -- UPDATED
Deleting Id: 00190000018SZ31AAG - Name: Test Account 2 -- UPDATED
Deleting Id: 00190000018SZ30AAG - Name: Test Account 1 -- UPDATED
0. Successfully deleted record - Id: 00190000018SZ2zAAG
1. Successfully deleted record - Id: 00190000018SZ33AAG
2. Successfully deleted record - Id: 00190000018SZ32AAG
3. Successfully deleted record - Id: 00190000018SZ31AAG
4. Successfully deleted record - Id: 00190000018SZ30AAG

BUILD SUCCESSFUL (total time: 29 seconds)

Monday, November 10, 2014

Apex Class Definition like Virtual, abstract, extends (Inheritance)


Abstract class

public abstract class mobileabstract
{
  public string SMS{get;set;}
  public string calling{get;set;}
  public mobileabstract()
  {
  }
  public virtual decimal Callcharge(integer t)
  {
        if(t<10)
        {
          return t*1.00;
        }else
        {
          return t*1.5;
        }
       
  }
  public abstract decimal myMethod();
 }
Extends Class

public class MultimediaPhonemobileabstract  extends mobileabstract
{
  public string mp3{get;set;}
  public string camera{get;set;}
  public integer calltype{get;set;}
  public override decimal myMethod(){
        return 52;
    }
   public override decimal Callcharge( integer t)
   {
        if(calltype==1)
        {
           return t*1.0;
        }else
        {
           return t*2.0;
        }
   }
}
  • The abstract definition modifier declares that this class contains abstract methods, that is, methods that only have their signature declared and no body defined. 
  • abstract class methods MUST be overridden
Virtual class
public virtual class mobile
{
  public string SMS{get;set;}
  public string calling{get;set;}
  public mobile()
  {
  }
  public  decimal Callcharge(integer t)
  {
        if(t<10)
        {
          return t*1.00;
        }else
        {
          return t*1.5;
        }
       
 
  }
  //public abstract decimal myMethod(); // We cannot
 use
 
 
 
 
}
Extends Class

public class MultimediaPhone extends mobile
{
  public string mp3{get;set;}
  public string camera{get;set;}
  public integer calltype{get;set;}
 
  public override decimal Callcharge( integer t)
   {
        if(calltype==1)
        {
           return t*1.0;
        }else
        {
           return t*2.0;
        }
   }

 
 /* public override decimal myMethod(){
        return 52;
    }*/
}

  • The virtual definition modifier declares that this class allows extension and overrides. You cannot override a method with the override keyword unless the class has been defined as virtual.
  • virtual class can be overridden in derived classes

 Interface

public interface mobileInterface
{

  decimal SMSCharge(integer duration);
  decimal callCharge(integer duration);
 
 
  /* We can not define
  public  decimal Callcharge(integer t)
  {
        if(t<10)
        {
          return t*1.00;
        }else
        {
          return t*1.5;
        }
       
 
  }
 
  */
 
 
 
 
 
}
 Implements

public class NewMobile implements mobileInterface
{
   public decimal SMSCharge(integer duration)
   {
      return  duration*1.3;
   }
   public decimal callCharge(integer duration)
   {
       return  duration*1.1;
   }
   //we can add
    public decimal OtherCharge(integer duration)
   {
       return  duration*1.1;
   }
}


reference 
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_defining.htm