About

Edit photo

Thursday, November 9, 2017

Retrieving lightning component tips


How to get Lightning Button label?
<lightning:button label="Get Label" onclick="{!c.getLabel}" aura:id="button1" />  
<aura:attribute name="buttonLabel" aura:id="btn1" type="String"/>

 var BtnLabelWay1 = event.getSource().get("v.label"); -- Used when any Action event occurs, ex: onClick  
 var BtnLabelWay2 = component.find("button1").get("v.label");          -- Can use when auraID exist  

Get and Set Attribute Value
 var att1 = component.get("v.buttonLabel"); -- Getting attribute   
 var att1 = component.find("btn1"); -- Getting attribute using aura:ID  
 component.set(att1,"this is the value for the buttonLabel"); -- Setting attribute with value   

Attribute is empty or not?
 var isEmpty = $A.util.isEmpty(cmp.get("v.label"));  


Wednesday, November 8, 2017

How to get Salesforce Lightning event label to controller?


The below code is a sample component, is having one attribute for message, and has 2 lightning buttons.
Wants to display the button label on Lighting component.

 <aura:component>  
   <aura:attribute name="message" type="String"/>  
   <p>Message of the day: {!v.message}</p>  
   <div>  
     <lightning:button label="You look nice today."  
       onclick="{!c.handleClick}"/>  
     <lightning:button label="Today is going to be a great day!"  
       onclick="{!c.handleClick}"/>  
   </div>  
 </aura:component>  

Controller:
 ({  
   handleClick: function(component, event, helper) {  
     var btnClicked = event.getSource();     // the button  
     var btnMessage = btnClicked.get("v.label"); // the button's label  
     component.set("v.message", btnMessage);   // update our message  
   }  
 })  

Component Displays:


The below code gets Button
 var btnClicked = event.getSource();     // the button  

The below code gets Button label
 var btnMessage = btnClicked.get("v.label"); // the button's label  

The below code updates the attribute named "message"
 component.set("v.message", btnMessage);   // update our message  


Source: trailhead.salesforce.com


Sunday, August 27, 2017

Lightning example, Addition of two numbers in lightning using helper


Component:
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
    
    <aura:attribute name="fname" type="Integer"/>
    <aura:attribute name="lname" type="Integer"/>
    <aura:attribute name="fullname" type="Integer"/>
    
    First name: <ui:inputText value="{!v.fname}"/>
    <br/>
    Last name: <ui:inputText value="{!v.lname}"/>
    <br/>
    <ui:button label="click me to get full name" press="{!c.fullname}" />
    <br/>
    Full name is: {!v.fullname}
</aura:component>


App:
<aura:application >
    <c:basics1/>
</aura:application>


Controller:
({
 fullname : function(component, event, helper) {
  helper.addHelper(component)
 }
})


Helper:
({
 addHelper : function(component, event, helper) {
  console.log("you pressed me buddy!");
        var first = component.get("v.fname");
        var last = component.get("v.lname");
        console.log(first);
        
        var fullname = parseInt(last) + parseInt(first);
        console.log(fullname);
        
        component.set("v.fullname",fullname);
 }
})


Output:

Lightning example, Get FullName



Component:
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
    
    <aura:attribute name="fname" type="string"/>
    <aura:attribute name="lname" type="string"/>
    <aura:attribute name="fullname" type="string"/>
    
    First name: <ui:inputText value="{!v.fname}"/>
    <br/>
    Last name: <ui:inputText value="{!v.lname}"/>
    <br/>
    <ui:button label="click me to get full name" press="{!c.fullname}" />
    <br/>
    Full name is: {!v.fullname}
</aura:component>

App:
<aura:application >
    <c:basics1/>
</aura:application>

Controller:
({
 fullname : function(component, event, helper) {
  console.log("you pressed me buddy!");
        var first = component.get("v.fname");
        var last = component.get("v.lname");
        console.log(first);
        
        var fullname = last + ', '+first;
        console.log(fullname);
        
        component.set("v.fullname",fullname);
 }
})

