Monday 6 February 2012

Call Apex Class method from visualForcePage

Let the apex class is with method as following :-


global class myClass { 
    webService static String myMethod() {     
      return "call to apex method." ; 
   }
}

  And visualForce Page is :- 

<apex:page >
   
 <!--Need this to make connection -->
 <script src="/soap/ajax/15.0/connection.js" type="text/javascript"/>
 <script src="/soap/ajax/15.0/apex.js" type="text/javascript" />

  <script type="text/javascript">
        
       function callApexMethod() {
  //     sforce.debug.trace= true;
         sforce.connection.sessionId = '{!$Api.Session_ID}' ;
         var msg = sforce.apex.execute("myClass","myMethod",{});
         alert(msg); 
           
      }
      
  </script>
  <h1>Congratulations</h1>
  Call apex method from page.
  <apex:form >  
    
      <apex:commandButton value="checkValidations"
onclick="callApexMethod();"/>
    
  </apex:form>  
  
</apex:page>
Result would be an alert 'call to apex method.';

Thursday 12 January 2012

Saved MultiSelect Value in Account Object

There may be requirement for save multiple values from visual page to one custom field of salesforce objects by multi Select. In this example I am saving firm Type a custom field in Account Object(Firm_Type__c).

1) Create a custom field of type Picklist (Multi-Select).



2) Create A visual force page.


<apex:page controller="multiselectPicklistSelectList2" showHeader="false" sideBar="false" >
<apex:form >
   <apex:sectionHeader title="Saved MultiSelect Value in Account Object"></apex:sectionHeader>

   <apex:pageBlock id="pageBlock" title="Search Filter">
    <apex:pageMessages ></apex:pageMessages>
    <apex:pageBlockSection title="Firm Types" columns="2" collapsible="true">
       
      
       <apex:outputLabel >Firm Type</apex:outputLabel>
       <apex:selectList multiselect="true" value="{!firms}" style="width:215px;height:100px">
            <apex:selectOptions value="{!items}" />
        </apex:selectList>
    </apex:pageBlockSection>
   </apex:pageBlock>
   <apex:pageBlock id="searchResults" title="Results">
     <apex:pageBlockButtons >
        <apex:commandButton value="Show Saved Values" action="{!showFirms}" status="status" rerender="searchResults"></apex:commandButton>
     </apex:pageBlockButtons>
     <apex:pageMessages ></apex:pageMessages>
    
  </apex:pageBlock>
</apex:form>
</apex:Page>


3) Create A controller.

public class multiselectPicklistSelectList2 {

 private Account account ;  
 public String name { get; set; }
 public multiselectPicklistSelectList2()
 {
    
 }

  public Account getAccount() {
      return account;
 }
 public void setAccount(Account acc) {
      account= acc;
 }

  public PageReference showFirms() {
    //To convert Getting firms Array into String separated by semi-comma.
    String tempstr='';
     for(Integer i=0;i<getFirms().size();i++) tempstr+=getFirms()[i]+';';
  name = account.name;  
     Account newAcct = new Account(Name= 'name',Firm_Type__c= tempstr);
   
    if (tempstr == null || tempstr == '') {
      apexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, 'Please select one or more regions first.'));
      return null;
    }else {
     apexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, 'Selected firms are :- '+ getFirms()));
     upsert newAcct;
    }
      return null;
  }

   private List<User> users;

   public List<User> getUsers()
   {
    return users;
   }

   String[] firms = new String[]{};

   public String[] getFirms() {
           return firms;
    }
 
    public void setFirms(String[] firms) {
        this.firms = firms;
    }
 
     List<SelectOption> options = new List<SelectOption>();
     public List<SelectOption> getItems() {
        options.add(new SelectOption('Broker Dealer','Broker Dealer'));
        options.add(new SelectOption('Bank','Bank'));
        options.add(new SelectOption('Asset Manager','Asset Manager'));
        options.add(new SelectOption('Insurance','Insurance'));
        options.add(new SelectOption('RIA','RIA'));
        options.add(new SelectOption('Partner','Partner'));
        options.add(new SelectOption('Media','Media'));
        options.add(new SelectOption('Analyst','Analyst'));
        
        return options;
        }
 }

4)  Saved field in the Account would be :-