Tag Archives: Apex

Get Default Picklist Field Value Using Apex

Sample Code:

String defaultLeadStatus = '';
//Get Lead object Status field description
Schema.DescribeFieldResult fieldDesc = Schema.Lead.Status.getDescribe();
//Loop on Status picklist field values
for (Schema.Picklistentry picklistEntry: fieldDesc.getPicklistValues()) {
    //Check if picklist value is default
    if (picklistEntry.isDefaultValue()) {
        defaultLeadStatus = picklistEntry.getValue();
        break;
    }
}

Freeze and UnFreeze Users in Salesforce Using Apex

Sample Code:

//Freeze User
UserLogin ulogin = [SELECT Id, IsFrozen, UserId From UserLogin Where UserId = '00558000003n7iGAAQ'];
ulogin.IsFrozen = true;
Update ulogin;

//UnFreeze User
UserLogin ulogin = [SELECT Id, IsFrozen, UserId From UserLogin Where UserId = '00558000003n7iGAAQ'];
ulogin.IsFrozen = false;
Update ulogin;

Restrict multiple approval for a single user to a single record in Salesforce

I had a requirement to restrict multiple approval for a single user to a single record. There were multiple steps of approval process and the approver may be in different queue or as a manager for a user.

So, I implemented it using a trigger. In this article I’ll demonstrate how to implement it.
Here Application__c is the object, on which 3 steps approval process has implemented.
A custom check box field Check_Approver__c has set value to true, in all 3 steps field update action.

In below trigger I filtered the ProcessInstanceStep object records with current user Id in ActorId and the object Id (here object Id means record Id) in ProcessInstance.TargetObjectId.

  • If an User will approve or reject a record, then the SOQL query will return a single record.
  • If the SOQL query return a record, then trigger will throw an error message.

Trigger source code:

trigger ApplicationTrigger on Application__c (before Update)
{
    if(trigger.isUpdate){

        Id currentUserId = UserInfo.getUserId();
        for(Application__c sf: trigger.new){   
                  
            if(sf.Check_Approver__c == true){
              
                List<processinstancestep> existingApprovals = [SELECT ActorId
                                    FROM ProcessInstanceStep WHERE ProcessInstance.TargetObjectId = :sf.Id
                                    AND (StepStatus = 'Approved' OR StepStatus = 'Rejected')
                                    AND ActorId = :currentUserId];
                                      
                if(existingApprovals != null){
                  
                    if(existingApprovals.size() > 0){

                        sf.addError('You have already approved or rejeted the record.');
                    }
                }                  
             }
         }                         
    }
}

Difference Between Database.query() and Database.getQueryLocator()

Database.query():

  • Database.query() allows to make a dynamic SOQL query at runtime. It returns a single sObject when the SOQL query returns a single record and it returns a list of sObjects when the SOQL query returns more than a single record.
  • We can retrieve up to 50,000 records using Database.query().
  • In Batch Apex, if we use Database.query(), it supports 50,000 records only.
  • If VF page doesn’t have read only attribute, use .
  • Use Database.query() in Visualforce page, if the readOnly attribute is false.

Database.getQueryLocator():

  • Database.getQueryLocator() returns a Query Locator that runs the selected SOQL query returning list that can be iterated over in batch apex and Visualforce page.
  • We can retrieve up to 10,000 records.
  • We can’t use getQueryLocator with any query that contains an aggregate function.
  • Use Database.getQueryLocator() in Visualforce page, if the readOnly attribute is true.
  • In Batch Apex, Database.getQueryLocator() supports up to 50 million records.
  • We can’t use the FOR UPDATE keywords with a getQueryLocator query to lock a set of records, In batch apex the start method automatically locks the set of records in the batch.