How to make Custom SLDS Lightning Spinner in Salesforce

Spinner is component loading indicator that should be shown when retrieving data or performing slow computations or server operations. Spinner needs to be stop displaying after operations.

 we can use spinners in two ways.

1. with standard events like aura:waiting & aura:doneWaiting : We can use this when spinner needs to be shown when all server operations means calling apex classes and receiving response.

 2. With custom events: We use this type when we need to show spinner when event occurs only like 'I need to show Spinner when user clicks on Save button'. For this we need to to define on Boolean attribute to rendering spinner.

Example:
Spinner.cmp

<aura:component >
    <aura:attribute name="showSpinner" type="Boolean"/>
    <lightning:button variant="brand" label="cancel" onclick="{!c.cancel }" />
    <lightning:button variant="brand" label="Show Spinner" onclick="{!c.Show}" />
    <aura:if isTrue="{!v.showSpinner}">
        <div class="demo-only" >
            <div role="status" class="slds-spinner slds-spinner_large slds-spinner_brand">
                <span class="slds-assistive-text">Loading</span>
                <div class="slds-spinner__dot-a"></div>
                <div class="slds-spinner__dot-b"></div>
            </div>
        </div>
    </aura:if>
</aura:component>

SpinnerController.js
({
Show : function(component, event, helper) {
  component.set("v.showSpinner",true);
  },
cancel : function(component, event, helper) {
   component.set("v.showSpinner", false);
   }
}) 
OutPut:

How to pass parameters from Lightning component to Controller.js(client side controller) with example

Lightning Tutorials Basics: Passing parameters from Lightning component to Controller.js(client side controller) and controller to Lightning component:

In this tutorial we are going to learn how to pass parameters from Lightning component to controller vice versa.

By using component.get(v.attrinuteName) or  component.find("aura:id").get("v.value") we can pass value from component to controller.
By using component.set("v.attrinuteName",value);  we can pass value from controller to lightning component.

In below code  empName attribute used to store the data which is entered by user in ui:inputText.
when user click on button "callme" function gets called and the value entered in inputText box will be passed to the attribute "empName" and the result will be printed.
<aura:component >
    <aura:attribute name="empName" type="String"/>
    <aura:attribute name="emp" type="String"/>
    <ui:inputText label="Enter Name"  aura:id="name" />
    <ui:button label="submit" press="{!c.callme}" /> <br />
    Employee Name Entered:  <ui:outputText value="{!v.empName}" />
</aura:component>


FirstComponent.js

({
    callme : function(component, event, helper) {
    var Name = component.find("name").get("v.value");
    component.set("v.empName",Name )
    }
})
Check Output
FitstComponentApp.app

<aura:application >
    <c:FirstComponent />
</aura:application>

when user entered something in input box and click on submit button "callme" function gets called and the value entered in inputText box will be passed to the attribute "empName" and the result will be printed as output below...

How to get all Parent and child objects related to the current object-soql query

Soql qury to fetch all Parent objects API names:

for(Schema.SobjectField fldMap: Account.SobjectType.getDescribe().fields.getMap().Values())
{
    if(fldMap.getDescribe().getType() == Schema.DisplayType.REFERENCE)
    {
        system.debug('Parent object Api names=====>' +fldMap.getDescribe().getReferenceTo());
    }
}

Soql qury to fetch all Child objects API names:

Schema.DescribeSObjectResult R = Account.SObjectType.getDescribe();
for (Schema.ChildRelationship childrelationship: R.getChildRelationships())
{
  system.debug('child object API names===>' +childrelationship.getChildSObject());
}

change on  API Name Account with your required object API name.

Lightning Component Basics: Add Two Numbers

In this post we are going to learn what is Attributes in Lightning Component and how to use it in components.

Attributes: Lightning Component Attributes are like  member variables in apex class.

syntax: <aura:attribute name=" " type=" " default=" " />
 here name and type are required.

ex: <aura:attribute name="Salesforce" type="String"  />


Add2nums.cmp

<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
<aura:attribute name="Number1" type="Integer" default="20" />
        <aura:attribute name="Number2" type="Integer" default="30" />
        <aura:attribute name="Sum" type="Integer" />
          {!v.Number1}+{!v.Number2}={!v.Sum}
    <br/> <br />
    <ui:button label="Click Here" press="{!c.add}" />
</aura:component>

Add2numscontroller.js

({
add : function(component) {
        var add2=component.get("v.Number1")+component.get("v.Number2");
        component.set("v.Sum",add2);
}

})

Add2nums.app

<aura:application >
    <c:Add2nums/>
</aura:application>

output:
when we click button Java script function get called and will set the value to {!v.Sum}

Salesforce Apex Collections:SET & Set Methods with Examples


Set is a unordered collection of simillar datatype elements. Set doesn't allow duplicate elemets. Set will not maintain insertion order. Salesforce has predefined Apex class called Set .This class contains methods required to support the operations on the set .

Syntax :