Output:

@SsaiK

Sunday, June 4, 2017

Salesforce Visualforce Pages Beginner component examples


What is Section Header and how to create? 
     If you are creating a new page and each page needs title and description, can be done by Section Header.

Code:

<apex:page >
    <apex:sectionHeader title="Section1" subtitle="Subtitle1" description="Description1" help="http://google.com" rendered="true"/>
</apex:page>

What is Global Data? how can we use it in VF pages in Salesforce?
     Global data is nothing but accessible in globally. we have many Global variables available in salesforce, refer the link to get all types of Global Variables.

https://help.salesforce.com/articleView?id=dev_understanding_global_variables.htm&language=en_US&type=0

Some Commonly used Global variables are
$User
$Label
$ObjectType
$Organization
$Permission
$Profile
$System
$UserRole

Code:

<apex:page controller="calAge">
    <apex:sectionHeader title="Section1" subtitle="Subtitle1" description="Description1" help="http://google.com" rendered="true"/>

    Lastname: <b>{!$User.LastName}</b> <br/>
    Organization: <b>{!$Organization.Name}</b> <br/>
    Profile: <b>{!$Profile.Name}</b>
    
</apex:page>

How to Parse Data from URL?
     {!$CurrentPage.Parameters.parameterName} is used to get Parameters from URL.
Example, Display Name, Age and Gender from URL in VF page.


Code:
<apex:page>
    <apex:sectionHeader title="Section1" subtitle="Subtitle1" description="Description1" help="http://google.com" rendered="true"/>

    Lastname: <b>{!$User.LastName}</b> <br/>
    Organization: <b>{!$Organization.Name}</b> <br/>
    Profile: <b>{!$Profile.Name}</b>
    
    Parameters from URL: -> {! $CurrentPage.Parameters.Name}, {! $CurrentPage.Parameters.Age}, {! $CurrentPage.Parameters.Gender}
    
</apex:page>

What is Controller in VF page?
     There are two type of controllers in the salesforce, named Standard Controller and Custom Controller.
Standard Controller    :-> StandardController = '<Standard Object Name>'
Custom Controller      :-> Controller = '<Apex class name>'
Extension                    :-> Extension = '<Apex class name>'

Example: Get account name, Note: can't get account name until id parameters parsed in URL.
URL?id=0014100000S6O0s

Code:
<apex:page StandardController="Account">
    <apex:sectionHeader title="Section1" subtitle="Subtitle1" description="Description1" help="http://google.com" rendered="true"/>

    Lastname: <b>{!$User.LastName}</b> <br/>
    Organization: <b>{!$Organization.Name}</b> <br/>
    Profile: <b>{!$Profile.Name}</b><br/><br/>
    
    Account name: {! Account.name}
        
</apex:page>

Example 2: Get list of Account names from Account object.
     It's time to use DataTable, if there are nay repetitive records or list of records, DataTable is required.

<!-- Updated soon-->

What are Output Components in VF page?
Output components are used to display the data, it works just like label in HTML.
OutputLabel  ->   Cannot Edit, Read only
OutputText    ->   Connot Edit, Read only
OutputField   ->   Can Edit, if used InlineEditing option.


Code:

<apex:page StandardController="Account">
    <apex:sectionHeader title="Section1" subtitle="Subtitle1" description="Description1" help="http://google.com" rendered="true"/>

    Lastname: <b>{!$User.LastName}</b> <br/>
    Organization: <b>{!$Organization.Name}</b> <br/>
    Profile: <b>{!$Profile.Name}</b><br/><br/>
    
    <apex:form >
        <apex:inlineEditSupport >
            <apex:outputLabel value="{!account.name}"/>   <br/>
            <apex:outputText value="{!account.name}"/>   <br/>
            <apex:outputField value="{!account.name}"/>   <br/>
        </apex:inlineEditSupport>
    </apex:form>
</apex:page>

What are Input components in VF pages? and example.
     Input components are used to input the values to the object, it is like Textbox in VisualStudio, inputField in HTML.

inputText     -> Used to Input text, irrespective to datatype
inputlabel    -> same as inutText, irrespective to datatype
inputField    -> Same as inputText, respective to Datatype
inputSecret  ->  Password type
inputHidden  -> Hidden Mode

What are Select Components in VF page? and Example.
     Used to select multiple/single (Ex: picklist/Radio/checkbox) value from list of options.
SelectList
SelectOption  -> Options in the picklist are created using this components
SelectOptions  -> When list of options are already available in List datatype.



Code:
VF Page:
<apex:page Controller="calAge1">
    <apex:sectionHeader title="Section1" subtitle="Subtitle1" description="Description1" help="http://google.com" rendered="true"/>

    Lastname: <b>{!$User.LastName}</b> <br/>
    Organization: <b>{!$Organization.Name}</b> <br/>
    Profile: <b>{!$Profile.Name}</b><br/><br/>
    
    <apex:form >
        <apex:selectList size="1">
          <apex:selectOption itemLabel="jan" itemvalue="one"/>
          <apex:selectOption itemLabel="feb" itemvalue="two"/>
          <apex:selectOption itemLabel="mar" itemvalue="three"/>
        </apex:selectList>
        
        <apex:selectList size="1">
          <apex:selectOptions value="{! Method1}"/>
        </apex:selectList>
    </apex:form>
</apex:page>

Apex Custom Controller:
public with sharing class calAge1 {
    public List<SelectOption> getMethod1()
    {
        List<Account> lname = [Select name from account limit 10];
        List<SelectOption> ll = new List<SelectOption>();
        for(integer i=0;i<10;i++)
        {
            ll.add(new selectOption(String.valueOf(i),String.valueOf(lname[i].name)));
        }
        return ll;
    }
}

by
SsaiK

Sunday, March 19, 2017

How to install Apache ANT tool on Windows 10



Installing Apache ANT on Windows 10

Download the Apache ANT from http://ant.apache.org/bindownload.cgi
Extract to the C -> Program Files

Now open the system properties, click on environment variables;

do the same as in the screen shot
Set JAVA_HOME:

Set ANT_HOME:


Click on PATH and add the highlighted text


Now restart the system and check whether it is installed succesfully by typing,
ant -version

@SsaiK

Saturday, March 18, 2017

Scheduling Jobs Simple examples


Example1:

Create a task for the opportunity owners who's opportunity is not closed even close date cross the close date.

Note: Schedule and Batch classes always should be Global.

global class RemindOpptyOwners implements Schedulable {

    global void execute(SchedulableContext ctx) {
        List<Opportunity> opptys = [SELECT Id, Name, OwnerId, CloseDate 
            FROM Opportunity 
            WHERE IsClosed = False AND 
            CloseDate < TODAY];
        // Create a task for each opportunity in the list
        TaskUtils.remindOwners(opptys);
    }
    
}
//Code from Salesforce.com - Trailhead


NoteTaskUtils.remindOwners(opptys); is creates a task for the owners.

Now, Autorun the jobs for every Monday at 2 am.

Schedule jobs run as -> Seconds Minutes Hours DaysOfMonth Month DayOfWeek Year

There are also some special characters you can use:

, used to delimit values [hours, day of month, month, day of week, year]
– used to specify a range [hours, day of month, month, day of week, year]
* used to specify all values [hours, day of month, month, day of week, year]
? used to specify no specific value [day of month, day of week]
/ used to specify increments [hours, day of month, month, day of week, year]
L used to specify the end of a range [day of month, day of week]
W used to specify the nearest weekday [day of month]

# used to specify the nth day of the month [day of week]

RemindOpptyOwners reminder = new RemindOpptyOwners();
// Seconds Minutes Hours Day_of_month Month Day_of_week optional_year
String sch = '0 30 8 10 2 ?';
String jobID = System.schedule('Remind Opp Owners', sch, reminder);


Testing:
http://blog.deadlypenguin.com/blog/2012/05/26/scheduled-actions-in-salesforce-with-apex/

I'll write my own test, Coming soon with real time examples.

Thursday, February 16, 2017

APEX Test Classes - Examples



Apply @istest annotation to code.

Signature Characterisitics:

  • static --- test method must be a static
  • void  --- it should always no return type.
  • no parameters --- Don't use parameters.

Example:
@istest
public class myclass
{
     @istest private static void mytest() {...}
     //or
     private static void testmethod mytest() {...}
}


Test cannot do:

  • commit changes to DB.
  • Perform callouts to external systems.
  • send outbound messages.

SOQL cannot find pre-existing data, except

  • user
  • profile
  • organization
  • record type
But if use @istest(seealldata=true) annotation, can get data from all the objects.


Test Requirements:

  • Atleast 75% apext statement must be executed.
  • All apex triggers must be called
  • All apex tests must executed without any exceptions / exceeding governors. 

we use,

  • system.assert()
  • system.assertequals()
  • system.assertnotequals()
Note: i) pass upto 200 records only. ii) Don't depend on pre-existing data.

Imp: use Test.StartTest() and Test.StopTest()

Example:
I have 3 objects named, a, b, c. C is parent of B and A,  B is parent of A. A to B has Master, B to C has Master, A to C has lookup. I want to get the C name to my A where B's object is having C.

trigger afrombfromc on a__c (before insert, before update) {
 set<id> aID = new set<id>();
 set<id> bID = new set<id>();
 set<id> cID = new set<id>();
 
 for(a__c a1:trigger.new)
 {
  bID.add(a1.b_object__c);
 }
 
    map<id,b__c> map2;
    map<id,c__c> map3;
    
 if(!bID.isempty())
  map2 = new map<id,b__c>([select id, name, b_field__c, c_object__c from b__c where id in:bID]);
    
    for(b__c b:map2.values())
    {
        cID.add(b.c_object__c);
    }
    
    if(!cID.isempty())
  map3 = new map<id,c__c>([select id, c_field__c from c__c where id in:cID ]);
 
 for(a__c a1:trigger.new)
 {
  a1.b_field__c = map2.get(a1.b_object__c).b_field__c;
  //a1.c__c = map3.get(map2.get(a1.b_object__c).b_object__c).id;
  a1.c__c = map3.get(map2.get(a1.b_object__c).c_object__c).id;
 }/span>
}

Apex Test Class:

@istest public class afrombfromc_test {
 @istest public static void mytest1()
    {
        c__c c = new c__c();
        c.name = 'cname';
        insert c;
        b__c b = new b__c();
        b.name = 'bname';
        b.c_object__c = c.id;
        insert b;
        a__C a = new a__c();
        a.name = 'aname';
        a.b_object__c = b.id;
        insert a;
        a__c asoql = [select c__c from a__c where id=:a.id];
        system.assertEquals(asoql.c__c, c.id);
    }
}




Thursday, February 9, 2017

Approval process using apex


An approval process is an automated process which can be used to approve/reject record updates. A record can be submitted for approval request from related list "Approval History". Once a records is submitted it goes for approval to a specified approver. This is a manual process where in every record should be individually sent for approval.
 How about doing this using apex? Sending the record fro approval from trigger? Salesforec provides number of method for handling approval processes in apex.

Let us submit a record for approval process from trigger in an example below.

Lets say you have a approval process named "Account Owner Approval". You can create a approval process by navigating to following:
set up --> create --> approval process





















Trigger to submit a account record for approval if its annual revenue is less then 2000
?
1
2
3
4
5
6
7
8
9
10
trigger Call_AprovalProcess_In_Trigger on Account (before insert, before update) {
 for(Account acc:trigger.new){
    if(acc.AnnualRevenue < 2000){
       approval.ProcessSubmitRequest aprlPrcs = new Approval.ProcessSubmitRequest();     
       aprlPrcs .setComments('Submitting record for approval.');
       aprlPrcs.setObjectId(acc.id);
       approval.ProcessResult result = Approval.process(aprlPrcs);
    }
 }
}