Category Archives: Salesforce

Get Salesforce Base URL in Visualforce Page

Apex Class:

public class SampleBaseURL{

    public List<Account> accList {get;set;}
    
    public SampleBaseURL(){
        accList = [SELECT Id, Name FROM Account LIMIT 100];
    }
}

Visualforce Page:

<apex:page controller="SampleBaseURL" sidebar="false">
    <apex:repeat value="{!accList}" var="a">
        <a href="{!$Site.BaseUrl}/{!a.Id}" target="_blank">{!a.Name}</a>
    </apex:repeat>
</apex:page>

Output:

Enable Organization Admins to Login as Any User

Go to Administer | Security Controls | Login Access Policies | Select Administrators Can Log in as Any User

With this feature enabled, System Administrators can log in as any user in their organization without asking internal end-users to grant login access.

Note: This feature is available in Enterprise, Performance, Unlimited and Developer Editions. If this feature is not found in your Org, contact Salesforce.com support to enable.

How to create a report that shows user license?

To display Salesforce as a User License type on report, Below is the workaround:

  • Create a Custom Formula Text field under the User object:
    1. Go to Setup | Customize | Users | Fields
    2. Click on New and select “Formula” field type and then “Text”
    3. Use “Profile.UserLicense.Name” in the formula editor
    4. Click on Save
  • Create a New Administrative User report:
    1. Go to the Report tab
    2. Click on New report
    3. Under the Administrative reports | Select Users
    4. Add the New Custom Formula field in the report
    5. Run the Report and Save

SOQL Query To Get Users with Salesforce User License

Get all Users with User License:

List<User> userList = [Select Id, Name, Profile.UserLicense.Name From User];

Get Users with specific User License:

List<User> userList = [Select Id, Name, Profile.UserLicense.Name From User WHERE Profile.UserLicense.Name = 'Salesforce'];

Invoke Batch Apex From Another Batch Apex

In batch class “Execute” method being called many times but “Start” and “Finish” method only once. So, To call another batch class from a batch class, we can call another batch class either in start method or in finish method as per our requirement.

Here in below example there are two batch classes “Batch1” and “Batch2“.
As per my requirement, I want to execute “Batch2” after finish the “Batch1“.
So, I’m calling “Batch2” class in “Batch1” finish method.

Batch1:

global class Batch1 implements Database.Batchable<Sobject>{

	//Method to get the data to be proceesed  
	global database.Querylocator Start(Database.BatchableContext bc){
		String query = 'Select Id, Name From Account Limit 1000';
		return Database.getQueryLocator(query);
	}


	//Method to execute the batch
	global void execute(Database.BatchableContext bc, Sobject[] scope){
		for(Sobject s : scope){ 
		Account a = (Account)s;
			// TO DO
			// add your logic 
		}
	}

	//Method to be called after the excute
	global void finish(Database.BatchableContext bc){
		//Add your start code for the other batch job here
		Database.executeBatch(new Batch2());
	}
}

Batch2:

global class Batch2 implements Database.Batchable<Sobject>{

	//Method to get the data to be proceesed  
	global database.Querylocator start(Database.BatchableContext bc){
		string query = 'Select Id, Name From Contact Limit 1000';
		return Database.getQueryLocator(query);
	}


	//Method to execute the batch
	global void execute(Database.BatchableContext bc, Sobject[] scope){
		for(Sobject s : scope){ 
		Contact c = (Contact)s;
			// TO DO
			// add your logic 
		}
	}

	//Method to be called after the excute
	global void finish(Database.BatchableContext bc){

	}
}