Author Archives: Biswajeet

About Biswajeet

Biswajeet is my Name, Success is my Aim and Challenge is my Game. Risk & Riding is my Passion and Hard Work is my Occupation. Love is my Friend, Perfection is my Habit and Smartness is my Style. Smiling is my Hobby, Politeness is my Policy and Confidence is my Power.

SCOPE clasue in SOQL

The new USING SCOPE clause for SOQL queries lets you limit results by filterScope.

SELECT column_name FROM table_name USING SCOPE filterScope_value

filterScope_value can take one of the following enumeration values:

  • Everything – All records, for example All Opportunities.
  • Mine – Records owned by the user running the list view, for example My Opportunities.
  • Queue – Records assigned to a queue.
  • Delegated – Records delegated to another user for action: for example, a delegated task. This option is available in API version 17.0 and later.
  • MyTerritory – Records in the territory of the user seeing the list view. This option is available if territory management is enabled for your organization. This option is available in API version 17.0 and later.
  • MyTeamTerritory – Records in the territory of the team of the user seeing the list view. This option is available if territory management is enabled for your organization. This option is available in API version 17.0 and later.
  • Team – Records assigned to a team. This option is available in API version 17.0 and later.

Here is an example:

SELECT Name FROM Account USING SCOPE Mine

Difference between SOQL and SOSL

SOQL(Salesforce Object Query Language):

  • SOQL is a Salesforce Object Query Language, retrieves the records from the database by using SELECT keyword.
  • SOQL is used if we know in which object the data is present.
  • In SOQL we can query data from single object and as well as multiple objects that are related to each other.
  • We can query on all fields of any datatype.
  • We can use SOQL in Triggers and classes.
  • We can perform DML operation on query results.

SOSL(Salesforce Object Search Language):

  • SOSL is a Salesforce Object Search Language, retrieves the records from the database by using the FIND keyword.
  • SOSL is used if we don’t know in which object the data is present.
  • We can retrieve multiple objects and field values efficiently when the objects may or may not be related to each other.
  • We can query only on fields whose data type is text, phone and Email.
  • We can use in classes but not in Triggers.
  • We cannot perform DML operation on search result.

Check Salesforce sObject is Custom or Standard Type

Check for a specific object:

Schema.DescribeSObjectResult objDescribe = Schema.sObjectType.Account;
System.debug(objDescribe.isCustom());

Check for all objects:

List<String> customObjectList = new List<string>();
List<String> standardObjectList = new List<string>();

for(Schema.SobjectType obj : Schema.getGlobalDescribe().values()) {
    Schema.DescribeSObjectResult objDescribe = obj.getDescribe();
    if(objDescribe.isCustom()) {
        String objName = objDescribe.getName();
        customObjectList.add(objName);
    }
    else {
        String objName = objDescribe.getName();
        standardObjectList.add(objName);
    }
}

System.debug('Custom Object List - ' + customObjectList);
System.debug('Standard Object List - ' + standardObjectList);

Get All Salesforce Users in Role Hierarchy Using Apex

Controller:

Public with sharing class Sample{
    Public List<string> CusrrentUserRole{get;set;}
    Public List<String> RolesInHierarchy{get;set;}
    Public Sample(){
        CusrrentUserRole = New List<string>();
        CusrrentUserRole.add([SELECT UserRoleId FROM User Where Id =:UserInfo.getUserId() LIMIT 1].UserRoleId);
    }
    Public Void getRollInHierarchy(){
        
        RolesInHierarchy = New List<Id>();
        RolesInHierarchy = getAllRoleInHierarchy(CusrrentUserRole);
    }
    
    Public List<string> getAllRoleInHierarchy (List<Id> UserRole) {     
        List<string> currentRoleIds = new List<string>();     
        for(UserRole userRoleName :[SELECT Id,name FROM UserRole Where ParentRoleId  = :UserRole AND ParentRoleID != null]){    
            currentRoleIds.add(userRoleName.id);    
        }    
        if(currentRoleIds.size() > 0) {     
            currentRoleIds.addAll(getAllRoleInHierarchy(currentRoleIds)); 
        }    
        List<String> RoleList = New List<String>();  
        for(UserRole r:[SELECT Name From UserRole Where Id In:currentRoleIds]){
            RoleList.add(r.name);      
        }
        return RoleList;
    }        
}

Visualforce Page:

<apex:page controller="Sample">
 <apex:form>
   <apex:pageBlock>
     <apex:commandButton value="Show Roles below my Hierarchy" action="{!getRollInHierarchy}"/> 
   </apex:pageBlock>
   <apex:pageBlock >
   <b> Role below in Hierarchy are : </b>{!RolesInHierarchy}
   </apex:pageBlock>
  </apex:form>
</apex:page>

Output:

Dynamically Add Rows in Apex PageBlockTable

Controller:

public class Sample{
    
    Account acc = new Account();
    public List<Account> accList { get; set; }
    
    public Sample()
    {
        accList = new List<Account>();
        accList.add(acc);
    }
    
    Public void addAccount()
    {
        Account ac = new Account();
        accList.add(ac);
    }
}

Visualforce Page:

<apex:page Controller="Sample">
    <apex:form>
        <apex:pageBlock id="pbAcc">
            <apex:pageblockSection >
                <apex:pageBlockTable value="{!accList}" var="acc">
                    <apex:column headerValue="Account Name">
                        <apex:inputField value="{!acc.Name}"/>
                    </apex:column>
                    <apex:column headerValue="Account Number">
                        <apex:inputField value="{!acc.AccountNumber}"/>
                    </apex:column>
                    <apex:column headerValue="Account Type">
                        <apex:inputField value="{!acc.Type}"/>
                    </apex:column>
                    <apex:column headerValue="Industry">
                        <apex:inputField value="{!acc.Industry}"/>
                    </apex:column>
                </apex:pageBlockTable>
                <br/><apex:commandLink value="Add Row" action="{!addAccount}" reRender="pbAcc"/>   
            </apex:pageblockSection>        
            <apex:pageblockSection columns="1" >
                <apex:pageblockSectionItem >
                    <apex:commandButton value="Save" />
                    <apex:commandButton value="Cancel" />
                </apex:pageblockSectionItem>         
            </apex:pageblockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Output: