Showing posts with label community. Show all posts
Showing posts with label community. Show all posts

Friday, April 24, 2020

Dynamically access any variable in an instance class

In this blog post, I will show you how to access any variable on an instance of an Apex class dynamically.

This is something useful when you want to make dynamic feature which are based on setting, especialy when doing ISV stuffs.

The code is as simple as below:

public class Employee {
    public String employeeId;
    public String firstname;
    public String lastname;
    public String username;
    public String profileUrl;

    public Object getValue(String variableName) {
        String jsonString = toJSON();
        Map<String, Object> untyped_instance;
        untyped_instance = 
            (Map<String, Object>)JSON.deserializeUntyped(jsonString);
        return untyped_instance.get(variableName);
    }
    
    public string toJSON() {
        return JSON.serialize(this);
    }
}

Example:

Employee emp = new Employee();
emp.employeeId = 'test';
emp.firstname = 'kevan';
emp.lastname = 'moothien';
emp.username = 'kevanmoothien';
emp.profileUrl = 'https://picsum.photos/200/300';

system.debug(emp.getValue('firstname')); // returns kevan
system.debug(emp.getValue('lastname')); // returns moothien

Here is a use case where this kind of integration can be used based on the above code.
Suppose you have to develop a custom related list lightning component which would be used on several layout. But on each layout, it has to display different columns.

To achieve this, we would think of different component and hardcoded variable. But, this can be done by simply having a Custom Metadata type to save the setting of each layout and the related list custom component can simply display the columns value dynamically using the above code based on the setting provided in the Lighting app builder.

Hope this is useful to you trailblazers.

Cheers :)

Thursday, February 9, 2017

Does a Lightning Component know if it is in a Community or a standard Lightning Salesforce page?



I have a component that implements both the interface for Community and Standard Lightning.
Some people suggest to add an attribute and set it in the Lightning App Builder or Community Builder so as to distinguish in which kind of environment we are.

It is quite annoying for the System Administrator to configure the behavior of the component each time it is needed.

But what if this can be achieved automatically by only a few lines of code?

Here is how I did it:

Your component must first extends a controller.

I Hope that this post will be helpful to you. Cheers!!
Lightning component .cmp aura:component implements="force:hasRecordId, force:appHostable, flexipage:availableForAllPageTypes, forceCommunity:availableForAllPageTypes" controller="CustomController"> aura:handler name="init" value="{!this}" action="{!c.init}"/> /aura:component Apex controller .cls public with sharing class CustomController { @AuraEnabled public static boolean isCommunity(){ Id siteId = Site.getSiteId(); if (siteId != null) { return true; } return false; } } Lightning component controller .js ({ init : function(component, event, helper) { var action = component.get("c.isCommunity"); action.setCallback(this, function(response) { var isCommunity = response.getReturnValue(); // do any operation needed here }); $A.enqueueAction(action); } })