Tag Archives: Apex

How to redirect to the same VF page from the Constructor with parameters in Salesforce?

Sample Code:

Apex Class:

public with sharing class SampleClass{

    public SampleClass(){
    
    }
    public PageReference doRedirect(){
        String param = ApexPages.currentPage().getParameters().get('UserId');
        if(param == null || param == ''){
            String userId = UserInfo.getUserId();
            PageReference pageRef = new PageReference('/apex/TestPage1');
            pageRef.getParameters().put('UserId',userId);
            pageRef.setRedirect(true);
            return pageRef;
        }
        else{
            return null;
        }
    }
}

VF Page:

<apex:page controller="SampleClass" action="{!doRedirect}">
	<h1>Congratulations</h1>
	This is your new Page
</apex:page>

How to redirect to a different VF page from the constructor with parameters in Salesforce?

Sample Code:

Apex Class:

public with sharing class SampleClass{

    public SampleClass(){
    
    }
    public PageReference doRedirect(){
        String userId = UserInfo.getUserId();
        PageReference pageRef = new PageReference('/apex/TestPage2');
        pageRef.getParameters().put('UserId',userId);
        pageRef.setRedirect(true);
        return pageRef;
    }
}

VF Page:

<apex:page controller="SampleClass" action="{!doRedirect}">
	<h1>Congratulations</h1>
	This is your new Page
</apex:page>

Salesforce: Create Test Data for Group and GroupMember Objects in Test Class

Sample Test Class:

@isTest
global class SampleTestClass
{
	public static TestMethod void TestSampleMethod()
	{
		Test.startTest();     

		//Create Parent Group
		Group grp = new Group();
		grp.name = 'Test Group1';
		grp.Type = 'Regular'; 
		Insert grp; 

		//Create Group Member
		GroupMember grpMem1 = new GroupMember();
		grpMem1.UserOrGroupId = UserInfo.getUserId();
		grpMem1.GroupId = grp.Id;
		Insert grpMem1;

		//Create Sub group
		Group subGrp  = new Group();
		subGrp.name = 'Test Group2';
		subGrp.Type = 'Regular'; 
		Insert subGrp; 

		//Assign Role To Parent Group
		GroupMember grpMem2 = new GroupMember();
		grpMem2.UserOrGroupId = subGrp.Id;
		grpMem2.GroupId = grp.Id;
		Insert grpMem2;  

		//Create Group Map Data
		Map<String,Id> groupNameRoleIdMap = new Map<String,Id>();
		for(Group gp: [SELECT Id, Name FROM Group WHERE Type = 'Role']){
			groupNameRoleIdMap.put('SuperAdmin', gp.Id);
		}     

		//Assign role to parent group
		GroupMember grpMem3 = new GroupMember();
		grpMem3.UserOrGroupId = groupNameRoleIdMap.get('SuperAdmin');
		grpMem3.GroupId = grp.Id;
		Insert grpMem3; 

		//Create Group And Sub Group Map Data
		Map<String,Id> groupNameRoleAndSubordinatesIdMap = new Map<String,Id>();
		for(Group gp: [SELECT Id, Name FROM Group WHERE Type = 'RoleAndSubordinates']){
			groupNameRoleAndSubordinatesIdMap.put('Admin', gp.Id);
		} 

		//Assign Role To Parent Group
		GroupMember grpMem4 = new GroupMember();
		grpMem4.UserOrGroupId = groupNameRoleAndSubordinatesIdMap.get('Admin');
		grpMem4.GroupId = grp.Id;
		Insert grpMem4;             
		
		//Execute your method here with Group and Group Member Test data
		
		Test.stopTest();
	}
}

How to add a list of records to a map in Salesforce?

Normally we used to do something like this:

Map<Id,Contact> mapContact =  new Map<Id,Contact>();
public static void addListToMap(List<Contact> conList){
	for(Contact con:conList){
		mapContact.put(con.id,con);
	}
}

Here the processing time will be more(CPU execution time will be more), because we are iterating over a for loop to construct a map.

The best way to construct the map where we can avoid iteration like this:

public static void addListToMap(List<Contact> conList){
	Map<Id,Contact> mapContact =  new Map<Id,Contact>(conList);
}

Here the processing time will be very less.