Search This Blog

Wednesday, March 29, 2017

How to shuffle the numbers in Apex Salesforce.

Thanks Shiva Chitta for providing this solution.
Requirement:We need to shuffle the numbers from 0 to 9 .Every time we run the page,different sequence of numbers to be displayed.
Solution:
Apex Class:
public class ShuffleNumbers {
    public Set<Integer> alreadyUsed {get;set;}
    public List<Integer> shuffledNumbersByCh {get;set;}
    public void shuffleNumbers() {
        //set the range of numbers from to N
        Integer lsitSize = 10;
        alreadyUsed= new Set<Integer>();
        shuffledNumbersByCh = new List<Integer>();
        while(shuffledNumbersByCh .size() < lsitSize){
            shuffledNumbersByCh .add(getRandom(lsitSize));
        }
        system.debug('Final shuffled Numbers'+shuffledNumbersByCh );
       
    }
    public Integer getRandom(Integer shuffledMaxNumber){
        Integer randomNum;
        do {
            randomNum = (math.random() * shuffledMaxNumber).intValue();
        } while (alreadyUsed.contains(randomNum));
        alreadyUsed.add(randomNum);
        return randomNum;
    }
}
Visualforce Page:
<apex:page controller="ShuffleNumbers" action="{!shuffleNumbers}">
The shuffled Numbers are :{!shuffledNumbersByCh}
</apex:page>
Note :When ever you re-run the page,difference sequence of numbers is given as output.
Output:

Friday, March 24, 2017

Using Like in SOQL Query

Example 1: To find all the accounts which contain "s" in their names using LIKE in SOQL Query:
Sample Code:
String querySrt = 'Select id,name from Account where name !=null and name Like  \'%s%\'';
List<Account> lstAccount = Database.query(querySrt);
system.debug('List of Accounts'+lstAccount);

Example 2: To find all the accounts which contain "s" in their names using LIKE in SOQL Query:
Sample Code:
String querySrt = 'Select id,name from Account where name !=null and name Like  \'%b%\'';
List<Account> lstAccount = Database.query(querySrt);
system.debug('List of Accounts'+lstAccount);


Example 3: To find all the accounts which contain "s" OR "Q" OR "M" in their names using LIKE in SOQL Query:
Sample Code:
Set<String> setAccountNames = new Set<String>();
String querySrt = 'Select id,name from Account where name !=null ';
setAccountNames.add('%'+'m'+'%');
setAccountNames.add('%'+'s'+'%');
setAccountNames.add('%'+'q'+'%');
querySrt = querySrt + ' and name Like :setAccountNames';
List<Account> lstAccount = Database.query(querySrt);
system.debug('List of Accounts'+lstAccount);

Friday, March 3, 2017

Search With anyfield in Lookup dialog box

Problem: For Example, In opportunity record, we have lookup to Account object.Generally on clicking the lookup,you will be able to search the account with "Account Name" field.If we search with other fields,you won't get any search results as shown in the image
Here I searched with "Phone" field.

Solution:
Step1: Make sure Enhanced Lookups are enabled for that object.In this case Account object.
Go to Setup -> search -->Search Settings --> Lookup Settings --> Enable "Enhanced Lookup" for account object as shown in the image below.-->Save

Step 2:Note, now the lookup dialogue window changed as shown in the image below.

Step 3: Now "Check the All fields" checkbox and search with phone number field.You will be able to search the records as shown in image below.