Play Games

Search This Blog

Friday, November 27, 2020

How to create a map of Group and GroupMembers apex salesforce - Salesforce Globe For You

 How to create a map of Group and GroupMembers apex salesforce  - Salesforce Globe For You 

Run the following piece of code in anonymous window.

Map<Group,List<GroupMember>> mapGroupGroupMembers = new Map<Group,List<GroupMember>>();

for(Group objGroup : [Select id,name,(select id,UserOrGroupId from GroupMembers) from Group]) {

    List<GroupMember> lstGroupMembers = new List<GroupMember>();

    if(!objGroup.GroupMembers.isEmpty()) {

        for(GroupMember objMember : objGroup.GroupMembers) {

        lstGroupMembers.add(objMember);    

        }

        mapGroupGroupMembers.put(objGroup,lstGroupMembers);

    } else {

        mapGroupGroupMembers.put(objGroup,lstGroupMembers);

    }

}

system.debug('mapGroupGroupMembers:'+mapGroupGroupMembers);

Output:



Thursday, November 26, 2020

is current year a leap year? apex salesforce - Salesforce Globe For You

 is current year a leap year? apex salesforce  - Salesforce Globe For You 

Solution: use isLeapYear() of Date to know if the given year is leap year or not.

Example: To check current year is leap year or not.

Run the piece of code below in anonymous window.

String year = system.today().year();

system.debug('Is current Year leap year:'+Date.isLeapYear(year));

output:



Tuesday, November 17, 2020

get all the child objects of a parent object salesforce - Salesforce Globe For You

 get all the child objects of a parent object salesforce  - Salesforce Globe For You 

In this example,all the child objects of opportunity object are fetched.

Apex Class:OpportunityChildObjectController

public class OpportunityChildObjectController {

    public list<String> lstOppChildObjects {get;set;}

    public void getChildObjectsOfOpp() {

        lstOppChildObjects = new List<String>();

        Schema.DescribeSObjectResult objSchemaResult = Opportunity.SObjectType.getDescribe();

        for (Schema.ChildRelationship objChildRelationship : objSchemaResult.getChildRelationships()) {

            lstOppChildObjects.add(string.valueOf(objChildRelationship.getChildSObject()));

        }

        system.debug('Opportunity Related Child Objects are:'+lstOppChildObjects);

    }

}

Visualforce Page:OpportunityChildObjects

<apex:page controller="OpportunityChildObjectController" action="{!getChildObjectsOfOpp}">

    <b>Opportunity Child Objects are:</b><br/>

    <apex:variable var="index" value="{!1}" />

    <apex:repeat value="{!lstOppChildObjects}" var="objName" id="OppRelatedChildObjects">

        {!index}) <apex:outputText value="{!objName}" id="theValue"/><br/>

        <apex:variable var="index" value="{!index + 1}" />

    </apex:repeat>

</apex:page>

Output:



SOQL to get the public groupId based on the group name salesforce - Salesforce Globe For You

 SOQL to get the public groupId based on the group name salesforce  - Salesforce Globe For You 

Solution:use the following query

select id,name,type from group where group.Name ='Test Public Group'

where Name is the name of the group.

Note: Use the label of the group for the group name.

Output:


Tuesday, November 3, 2020

Aggregate Query to get the minimum date of all the child records associated with a parent record in salesforce - Salesforce Globe For You

 Aggregate Query to get the minimum date of all the child records(opportunities) associated with a parent record(Account) in salesforce  - Salesforce Globe For You 

Requirement: I need to get the minimum clode date of all the opportunities associated to a Account Record.

Solution:

List<AggregateResult> lstAggregateResult = new List<AggregateResult>();

lstAggregateResult = [Select accountId,Min(closeDate) minDate from opportunity where accountId != null group by accountId];

for(AggregateResult objResult:lstAggregateResult) {

    system.debug('Account Id: '+objResult.get('accountId')+' and its associated opportunity min date is:'+objResult.get('minDate'));

}

Output: