Tag Archives: Force.com

Convert sObject to JSON String and JSON String to sObject Using Apex in Salesforce

Sample Code:

Account acc = new Account(Name = 'Account Name', Phone = '8888888888', Industry = 'Agriculture');
//Code to convert Account to JSON string
String str = JSON.serialize(acc);
system.debug('Account JSON Data - ' + strJSON);
//Code to convert JSON string to Account
Account acc1 = (Account)JSON.deserialize(str, Account.Class);
system.debug('Account Data - ' + acc1);


Set Default Value in Picklist in Lightning Component

Here in below example the default value in picklist is “Apple”.
Lightning Component:

    <ui:inputSelect aura:Id="brandId" multiple="false" label="Select Brand">
        <ui:inputSelectOption label="Apple" text="Apple" value="true"/>
        <ui:inputSelectOption label="Nokia" text="Nokia"/>
        <ui:inputSelectOption label="Samsung" text="Samsung"/>
        <ui:inputSelectOption label="Google" text="Google"/>
        <ui:inputSelectOption label="Mi" text="Mi"/>


Multiple Conditions in aura:if on Lightning Component

aura:if evaluates the isTrue expression on the server and instantiates components in either its body or else attribute. Only one branch is created and rendered. Switching condition unrenders and destroys the current branch and generates the other.

In aura:if tag we can not use &&,AND,||,OR operator for multiple conditions in isTrue attribute, but we can use logical functions in aura:if tag like or(), and().

Here is the example how to use multiple Boolean conditions in aura:if tag.

Lightning Component:

<aura:component >
    <aura:attribute name="ATrue" type="boolean" default="true"/>
    <aura:attribute name="BFalse" type="boolean" default="false"/>
    <aura:attribute name="CTrue" type="boolean" default="true"/>
    <aura:attribute name="DFalse" type="boolean" default="false"/>
    <!--aura:if Sample -->
    <aura:if  isTrue="{!and(v.ATrue, v.CTrue)}" >
        <div style="padding:15px; background-color:LightBlue">
            And => This div show because both A and C attributes are true
    <!--aura:if with aura:set sample-->
    <aura:if  isTrue="{!or(v.DFalse, v.BFalse)}">
        <div style="padding:15px;">
            Or => This div show because one attribute is true
        <aura:set attribute="else">
            <div style="padding:15px;">
                Or => This div show because both attributes are false
    <!--aura:if with nested and condition-->
    <aura:if  isTrue="{!or(and(v.ATrue, v.CTrue), v.BFalse)}" >
        <div style="padding:15px; background-color:LightGreen  ">
            Nested condition div show because in statment one of or condition returns true

Lightning App: