Tag Archives: Apex

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:

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

Only in batch class finish method, We can call another batch class. If you will call another batch class from batch class execute and start method, then Salesforce will throw below runtime error.

 
System.AsyncException: Database.executeBatch cannot be called from a batch start, batch execute, or future method.

Here in below example there are two batch classes “Batch1” and “Batch2“.
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){

	}
}

Salesforce Named Credentials

  • Salesforce introduced Named Credentials in the Spring’15 release.
  • Named credential specifies the URL of a callout endpoint and its required authentication parameters in one definition.
  • No need to handle Remote site settings of the Named credential callout URL.
  • Named credentials separate the URL from the authentication, making it easier to make changes to both the endpoint URL and authentication if needed.
  • It supports Basic Password authentication and OAuth 2.0 authentication protocols.
  • You can configured Named credentials to use an org-wide named principal or to use per-user authentication so that users can manage their own credentials.

Apex HTTP Callout Without Named Credential:

String username = 'username';
String password = 'password';
String endpoint = 'https://testapi.com';

Http http = new Http();
HttpRequest req = new HttpRequest();
req.setMethod('POST');
req.setEndpoint(endpoint);

//Add basic authentication header to the callout
Blob headerValue = Blob.valueOf(username + ':' + password); 
String authHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue); 
req.setHeader('Authorization', authHeader);

HttpResponse response = http.send(req);
System.debug('response-' + response);

Example:
Setup | Quick Find – Search Named Credentials | New Named Credential

Create Named Credential as per your requirement:

Apex HTTP Callout With Named Credential:

Http http = new Http();
HttpRequest req = new HttpRequest();

req.setEndpoint('callout:Sample_API/some_path');
req.setMethod('POST');

HTTPResponse response = http.send(req);
System.debug('response-' + response);