Tag Archives: Apex

Convert String To Lowercase And Uppercase In Salesforce Apex

Salesforce provides a number of methods to manipulate strings in apex. You can actually play with strings in apex using these methods. Lets see how we can convert a string to a upper case and lower case in the example below.

toLowerCase(): method is to convert all of the characters in the String to lowercase.

String name = 'Biswajeet Samal';
System.debug('Lowercase-' + name.toLowercase());

toUpperCase(): method is to convert all of the characters in the String to uppercase.

String name = 'Biswajeet Samal';
System.debug('Uppercase-' + name.toUppercase());

Find All Users Below a Role in Role Hierarchy

Sample Code:

public with sharing class Utility {
    
    //Get all sub roles.
    public static Set<ID> getRoleSubordinateUsers(Id userId) {
        
        //Get requested user's role Id
        Id roleId = [SELECT UserRoleId From User Where Id = :userId].UserRoleId;
        //Get all of the roles below the user
        Set<Id> allSubRoleIds = getAllSubRoleIds(New Set<ID>{roleId});
        // get all of the ids for the users in those roles
        Map<Id, User> users = new Map<Id, User>([SELECT Id, Name From User Where UserRoleId IN :allSubRoleIds]);
        //Return the ids as a set
        return users.keySet();
    }
    
    //Get all Parent Roles.
    private static Set<ID> getAllSubRoleIds(Set<ID> roleIds) {
        
        Set<ID> currentRoleIds = new Set<ID>();
        
        //Get all of the roles below the passed roles
        for(UserRole userRole :[SELECT Id from UserRole Where ParentRoleId IN :roleIds AND ParentRoleID != null]){
            currentRoleIds.add(userRole.Id);
        }
        
        //Fetch more rolls
        if(currentRoleIds.size() > 0){
            currentRoleIds.addAll(getAllSubRoleIds(currentRoleIds));
        }        
        return currentRoleIds;   
    }
}

Call the getRoleSubordinateUsers method of the above class by passing User Id in it.

Id userId = UserInfo.getUserId();
Set<Id> allUsersId = Utility.getRoleSubordinateUsers(userId);

Delete Records Permanently from Recycle Bin in Salesforce

In Salesforce when we delete any record from an object, it will not be deleted permanently and it will be stored in Recycle Bin till 15 days from the deletion date. The number of records stored in recycle bin depends on Number of User Licenses of the Organisation has pursed and multiply it with 5000 records.

If the number of records in recycle bin crosses this limit Salesforce automatically deletes the old records from Recycle Bin, which are at least in recycle bin for 2 hours. If you want to delete the records permanently from recycle bin before Salesforce deletes it you can use emptyRecylebin() method.

//To permanently delete the specified records from the recycle bin.
Database.emptyRecylebin(List<Id> ids)
//To permanently delete the specified sObject from the recycle bin.
Database.emptyRecylebin(sObject sObj)
//To permanently delete the specified list of sObject from the recycle bin.
Database.emptyRecylebin(List<sObject> sObjList)

Example:

Account acc = new Account(Id='00128000002KuXO');
Database.emptyRecycleBin(acc);

Notes:

  • Maximum number of records per call that we can pass is 200.
  • Using this function records deleted can not be undeleted again.
  • Theses deleted records from recycle bin can be accessed using the DataBase.queryAll() for some time typically that would be with in 24 hours or may shorter or longer.

Retrieve Child Record From Parent Record in Salesforce

In below example “Student” is the custom Master object, and “Project” is the custom Child object.

Controller:

public class SampleController
{
    //Contact List Variable
    public List<Student__c> stuList {get;set;}
    
    //Constructor
    public SampleController(){
        stuList = [SELECT Id, Name, (SELECT Id, Name FROM Projects__r) FROM Student__c LIMIT 10];
    }    
}

Visualforce Page:

<apex:page controller="SampleController">
    <table>
        <apex:repeat value="{!stuList}" var="stu">
            <tr>
                <td><apex:outputText value="{!stu.Name}"/></td>
                <apex:repeat value="{!stu.Projects__r}" var="pro">
                    <td><apex:outputText value="{!pro.Name}"/></td>
                </apex:repeat>
            </tr>
        </apex:repeat>
    </table>
</apex:page>