Thursday, September 13, 2018

Best way of using lightning helper method and Switch statements

JavaScript Controllers and Helpers play a key role in Lightning Components Framework. One of the first things people new to the framework wonder is: What exactly is the difference between JavaScript Controllers and JavaScript Helpers. 

 To understand it better, here I'm taking simple example to determine what would get from Helper and why we need to delegate logic to the helper from controller. Though this code incorporated with basic logic like add, sub, mul and div. It has lot of take away's in terms of frame work concepts.  Out put looks as below.







Earlier I have provided similar kind of code to demonstrate getters, setters, hide and seek mechanism, and many more with very basic example to keep the things simple. If you want to check it, please hit this link Simple calci for the same. 

================

Step 1: create a component with the name "NoHelpercmp" and  copy the below mark up code in the new component

Mark up: c:NoHelpercmp 

================
<aura:component implements="flexipage:availableForAllPageTypes" access="global" >

    

    <aura:attribute name="num1" type="integer" />

    <aura:attribute name="num2" type="integer" />    

    <aura:attribute name="num3" type="integer" />

    <aura:attribute name="str" type="String" default="None of them selected"/>
    <lightning:card variant="Narrow" title="My Caluclations" >

        <lightning:input name="input1" label="Enter first number" type="number" value="{!v.num1}" aura:id="abc" id="ccc"/>

        <lightning:input name="input2" label="Enter second number" type="number" value="{!v.num2}" aura:id="xyz"/>

        {!v.str}     <ui:outputNumber value="{!v.num3}"/>

        <aura:set attribute="actions">

            <lightning:buttonGroup >

                <lightning:button label="Div" variant="brand" onclick="{!c.Div}" title="Div Method"  />

                <lightning:button label="Add" variant="success" onclick="{!c.Add}" title="Add Method"  />

                <lightning:button label="Sub" variant="brand" onclick="{!c.Sub}"  />

                <lightning:button label="Mul" variant="destructive"  onclick="{!c.Mul}"  class=""/>

            </lightning:buttonGroup>

        </aura:set>
    </lightning:card>

</aura:component>

==============

Step 2: copy the below code and paste it in the NoHelpercmp Controller

=================

({
    Sub : function(component, event, helper) {

     var n1=parseInt(component.find("abc").get("v.value"));  //repeated code

         var n2=parseInt(component.find("xyz").get("v.value"));  //repeated code

         var c=n1-n2; 

         component.set("v.str","sub of 2 number");

          component.set("v.num3",c);

},

    Mul : function(component, event, helper) {

         var n1=parseInt(component.find("abc").get("v.value"));  //repeated code
         var n2=parseInt(component.find("xyz").get("v.value"));  //repeated code

         var c=n1*n2; 

          component.set("v.str","Mul of 2 number");

      component.set("v.num3",c);

},
Add : function(component, event, helper) {

     var n1=parseInt(component.find("abc").get("v.value"));  //repeated code

         var n2=parseInt(component.find("xyz").get("v.value"));  //repeated code

         var c=n1+n2; 

         component.set("v.str","sub of 2 number");

          component.set("v.num3",c);

},

    Div : function(component, event, helper) {

         var n1=parseInt(component.find("abc").get("v.value"));  //repeated code

         var n2=parseInt(component.find("xyz").get("v.value"));  //repeated code

         var c=n1%n2; 

          component.set("v.str","Mul of 2 number");

      component.set("v.num3",c);

}

})



============

Test the above component by using below application container, It still works but its not a best practice to handle the logic in the controller. Your Js controller methods were not desinged for re-usability  purpose.

step 3 : create aura:application container and replace following code and preview it.

============

<aura:application extends="force:slds">

    <c:NoHelpercmp/>


</aura:application>

================

Mark up: c:Helpercmp 

Follow the similar kind of steps which you followed from step 1 to step 3 and then additionally use the step 4(helper function) which is mentioned below.

================

<aura:component implements="flexipage:availableForAllPageTypes" access="global" >

    

    <aura:attribute name="num1" type="integer" />

    <aura:attribute name="num2" type="integer" />    

    <aura:attribute name="num3" type="integer" />

    <aura:attribute name="str" type="String" default="None of them selected"/>

    
    <lightning:card variant="Narrow" title="My Caluclations" >

        <lightning:input name="input1" label="Enter first number" type="number" value="{!v.num1}" aura:id="abc" id="ccc"/>


        <lightning:input name="input2" label="Enter second number" type="number" value="{!v.num2}" aura:id="xyz"/>

        {!v.str}     <ui:outputNumber value="{!v.num3}"/>

        <aura:set attribute="actions">

            <lightning:buttonGroup >

                <lightning:button label="Div" variant="brand" onclick="{!c.Allcaluclations}" title="Div Method"  />

                <lightning:button label="Add" variant="success" onclick="{!c.Allcaluclations}" title="Add Method"  />

                <lightning:button label="Sub" variant="brand" onclick="{!c.Allcaluclations}"  />

                <lightning:button label="Mul" variant="destructive"  onclick="{!c.Allcaluclations}"  />

            </lightning:buttonGroup>

        </aura:set>

        

        

    </lightning:card>

</aura:component>



=================

Js controller code with delegates business logic to helper.

=================



({

Allcaluclations : function(component, event, helper) {

      helper.Mymethod(component, event, helper);

}

})


==============

Step 4: Helper to perform all the calculations with in the same function

==============

