Sunday, September 25, 2016

Odd Even Number in salesforce

                                Say Odd or Even number.

Today We will learn about how to use Odd or Even number in Apex class and Visualforce page in salesforce. 
1 . Create a visualforce page.

<apex:page controller="LearnOddOrEvenNumber" sidebar="false">
    <apex:form >
        <apex:pageBlock title="Find Odd/Even Number">
         <apex:pageMessages/>
           <apex:pageBlockButtons location="bottom">
               <apex:commandButton value="Submit" action="{!FindOddOrEvenNumber}"/>
           </apex:pageBlockButtons>
            <apex:pageBlockSection >
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Enter Number:" for="thr"/>
                    <apex:inputText value="{!Mynumber}" id="thr"/>                  
                </apex:pageBlockSectionItem>
                <apex:outputLabel value="Result : ">
                {!Result}
                </apex:outputLabel>
            </apex:pageBlockSection>
        </apex:pageBlock>
</apex:page>

2. Create a Apex class.

public class LearnOddOrEvenNumber{
    public integer Mynumber{get;set;}
    public string Result{get;set;}
    public pageReference FindOddOrEvenNumber(){
        if(math.mod(Mynumber,2) == 0){
            Result = 'This is Even number.';
        }else{
            Result = 'This is Odd number.';
        }
        return null;
    }
}

Monday, September 19, 2016

Using apex Trigger to create a Task.

When we create a record on account object then Task will be create automatically, using by Apex trigger.

First create Apex Trigger on object.


trigger CreateRelatedTask on Account (After insert) {
 
        list<Account> accountRecords = trigger.new;
     
        list<Task> taskList = new list<Task>();
        for(Account acct : accountRecords){
         
            Task t = new Task();
            t.Subject = 'Other';
            t.Priority = 'High';
            t.Description = 'Created this task on account creation.';
            t.ActivityDate = System.today();
            t.WhatId = acct.id;
         
            taskList.add(t);
        }
     
        if(taskList.size() > 0){
           insert taskList;
        }
     

}

Thursday, September 15, 2016

Use an In-line visualforce page

                                        Add In-Line Visualforce page.

1. Create Visualforce Page.

<apex:page standardController="Account" showHeader="false" >
  <center><h1 style="color:red">In-Line Visualforce Page</h1></center>
  <apex:pageMessage summary="Added In-Line Visualforce Page on Account Page Layout." severity="Info" strength="3" />
  <apex:form >
  <apex:pageBlock >
  <!-- <apex:pageblockButtons >
     <apex:commandButton value="Save" action="{!save}"/>
   </apex:pageblockButtons>-->
     <apex:pageblockSection title="In-Line Visualforce Page" columns="1">
       <apex:outputField value="{!account.name}"/>
       <apex:outputField value="{!account.phone}"/>
       <apex:outputField value="{!account.fax}"/>
       <apex:inlineEditSupport event="ondblClick" />
     </apex:pageblockSection>
  </apex:pageBlock>
  </apex:form>
</apex:page>

2. Go to Page Layout and select VF Page.


Output :




Wednesday, September 14, 2016

Create custom button

                                                        Create custom button

How to create custom button and link ?.

Step 1. First you must go to the Setup.




Step 2. Now you need to open site with all of your custom objects. Click on the Create and then on the Objects link:


Step 3.  Now you can see the list with all custom objects. Select the object where you want to create a new custom button:


Setup 4. Now you can see the custom Object page scroll down to the Button,Links and Actions and click on New Button and Link.


Setup 5. After that click New Button or Link the appear page:


Step 6. After save the Custom Button and Link then go to Page Layout:


Step 7. At the layout page select button.


Step 8. You just save the layout page and then go to the custom object item page to see.






Monday, September 12, 2016

Outbound Messaging :: Send Email using apex class and Visualforce page.

                     Outbound Messaging


Send email using apex class and Visualforce page:

1. Create apex controller

public class EmailWithVisualforce{
   public string subject {get;set;}
   public string Body{get;set;}
   public string EmailAddress{get;set;}
   list<string> emails = new list<string>();
 
  public pageReference SendEmail(){
     Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
     //Split multiple email addess by , seperated.
     emails.addAll(EmailAddress.split(','));
     email.setSubject( subject );
     email.setPlainTextBody( Body);
     email.setToAddresses( emails );
     Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
     return null;
  }
}

2. Create Visualforce Page:

<apex:page controller="EmailWithVisualforce">
   <apex:form >
      <apex:pageBlock title="Email With Visulforce Page">
          <apex:pageBlockSection columns="1">
             <apex:pageBlockSectionItem >
                 <apex:outputLabel value="E-Mail:" for="edid"/>
                 <apex:inputtext value="{!EmailAddress}" id="edid"/>
             </apex:pageBlockSectionItem>
             <apex:pageblockSectionItem >
                 <apex:outputLabel value="Subject : " for="subject"/>
                 <apex:inputtext value="{!subject }" id="subject"/>
             </apex:pageblockSectionItem>
           
             <apex:pageblockSectionItem >
                <apex:outputlabel value="Enter Message Here:" for="Body"/>
                <apex:inputtextarea value="{!Body}" rows="8" cols="80" id="Body" style="color:red" />
             </apex:pageblockSectionItem>
          </apex:pageBlockSection>
     
        <apex:pageBlockButtons >
           <apex:commandButton value="Send !" action="{!SendEmail}"/>
        </apex:pageBlockButtons>
       
      </apex:pageBlock>
   </apex:form>
</apex:page>

You can see below UI once you done the step 1 & 2:





Sunday, September 11, 2016

Create Picklist, Checkbox, Radio button Dynamically using Apex Class or on Visualforce Page.

Create Custom  Picklist , Radio Button, Checkbox, InputText   using apex class and Visualforce page.


1.   Create Apex Class:

public class customPickListInVFDemoController {
    public String selectedCountry1{get;set;}
    public String selectedCountry2{get;set;}
    public string selectedgender{get;set;}
    public list<string> selectCheckBoxesItem{get;set;}
    public string selectedInputText{get;set;}
    public string selectedInputName{get;set;}
    public string selectedInputEmail{get;set;}
    //public string selectedInputEmailaddress{get;set;}
    public customPickListInVFDemoController(){
       selectCheckBoxesItem = new list<string>();
    }
     
    public List<SelectOption> getCountriesOptions() {
        List<SelectOption> countryOptions = new List<SelectOption>();
        countryOptions.add(new SelectOption('','-None-'));
        countryOptions.add(new SelectOption('INDIAN','India'));
        countryOptions.add(new SelectOption('USA','USA'));
        countryOptions.add(new SelectOption('United Kingdom','UK'));
        countryOptions.add(new SelectOption('Germany','Germany'));
        countryOptions.add(new SelectOption('Ireland','Ireland'));

        return countryOptions;
    }
    
    public list<selectOption> getgenderdetail(){
       list<selectOption> selectgender = new list<selectOption>();
       selectgender.add(new selectOption('Female','Female'));
       selectgender.add(new selectOption('Male','Male'));
       selectgender.add(new selectOption('Transgender','Transgender'));
       
       return selectgender; 
    }
    
    public list<selectOption> getcheckboxes(){
      
       list<selectOption> checkbox = new list<selectOption>();
       checkbox.add(new selectOption('Bihar','Bihar'));
       checkbox.add(new selectOption('Arrah','Arrah'));
       checkbox.add(new selectOption('Bhojpur','Bhojpur'));
    
       return checkbox ;
    }
    public PageReference save(){
        return null;
    }
}

2.   Create Visualforce Page for UI :

<apex:page controller="customPickListInVFDemoController" tabStyle="opportunity" >
<div style="background-color:pink">
   <marquee><apex:image url="{!$Resource.MyImage}">Randhir Singh</apex:image></marquee>