Set<DataType> setName =new Set<DataType>();
            or
Set<DataType> setName=new Set<DataType>(List/Set) ;

Examples :
Set<Integer> ages=new Set<Integer>();
Set<String> names=new Set<String>();

Methods : 
add(ele):  This method is used to add new elements into the set .
Example :
Set<Integer> ages=new Set<Integer>();
ages.add(90);
ages.add(30);
ages.add(10);
ages.add(90);
System.debug(ages); //  {10,30,90 }

addAll(set/list) : This method is used to add list /set of elements to a set .

Example:
List<Integer> values =new List<Integer>{10,90,30,40};
Set<Integer> ages=new Set<Integer>();
ages.add(10);
ages.add(80);
ages.addAll(values); // All the elements in the values are added to ages.
System.debug(ages);  // { 10,30,40,80,90}


remove(ele) : This method is used to remove given element from the set.

Example :
Set<Integer> ages=new Set<Integer>();
ages.add(10);
ages.add(20);
ages.add(30);
System.debug(ages); // {10,20,30 }
ages.remove(20);
System.debug(ages); // {10,30 }

removeAll(list/set) : This method is used to remove list or set of elements from the set.

List<Integer> values=new List<Integer>{10,90,40};
Set<Integer> ages=new Set<Integer>();
ages.add(10);
ages.add(30);
ages.add(90);
ages.add(50);
System.debug(ages); // {10,30,50,90 }
ages.removeAll(values); // All the elements which are in values ,remove them from the set ages.
System.debug(ages); // { 30,50}

clear(): This method will remove all the elements from the set .
Set<Integer> ages=new Set<Integer>();
ages.add(10);
ages.add(30);
ages.add(90);
System.debug(ages); // { 10,30,90}
ages.clear();
System.debug(ages); {}


size()  : This will return  count of no of elements in the set .

Set<Integer> ages=new Set<Integer>();
ages.add(10);
ages.add(30);
ages.add(90);
ages.add(50);
Integer count= ages.size();
System.debug(count) ; // 4

contains(ele) : This method will return true ,if the set contains the given element.

Set<Integer> ages=new Set<Integer>();
ages.add(10);
ages.add(30);
ages.add(90);
ages.add(50);
System.debug(ages); // {10, 30,50,90}

Boolean flag1 =ages.contains(10);
System.debug(flag1); // true
Boolean flag2 =ages.contains(80); // false
System.debug(flag2); // false

containsAll(set/list) : This method will return if all the elements in the set/list are available


List<Integer> values=new List<Integer>{10,40,90};
Set<Integer> ages=new Set<Integer>();
ages.add(10);
ages.add(30);
ages.add(90);
ages.add(50);
System.debug(ages); // {10,30,50,90 }
Boolean flag1= ages.containsAll(values);
System.debug(flag1);// false

Set<Integer> myData=new Set<Integer>{10,30};
Boolean flag2=ages.containsAll(myData);
System.debug(flag2); // true

retainAll(set/list): This will retain the common elements in the set  and given list/set.

List<Integer> values=new List<Integer>{10,40,90};
Set<Integer> ages=new Set<Integer>();
ages.add(10);
ages.add(30);
ages.add(90);
ages.add(50);
System.debug(ages); // {10,30,50,90 }
ages.retainAll(values);
System.debug(ages); // {10,90}

isEmpty() : This will return true if the set is empty .
Set<Integer> ages=new Set<Integer>();
System.debug(ages.isEmpty()); // true
Set<Integer> values ;
values.isEmpty(); // error  null pointer exception

List & List Methods with Examples

Salesforce Apex Collections: List & List Methods with Examples


List is a collection of simillar Datatype elements .DataType can be any thing that is supported by apex (Primitive,Sobjects, collections, Wrapper classes..). The  size of the list can grow or reduce based on run time requirement .

-Elements in the list maintains insertion order .
- list allows Duplicate elements .
-In the salesforce we have predefined apex class List .
-There are some predefined methods to support list operations.

Syntax for List Creation:

 List<DataType> listName =new List<DataType>();

Example:

List<Integer> ages=new List<Integer>();

Salesforce Apex List Methods with Examples

add(element) : This method is used to add new element into the list at the last position .

Example :

List<Integer> ages=new List<Integer>();

ages.add(10);

ages.add(20);

ages.add(20);

System.debug(ages); // { 10,20,20}


add(index,element) : This method is used to add new Element at the given index. Index value in the list starts with  zero

     0    1     2
------------------
| 10 |  20 | 20  |
------------------

ages.add(2,60);

  0    1     2    3
-----------------------
| 10 | 20  | 60 | 20  |
-----------------------

System.debug(ages); //  { 10,40,20,30 }

addAll(List|Set) :  This method is used to add set|List of elements into the list.

List<Integer> ages=new List<Integer>();

ages.add(10);

ages.add(20);

ages.add(30);

System.debug(ages); //  { 10,20,30 }

List<Integer> myList=new List<Integer>();

myList.add(10);

myList.add(90);

myList.add(40);

System.debug(myList); // { 10,90,40}

myList.addAll(ages);  // All  the elemenets which are in ages are added to myList.

System.debug(myList); // { 10,90,40,10,20,30 }


get(index) : This method will return the element which is at the given index .

Example :

List<Integer> ages=new List<Integer>();

ages.add(10);

ages.add(20);

ages.add(30);

ages.add(40);

System.debug(ages);  // {10,20,30,40 }

 0    1     2   3
---------------------
| 10 | 20 | 30 | 40 |
---------------------

Integer a=ages.get(2);

System.debug(a); // 30

Integer b=ages.get(3);

System.debug(b); // 40

Integer c=ages.get(0);

System.debug(c); // 10

remove(index) : This method will remove the element at the given index .

Example :  List<Integer> ages=new List<Integer>();

ages.add(10);

ages.add(30);

ages.add(40);

ages.add(90);

System.debug(ages);  // { 10,30,40,90 }

 0      1      2     3
---------------------------
| 10  |  30  |  40  | 90  |
---------------------------

ages.remove(2);

System.debug(ages); //  { 10, 30, 90}

 0      1     2
-------------------
| 10  |  30  | 90 |
-------------------

ages.remove(1);

System.debug(ages); // { 10,90 }

  0      1
--------------
|  10   | 90  |
--------------
clear() : This method will remove all the elements in the list .

Example :

List<Integer> ages=new List<Integer>();

ages.add(10);

ages.add(20);

ages.add(30);

System.debug(ages); // { 10, 20,30 }

ages.clear();

System.debug(ages); // {}

sort() : This method is used to sort the elements in the ascending order .

Example :

List<Integer> ages = new List<Integer>();

ages.add(10);

ages.add(90);

ages.add(30);

ages.add(70);

System.debug(ages); //  { 10,90,30,70 }

ages.sort();

System.debug(ages); //  { 10,30,70,90 }


clone() : This will create a duplicate copy of list of elements

Example :

List<Integer> ages=new List<Integer>();

ages.add(10);

ages.add(20);

ages.add(30);

List<Integer> values=ages.clone();

System.debug(ages); // { 10,20,30 }

System.debug(values); // { 10,20,30}

values.add(90); // Changes made on values will not effect ages

System.debug(values); // { 10,20,30,90}

System.debug(ages); // {10,20,30} // Changes made on ages will not effect values.


deepClone() : Makes a duplicate copy of a list of sObject records,
including the sObject records themselves

Example : List<Account> accs= new List<Account>();

Account a1=new Account(Name='Wipro',Industry='Banking');

accs.add(a1);

Account a2=new Account(Name='TCS',Industry='Energy');

accs.add(a2)

System.debug(accs); // {  Account :{Name=Wipro ,Industry=Banking } ,

 Account :{Name =TCS ,Industry=Energy} }

}

List<Account> dupLicates=accs.deepClone();

System.debug(dupLicates); // {  Account :{Name=Wipro ,Industry=Banking } ,

  Account :{Name =TCS ,Industry=Energy} }

     }


Notes : Any changes made on accs elements will also be applied on duplicates


size() : This will return no of elements in the list .

Example :

List<Integer> ages=new List<Integer>();

ages.add(10);

ages.add(20);

ages.add(30);

Integer count =ages.size();

System.debug(count); // 3


 set(Index,ele) : This will replace  the element at the given index with value .

Example :

List<Integer> ages=new List<Integer>();

ages.add(10);

ages.add(20);

ages.add(30);

ages.add(40);

System.debug(ages); // {10,20,30,40 }

ages.set(1,90);

System.debug(ages); // { 10,90,30,40 }

ages.set(3,55);

System.debug(ages); // {10,90,30,55}

Difference Between Group By & Order By Clause in SOQL Salesforce

ORDER BY CLAUSE: The ORDER BY clause is used to sort the query result by specific fields by ascending or descending order. By default it returns query result in  ascending order.  If we want Query result in Descending order then we need to write "Order by FieldName DESC ".

example: if we want to get latest Ten records created on Account, then query will be like below

select NAME from ACCOUNT where createddate=today order by createddate desc limit 10

above soql returns First Ten account records which was created  today.
o/p:


GROUP BY Clause:  The SOQL GROUP BY clause can be used for collecting data across multiple records and group the results by one or more columns.

Group By Clause examples: 
for example  i want to find out maximum "Annual Revenue" from Account records whose rating is "HOT".

Query: SELECT rating, maX(annualRevenue) from account where rating='hot' group by rating

for suppose if we want to find out Industry wise Maximum "Annual revenue" from account record whose rating is hot. Then query will be like below

SELECT rating,industry, Max(annualrevenue) from account where rating='hot' group by rating, industry

I want to find out how many Banking Industry Account records having no phone number

SELECT industry, count(phone) from account where industry='Banking' group by industry