({

    Mymethod : function(component, event, helper) {

        var buttonlabel=event.getSource().get('v.label');

        this.Myswtich(component, event, helper,buttonlabel);


    },


     Myswtich : function(component, event, helper,buttonlabel) {

            var a=parseInt(component.get('v.num1'));

        var b=parseInt(component.get('v.num2'));

        switch (buttonlabel) {

            case 'Add':

                component.set("v.str","Addition of two numbers");       

                var c=a+b; 

                break;

            case 'Sub':

                component.set("v.str","Sub of two numbers");

                var c=a-b; 

                break;

            case 'Mul':

                component.set("v.str","Mul of two numbers");

                var c=a*b; 

                break; 

            case 'Div':

                component.set("v.str","Div of two numbers");

                var c=a%b; 

                break; 

        }

        
        component.set("v.num3",c);


     }

    

})



============

Test the above component by using below application container.

============



<aura:application extends="force:slds">

    
    <c:Helpercmp/>



</aura:application>

Saturday, August 4, 2018

Lightningout in W3 schools (Share Lightning Out Apps with Non-Authenticated Users)

Share Lightning Out Apps with Non-Authenticated Users


Paste the below code in the w3 schools which enables you to see the lightning component screen in the external site(w3school). As of my research this is the simple and best POC to show how the Lightning out works in the external sites.
====================================================================
<!DOCTYPE html>
<html>
<head>
<body>
<div id="lightning" />


<script src="https://oliva-developer-edition.ap5.force.com/dave/lightning/lightning.out.js"></script>

<script type="text/javascript">


window.onload= function(){

    $Lightning.use("c:CalciApp", function() {
          $Lightning.createComponent("c:Calci",
          {},
          "lightning",
          function(cmp) {
            // do some stuff
          });
        }, "https://oliva-developer-edition.ap5.force.com/dave");
     
}       
     
</script>
==================================================================

Add the ltng:allowGuestAccess interface to your Lightning Out dependency app to make it available to users without requiring them to authenticate with Salesforce. This interface lets you build your app with Lightning components, and deploy it anywhere and to anyone.
A Lightning Out dependency app with the ltng:allowGuestAccess interface can be used with Lightning Components for Visualforce and with Lightning Out.

The ltng:allowGuestAccess interface is only usable in orgs that have Communities enabled, and your Lightning Out app is associated with all community endpoints that you’ve defined in your org.

Note: When you make a Lightning app accessible to guest users by adding the ltng:allowGuestAccess interface, it’s available through every community in your org, whether that community is enabled for public access or not. You can’t prevent it from being accessible via community URLs, and you can’t make it available for some communities but not others.
IMP: this lightningout only works when you keep your community domain in the code. It wont work with your regular org domain.

For more details, please follow the below source.

Lightningout documentation

None of SSO users were able to login.

If SSO is down and you dont have option to login into the Salesforce org completely, don't worry there is a way system administrator can login into the org by hitting below URL 

<MyDomain URL>?login=1 


Replace My domain url with your companies domain name and try to login, you should be able to login.

Tuesday, May 1, 2018

SALESFORCE CERTIFIED PLATFORM DEVELOPER – SPRING ’18 RELEASE EXAM QUESTIONS

1 of 5. What is the replacement that Salesforce recommends for Force.com IDE 2 Beta?
  • A. MavensMate for Sublime Text
  • B. Workbench
  • *C. Salesforce Extensions for VS Code
  • D. Developer Console
2 of 5. Which Salesforce command line interface (CLI) command supports the generation of new Apex triggers?
  • A. force:data:sobject:createtrigger
  • *B. force:apex:trigger:create
  • C. force:schema:sobject: -t I createtrigger
  • D. force:trigger:new
3 of 5. Which debug type captures information related to accessing external objects with the Salesforce Connect cross-org and OData adapters?
  • A. XDS_REQUEST
  • *B. XDS_RESPONSE
  • C. XDS_OBJECT
  • D. XDS_CONNECT
4 of 5. Which global variable can be used to detect whether a Visualforce page is loaded in Lightning apps with console navigation or standard navigation?
  • A. $U1.UserTheme
  • B. $Browser.formFactor
  • C. $Organization.UlTheme
  • *D. $User.U1Theme
5 of 5. What can be used to control the styling and behavior of the Salesforce login process?

  • A. Lightning component login flow
  • *B. Visualforce Page login flow
  • C. Process Builder on login event
  • D. Quick Action on login event

Salesforce Certified Platform App Builder - Spring '18 Release Exam

1 of 5. What is a new feature in Lightning Experience added to Reports and Dashboards in this release?
  • A. Matrix Reports
  • B. Joined Reports
  • C. Row limit filters on Dashboards
  • D. Subscribe to Reports
  • *E. Subscribe to Dashboard
2 of 5. How can an App Builder add a Flow to the Action menu on a Lighting record page?
  • A. Using a Global Action
  • B. Using an Auto-launched Flow
  • *C. Using an Object-specific Action
  • D. Using a Lightning component
3 of 5. Which new standard feature should an App Builder use to collect Sales team feedback about a new Lightning page layout?
  • A. Survey Quick Action
  • B. Survey Email Alert
  • C. Survey Visualforce page
  • *D. Survey Lightning component
4 of 5. Which data protection functionality does the Individual object provide?
  • A. Personal Information packaging
  • B. Personal Information deletion
  • *C. Data Privacy Preferences
  • D. Data Privacy Protection
5 of 5. Which object now triggers a Process Builder and Workflow Rule?

  • A. Orders
  • *B. Topics
  • C. Assets
  • D. Campaigns

Saturday, April 28, 2018

Get Cookies Data in VF page


<apex:page >


    <script type="text/javascript">

    function validate()
    {
   alert(document.cookie);
    }

    </script>
<!-- Javascript -->

<apex:form id="frm">
    <apex:pageBlock id="pb">
     
   
   
            <apex:commandButton value="Insert" onclick="validate();"/>
       
   
    </apex:pageBlock>
</apex:form>


</apex:page>