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

What is Object & How to create custom object in salesforce

Salesforce Object: Objects are nothing but Tables in regular database. The entire application data is stored  in the Object.

> There are two types of Objects.
1. Standard Objects.
2. Custom Objects.


Standard Objects: These are the Objects, Which are provided by salesforce.com to meet global CRM functionalities.
Eg: Accounts, Contacts, Leads, Products, Campaigns, Cases, Solutions, Reports, Dashboards..


Custom Objects: Custom Objects are created by the user to meet his organizational business requirements. These are the main things of any application.
> Custom Objects provide a structure for strong data.
> Custom Objects have properties such as
       1. Custom fields.
       2. Relationship to other objects.
       3. Page layouts.
       4. A Custom user interface tab (optional).

How to create Custom Object in Salesforce : 

Navigation: Click on your name--> Setup-->  Build --> Create --> Objects --> New Custom Object. You will get below console.

-Then click on "New Custom Object"  button. You will below screen and fill the fields with respective data.


here Object label :  This is the name in which object is visible to the users.
Object Name : If we want to refer to the object programatically we use this name.
Plural Name: If we create a tab for an object 'Plural Name' is displayed on tab panel. Ex: if we want to create a object called Student then plural name Students will be displayed on Tab panel.
Context Sensitive Help: On every object , Salesforce provide help link. When we click on the link Salesforce standard page will open. In case if you want to define your own help page you can redefine the help page.

Record Name: Salesforce by default creates a this field. This is required field. It allows two data types, I.e Text & Auto Number. If we choose Text as a datatype user has to manually enter the alphanumeric data. If we choose Auto Number as the datatype, System would automatically generate a number based on the number we have given. Ex: EID-{001} .

Optional Features: These options can be enable or disable at the time of creating an object or later at any stage of the application development. They are
Allow Reports: if we enable this option we can create reports on the object data.
Allow Activities: If we enable this option we can create tasks & calendar events on an Object.
Track Field History: If you enable this option any changes that are made on the fields of the Objected are tracked.

 Deployment Status :
 In Development :  If you specify the status as in development the process of development is not at completed and don’t show object any user except system Admin.
Deployed :  The process of development is completed and users can access the object based on the Security model of the organization.

Object Creation options
These  options can be enabled or disabled only at the time of creating a object .
There are two options ;
1.Add notes and Attachments to related list of the record :
If you enable this option we  can add notes and attachments to the related list of the record like
Email attachment
2.Launch new Custom Tab wizard after saving the object :

If you enable this option once the object is saved it will automatically takes navigation to tab creation page.
    After entering all the fields click on 'save' button .

Note: If we doesn't select the option called ' Launch New Custom Tab Wizard' from from above step, the object will save without Tab creation on tab panel, Then we have to create tab again for this object. If we click that option, Tab will be appeared in Tab Panel.


Tags: Object in salesforce how to create custom object in salesforce