Tag Archives: Apex

Accessing keys from a map in a SOQL Query

Using .KeySet() on a map, we will get a list of all the keys returned.

Map<Id, Master_Obj__c> masterMap = new Map<Id, Master_Obj__c>([SELECT Id, Name FROM Master_Obj__c]);
List<Detail_Obj__c> objDetailList = [Select Id, Name FROM Detail_Obj__c WHERE Master_Obj__c IN :masterMap.KeySet()]

How to cover pagereference method in test class

For Custom Controller


//Page reference to your VF Page
PageReference pageRef = Page.TestPage;
Test.setCurrentPage(pageRef);

//Pass necessary parameter
pageRef.getParameters().put('Id',id); 

//init controller 
CustomCtrl objCtrl = new CustomCtrl();

//Call pageRef mymethod
PageReference objPageRef = objCtrl.mymethod();

//Put system asserts
System.assertEquals (null,pageRef);

For Standard Controller

//First create record
Account acc = New Account();
acc.Name = 'Test Account';
INSERT acc;

//Page reference to your VF Page
PageReference pageRef = Page.TestPage;
Test.setCurrentPage(pageRef);

//Pass necessary parameter
pageRef.getParameters().put('Id',acc.id);   

//Pass your object to controller     
ApexPages.StandardController stc = new ApexPages.StandardController(acc);

//Call controller
CustomCtrl objCtrl = new CustomCtrl(stc);

//Call pageRef mymethod
PageReference objPageRef = objCtrl.mymethod();

//Put system asserts
System.assertEquals (null,pageRef);

How to open a new Tab using PageReference in apex class?

We can use apex:commandLink to redirect a visualforce page in a new Tab URL using PageReference in apex class.

Sample Code:

VF Page:

<apex:page controller="SampleleRedirect">
	<apex:form >
		<apex:pageblock >
			<apex:commandlink action="{!redirect}" target="_blank">
				<apex:commandButton value="Open in New Tab"/>
			</apex:commandLink>
		</apex:pageblock>
	</apex:form>
</apex:page> 

Apex Controller:

public class SampleleRedirect {   

	public SampleleRedirect() {
	
	}

	public pageReference redirect() {
		PageReference pageRef = new PageReference('http://www.biswajeetsamal.com');
		pageRef.setRedirect(true);
		return pageRef;
	}            
}

Synchronous and Asynchronous call in Salesforce

Synchronous:
In Synchronous process the thread waits for the task to be completed and then moves to the next task Sequentially. All the tasks are completed in a single thread.

Example:
Trigger
Controller Extension
Custom Controller

Asynchronous:
In Asynchronous call, the thread will not wait until it completes its tasks before proceeding to next. In a Asynchronous call, the tasks are run in different threads all together.

Example:
Batch
@future Annotation