Tag Archives: Apex

Displaying records based on the picklist values

Here is the example of Opportunity object, based on Stage Picklist values.

Apex Class:

public class SearchOpportunity {
    public Opportunity opp  {get;set;}
    public List<Opportunity> OpportunityList  {get;set;}
    
    public SearchOpportunity () {
        opp = new Opportunity();
    }
    
    public PageReference doSearch() {
        OpportunityList =[Select Id, Name, StageName FROM Opportunity WHERE StageName =: Opp.StageName];
        return null;
    }
}

Visualforce Page:

<apex:page controller="SearchOpportunity">
    <apex:form>
        <apex:pageBlock Title="Search Opportunities">
            <b>Stage :</b>
            <apex:inputField value="{!opp.StageName}" />
            <apex:commandButton value="Search" action="{!doSearch}"/>
        </apex:pageBlock>
        
        <apex:pageBlock title="List of Opportunities">
            <apex:pageBlockSection columns="1">
                <apex:pageBlockTable value="{!OpportunityList}" var="item">
                    <apex:column value="{!item.Name}" headerValue="Name"/>
                    <apex:column value="{!item.StageName}" headerValue="Stage"/>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Output:

SearchOpportunities

Primitive Data Types in Salesforce Apex

Apex uses the same primitive data types as the SOAP API. All primitive data types are passed by value, not by reference.

All Apex variables, whether they’re class member variables or method variables, are initialized to null. Make sure that you initialize your variables to appropriate values before using them. For example, initialize a Boolean variable to false.

  • Integer: A 32-bit number that does not include decimal point. Integers have a minimum value of -2, 147,483648 and maximum value of 2,147,483648.
    For example:

     Integer i = 1; 
  • Long: A 64 bit number that doesn’t includes a decimal point. Long has a minimum value of -2^63 and a maximum value of 2^63-1.
    For example:

    Long l = 2147483648L;
  • Double: A 64 bit number that doesn’t includes a decimal point. Long has a minimum value of -2^63 and a maximum value of 2^63-1.
    For example:

     Double d = 3.14159; 
  • Decimal: A number that includes a decimal point. Decimal is an arbitrary precision number. Currency fields are automatically assigned the type decimal.
    For example:

     Double d = 256.32; 
  • String: Strings are set of characters and are enclosed in single quotes. They store the text values such as name or an address.
    For example:

     String str = 'Biswajeet Samal'; 
  • Date: A value that indicates a particular day. Date values contain no information about time. Date values must always be created with system static method.
    For example:

     Date myDate = Date.newinstance(2015, 09, 14);
    Output: 2015-09-14       00:00:00
    
  • Time: A value that indicates a particular time. Time values must always be created with a system static method.
    For example:

    Time tm = newInstance(10,12,5,11);
    Output: 10:12:05
    
  • Date Time: These are data types associated with dates and times along with Date data type. The time data type stores times (hours, minutes, second and milliseconds). The Date data types stores dates (Year month and day). The time data type stores both dates and times.
    Each of these classes has a newInstance method with which we can construct particular date time values.
    For example:

    Date dt = Datetime.now();
    
  • Id: Any valid 18-character Force.com record identifier.
    For example:

    ID id='00910000004T2AGAA0';
    

    If you set ID to a 15-character value, Apex converts the value to its 18-character representation. All invalid ID values are rejected with a runtime exception.

  • Boolean: A value that can only be assigned true, false, or null.
    For example:

    Boolean isValid = true;
    
  • Blob: A value that can only be assigned true, false, or null.
    For example:

    It stores files data in binary format.
    

Schedule Batch Apex in Salesforce

Batch Class:

global class accountBatch implements Database.Batchable<sobject> {
  
    global Database.QueryLocator start(Database.BatchableContext bc){
      
        String query = 'SELECT Id, Name FROM Account';
        return Database.getQueryLocator(query);
    }
      
    global void execute(Database.BatchableContext bc, List<account> scope) {
      
        for(Account a : scope) {
            a.Name = a.Name + 'Updated';
        }
        update scope;
    } 
      
    global void finish(Database.BatchableContext bc) {
      
    }
}

Scheduled Class:

global class accountBatchSchedule implements Schedulable{
	global void execute(SchedulableContext sc) {
		//invoke the batch class
        Database.executeBatch(new accountBatch());
    }
}

There are 2 ways to schedule an apex job:

  • Scheduling a Job from the UI
  • Using the System.Schedule Method

Go to Setup | Apex Class | Click Schedule Apex

  • Enter Job name and select Apex Scheduled Schedulable.
  • Select the Schedulable frequency Weekly or Monthly as per your requirement.
  • Select the start and end dates.
  • Select preferred start time.
  • Click on Save.

LIKE Clause In Salesforce Dynamic SOQL Query

Sample Code:

string conName = 'biswa';
string conNameLike = '%' + String.escapeSingleQuotes(conName.trim()) + '%';
String query = 'SELECT Id, FirstName, LastName From Contact Where Name LIKE :conNameLike';
List<Contact> conList = Database.query(query);
System.debug('conList-' + conList);