Category Archives: Salesforce

Iterate Map values in Lightning Component

Apex Controller:
Create below apex controller to get the Map value.

public class MapAuraController {
    public static map<Integer, String> getMap(){
        Map<Integer, String> companyMap = new Map<Integer, String>();
        //Put value in Map 
        //return map  
        return companyMap;

Child Component:
Create below Child.cmp for display the Map key values on component.

<aura:component >
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <aura:attribute name="map" type="map"/>
    <aura:attribute name="key" type="string"/>
    <aura:attribute name="value" type="string"/>
    <p>{!v.key} - {!v.value}</p>

Child Component JS Controller:
Create below JavaScript Controller for above Child.cmp component.

    doInit : function(component, event, helper) {
        var key = component.get("v.key");
        var map = component.get("");
        component.set("v.value" , map[key]);

Parent Component:
Create below Parent.cmp, this is the main component. By this component we retrieve the map value from apex controller and pass key and map attribute to Child.cmp component.

<aura:component controller="MapAuraController">
    <!--declare attributes-->
    <aura:attribute name="keyList" type="List"/>
    <aura:attribute name="companyMap" type="map"/>
    <ui:button label="Iterate Map" press="{!c.getMapCompany}"/>
    <!--Iterate the mapCompany-->
    <aura:iteration items="{!v.keyList}" var="key" >
        <c:Child map="{!v.companyMap}" key="{!key}"/>

Parent Component:
Create below JavaScript Controller for above Parent.cmp component.

    getMapCompany: function(component, event, helper) {
        //call apex class method 
        var action = component.get('c.getMap');
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                //Create an empty array to store the map keys 
                var arrayMapKeys = [];
                //Store the response of apex controller (return map)     
                var result = response.getReturnValue();
                //Set the store response[map] to component attribute, which name is map and type is map.   
                component.set('v.companyMap', result);
                for (var key in result) {
                //Set the list of keys.     
                component.set('v.keyList', arrayMapKeys);
        // enqueue the Action   

Lightning Test App:

<aura:application extends="force:slds">
    <c:Parent />


Map Attribute in Lightning Component

Sample Component:

    <aura:attribute name="map" type="Map" default="{str1:null,str2:null,obj:null}"/>
    <aura:attribute name="str1" type="String"  default="{!}"/>
    <aura:attribute name="str2" type="String" default="{!}"/>
    <aura:attribute name="obj" type="Contact" default="{!}"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>

Sample Component JS Controller:

    doInit: function(cmp, event, helper) {
        //Set the attribute value.
        var map = cmp.get("");
        map['str1'] = 'FirstName';
        map['str2'] = 'Lastname';
        var obj = {"FirstName":"Biswajeet","LastName":'Samal'};
        map['obj'] = obj;
        cmp.set("", map);

Lightning App:

<aura:application extends="force:slds">
    <c:Example />


Avoid Recursive Trigger Calls In Salesforce

Recursion occurs when same code is executed again and again. It can lead to infinite loop and which can result to governor limit sometime. Sometime it can also result in unexpected output or the error “maximum trigger depth exceeded”. So, we should write code in such a way that it does not result to recursion.

For example, I’ve a trigger on Account object, which will be execute on before Update and after Update. In after Update I’ve some custom logic to update Account records. So, when I’m updating the Account records in After Update, it is throwing error “maximum trigger depth exceeded”. So, it is a recursion in apex trigger.

To avoid the situation of recursive call, we have to write code in such a way that the trigger will execute one time. To do so, we can create a class with a static Boolean variable with default value true. In the trigger, before executing the code keep a check that the variable is true or not.

Here in below trigger, I want to execute both before and after Update trigger only one time. I’m checking the static Boolean variable is true in both before and after Update trigger, and in after update trigger changed the variable to false. So, if trigger will try to run second time in same request then it will not run.

Apex Class:

public Class RecursiveTriggerHelper{
   public static Boolean runOnce = true;

Apex Trigger:

trigger AccountTrigger on Account(before Update, after Update) {
    if(Trigger.isBefore && Trigger.isUpdate){
            //Write your logic here
            System.debug('Is Before Update');            
    if(Trigger.isAfter && Trigger.isUpdate){
            RecursiveTriggerHelper.runOnce = false;
            Set<String> accIdSet = new Set<String>();
            for(Account acc : Trigger.New){
			//Write your logic here
            List<Account> accList = [SELECT Id, Name From Account Where Id IN : accIdSet];
            Update accList;
            System.debug('Is After Update');

Debug Log:

Check Case Owner is a User or Queue

Check Case Owner in Apex Class.

//Check object Id in Apex 
    //Owner is User       

    //Owner is Queue

Check Case Owner in Apex Trigger.

//In Apex Trigger
for (Case objCase : { 
    If (objCase.OwnerID.getsobjecttype() == User.sobjecttype) {
        /*Code if Owner is User*/
        /*Code if Owner is Queue*/ 

Check Case Owner by SOQL query.

//By Query Owner.Type Field
List<Case> caseList = [SELECT Id, CaseNumber, OwnerId, Owner.Name, Owner.Type FROM Case];

for (Case objCase : caseList){
    If (objCase.Owner.Type == User.sobjecttype) {
        /*Code if Owner is User*/
        /*Code if Owner is Queue*/ 

Check Case Owner in Process Builder.

//Check in Process Builder
BEGINS([Case].OwnerId, "005") //Check Owner is User
BEGINS([Case].OwnerId, "00G") //Check Owner is Queue

Disable LookUp Input Field on Visualforce Page

Sample Code:

<apex:page standardController="Contact" id="pg">
    <apex:form id="fm">
        <apex:inputField value="{!Contact.AccountId}" id="ifAcc" />
    var inpAcc = document.getElementById("pg:fm:ifAcc");