</div>
  <apex:form style="color:pink">
    <apex:pageBlock title="Custom PickList Demo" id="out">
        <apex:pageBlockButtons >
            <apex:commandButton value="Save" action="{!save}" rerender="out" status="actStatusId"/>
            
            <apex:actionStatus id="actStatusId">
                <apex:facet name="start">
                    <img src="/img/loading.gif" />
                </apex:facet>
            </apex:actionStatus>
            
        </apex:pageBlockButtons>
        
        <apex:pageBlockSection title="Custom Picklist Using selectList and selectOption" collapsible="false">
            
            <apex:selectList value="{!selectedCountry1}" multiselect="false" size="1">
                <apex:selectOption itemValue="None" itemLabel="--None--"/>
                <apex:selectOption itemValue="INDIA" itemLabel="India"/>
                <apex:selectOption itemValue="USA" itemLabel="USA"/>
                <apex:selectOption itemValue="JP" itemLabel="Japan"/>
                <apex:selectOption itemValue="United Kingdom" itemLabel="UK"/>
            </apex:selectList>
             
            <apex:outputText value="{!selectedCountry1}" label="You have selected Country:"/>
        </apex:pageBlockSection>
         
        <apex:pageBlockSection title="Custom Picklist Using selectList and selectOptions" collapsible="false">
           
            <apex:selectList value="{!selectedCountry2}" multiselect="false" size="1">
                <apex:selectOptions value="{!countriesOptions}"/>
            </apex:selectList>
             
            <apex:outputText value="{!selectedCountry2}" label="You have selected nationality:"/>
        </apex:pageBlockSection>
        <apex:pageblockSection title="Custom Radio Using in Visualforce Page">
           <apex:selectRadio value="{!selectedgender}">
              <apex:selectOptions value="{!genderdetail}"/>
          </apex:selectRadio>
          <apex:outputText value="{!selectedgender}" label="You have selected gender :"/>
        </apex:pageblockSection>
        
        <apex:pageBlockSection title="Custom CheckBox using in Visualforce Page">
           <apex:selectCheckboxes value="{!selectCheckBoxesItem}">
               <apex:selectOptions value="{!checkboxes}"/>
           </apex:selectCheckboxes>
           <apex:outputText value="{!selectCheckBoxesItem}" label="You have selected"/>
        </apex:pageBlockSection>
        
        <apex:pageblockSection title="Custom InputText using in Visualforce Page">
           <apex:inputText value="{!selectedInputText}">First Name : &nbsp;&nbsp;</apex:inputText>
           <apex:outputText value="{!selectedInputText}" label="You have selected First name :"/>
           <apex:inputtext value="{!selectedInputName}" >Last Name : &nbsp;&nbsp;</apex:inputtext>
           <apex:outputText value="{!selectedInputName}" label="You have selected Last name :"/>
           <apex:inputText value="{!selectedInputEmail}" >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;E-Mail : &nbsp;&nbsp;</apex:inputtext>
           <apex:outputText value="{!selectedInputEmail}" label="You have selected E-mail :"/>
           
         <!-- &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Email : <input type="email" title="Enter valid E-mail"/>
           <input type="submit"/><br/><br/>-->
           
       <!--   E-Mail : <input id="email" type="text" class="validate" value="{!selectedInputEmailaddress}"/>
           <label for="email"/>
           <input type="submit"/>-->
           
        </apex:pageblockSection>
    </apex:pageblock>
  </apex:form>
</apex:page>

Output : You can see the following screen screen once you complete above steps.

Thursday, September 8, 2016

Salesforce Sandbox Overview


Today I am going to post info about Salesforce Sandbox :)
                                                           
What is Sandbox?
=> Sandboxes are special organizations that are used to test changes or new apps without risking damage to your production data or configuration.

=> Sandboxes are mainly used for Development, Testing or User Training purpose.

There are four types of Sandboxes:
1. Developer Sandbox
2. Developer Pro Sandbox
3. Partial Copy Sandbox
4. Full Copy

I have attached below the screen shot where you can find more details and Limit about Sandboxes.



For more details Click Here

Wednesday, September 7, 2016

Form Validation using Custom Controller(Server Side Validation)

I have used a custom object for validation. You need to do the changes based on the object and Field API Name.


Setp 1: Create a Custom Object: Stationary
Step 2: Create a Apex Class
Navigation: Setup => Develop => Apex Classes
Class Name: CustomStationaryController 

public class CustomStationaryController {

  public stationary__c stat{get;set;}
  public boolean isEditPage{get;set;}
  public CustomStationaryController(){
    stat = new stationary__c();
    isEditPage = true;
  }
  
  public PageReference EnableEdit(){
     isEditPage = true;
     return null;
  }
  
  public pageReference SaveStationary(){
     
     if(validateStationaryValuesCombinedMSG()){
        if(stat.id == null)
           insert stat;
        else
           update stat;
           
        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Stationary item created succesfully.'));
        isEditPage = false;
     }
     
     return null;
  }
  
  private boolean validateStationaryValues(){
     boolean isValidationPass = true;
     
     //To validate Name Field
     if(String.isBlank(stat.name)){
        isValidationPass = false;
        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'Please Enter the Stationary Name.'));
     }
     
     //To validate Item Field
     if(String.isBlank(stat.Item__c)){
        isValidationPass = false;
        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'Please Select the Stationary Item.'));
     }
     //To validate Quantity Field
     if(stat.Quantity__c == null){
        isValidationPass = false;
        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'Please Enter the Stationary Quantity.'));
     }
     //To validate List Price Field
     if(stat.List_Price__c == null){
        isValidationPass = false;
        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'Please Enter the Stationary item  List Price.'));
     }
     //To validate Selling Price Field
     if(stat.Selling_Price__c == null){
        isValidationPass = false;
        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'Please Enter the Stationary item  Selling Price.'));
     }
     //To validate Discount Price Field
     if(stat.Discount_Price__c == null){
        isValidationPass = false;
        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'Please Enter the Stationary item Discount.'));
     }
     
     return isValidationPass;
  }
  
  private boolean validateStationaryValuesCombinedMSG(){
     boolean isValidationPass = true;
     
     //To validate Name Field
     if(String.isBlank(stat.name) || String.isBlank(stat.Item__c) || stat.Quantity__c == null || stat.List_Price__c == null || stat.Selling_Price__c == null || stat.Discount_Price__c == null){
        isValidationPass = false;
        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'Please enter the value, These fields are required(Stationary Name, Item Name, Quantity, List Price, Selling Price and Discount Price).'));
     }
     return isValidationPass;
  }

}

Step3: Create Visualforce page
Page Name: CustomStationaryPage

<apex:page controller="CustomStationaryController" tabStyle="Stationary__c">
 <apex:sectionHeader title="Stationarys" subtitle="Home"/>
  <apex:form >
    <apex:pageBlock title="Stationary Form" rendered="{!isEditPage}">
       <apex:pagemessages ></apex:pagemessages>
       <apex:pageBlockButtons >
          <apex:commandButton value="Save" action="{!SaveStationary}"/>
       </apex:pageBlockButtons>
    
       <apex:pageblockSection title="Information" columns="1">
          <apex:inputField value="{!stat.name}"/>
          <apex:inputField value="{!stat.Item__c}"/>
          <apex:inputField value="{!stat.Quantity__c}"/>
          <apex:inputField value="{!stat.List_Price__c}"/>
          <apex:inputField value="{!stat.Selling_Price__c}"/>
          <apex:inputfield value="{!stat.Discount_Price__c }"/>
       </apex:pageblockSection>
    </apex:pageBlock>
    
    <apex:pageBlock title="Stationary Form" id="detailPage" rendered="{!!isEditPage}">
       <apex:pageBlockButtons >
          <apex:commandButton value="Edit" action="{!EnableEdit}"/>
       </apex:pageBlockButtons>
    
       <apex:pageblockSection title="Information" columns="1">
          <apex:outputField value="{!stat.name}"/>
          <apex:outputField value="{!stat.Item__c}"/>
          <apex:outputField value="{!stat.Quantity__c}"/>
          <apex:outputField value="{!stat.List_Price__c}"/>
          <apex:outputField value="{!stat.Selling_Price__c}"/>
          <apex:outputField value="{!stat.Discount_Price__c }"/>
       </apex:pageblockSection>
    </apex:pageBlock>  
  </apex:form>
</apex:page>

Output: You can see the Edit page on page load. Once validation pass and record inserted, edit page disable and automatically Stationary detail page will appear with Edit button. 

Once Click on Edit button, we navigate to edit page where you can change the value and update the stationary existing record and after save go to detail page.

User will see error message on the page after click on Save button without entering required field's value.

                                                                              [Edit Page with Error Message]
[Edit Page]


[Detail Page]


Wednesday, August 31, 2016

Show Status message on Page using apex:actionStatus component.

<apex:page >
  <apex:form id="frmId">
     <apex:pageBlock >
        <apex:commandButton value="Save!" status="statusid" reRender="frmId"/>
       
<apex:actionstatus id="statusid">
            <apex:facet name="start">
                <div class="waitingSearchDiv" id="el_loading" style="background-color: #fbfbfb;height: 100%;opacity:0.65;width:100%;"> 
                    <div class="waitingHolder" style="top: 74.2px; width: 91px;">
                        <img class="waitingImage" src="/img/loading.gif" title="Please Wait..." />
                        <span class="waitingDescription">Please Wait...</span>
                    </div>
                </div>
            </apex:facet>
        </apex:actionstatus>

     </apex:pageBlock>
  </apex:form>
</apex:page>

apex:actionStatus visualforce component is used to show message on  the page when server is busy.

It stop the status message once request completed.

Show message when server is busy.

Please Wait...