Wednesday, 6 December 2017

Visualforce: Showing nearest account locations on google map

Visualforce: Showing nearest account locations on google map

Few days back i got a requirement from my client that salesperson need a app in salesforce which allows them to do following.

Requirements:
  • When they are onsite to visit an account or prospect this salesforce app will tell them other nearby(get current location as they will be using this app from mobile) accounts location on google map.This will help them to visit more than 1 account in a day.
  • Allow them to enter a dynamic address and on the bases of that address find out nearby accounts.
Problem:
  • In order to get current/dynamic location latitude and longitude to show on map we have to use any API which will provide us latitude and longitude values in return of address.
  • All the account in the system should have their latitude and longitude values saved in a field in order to show them on map.
  • Client do not want to pay for any API.
Solution:

  • Many of you know about data integration rule. This feature is released by Salesforce few releases back. You can get a better idea of this feature from this link.
  • This will help us to get lat and lon values of all accounts resit in our database. We will use javascript to get lat and lon values of current location and dynamic entered address.
Enough of talk lets do some code :)

Visualforce page:

<apex:page docType="html-5.0" sidebar="false" Controller="accountMappingController" showHeader="true" id="page">  
    <apex:pagemessages id="message"/>
<apex:stylesheet value="{!URLFOR($Resource.bootstrap, '/dist/css/bootstrap.min.css')}"/>
<apex:includeScript value="/support/console/34.0/integration.js"/>
<script type="text/javascript" src="https://maps.google.com/maps/api/js?sensor=false"></script>



  <script> var previousOnload = window.onload; window.onload = function() { if (previousOnload) { previousOnload(); } setUserLocation(); } </script> 

<!-- Javascript to get the user's current location and set appropriate URL call-->
<script type="text/javascript">

    
      function geocodeAddress() {
        var street = document.getElementById('page:MyId:Pb:street1').value;
        var city = document.getElementById('page:MyId:Pb:city1').value;
        var state = document.getElementById('page:MyId:Pb:state1').value;
        var country = document.getElementById('page:MyId:Pb:country1').value;
        var address = street + ' ' + city + ' ' +state + ' ' +country;
        var geocoder = new google.maps.Geocoder();
        geocoder.geocode({'address': address}, function(results, status) {
          if (status === 'OK') {
            
            var array = results[0].geometry.location.toString();
            array = array.replace(/\(|\)/g,'').split(",");
            document.getElementById("page:MyId:Pb:Lat").value = array[0];
            document.getElementById("page:MyId:Pb:Long").value = array[1];
            NewCurrentLocation();
          } else {
            alert('Geocode was not successful for the following reason: ' + status);
          }
        });
}

    

    function setUserLocation() {
        if("{!street}" && "{!city}" && "{!state}"){
        }
        else{
            if (navigator.geolocation) { 
                navigator.geolocation.getCurrentPosition(function(loc){
                    var latlon = loc.coords.latitude + "," + loc.coords.longitude;
                    var el = document.querySelector("input.currentPosition");
                    el.value = latlon;
                    generateMap();
                });
            }
            
            var gURL;
            var el2 = document.querySelector("input.googleURL");
            
            if(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
                gURL = "comgooglemaps://?"; // if User device is mobile, call Google Maps mobile app url
            }
            else {
                gURL = "https://www.google.com/maps?"; // else, call Google Maps for Desktop url
            } 
            
            el2.value =  gURL;
        }
    }
    

</script>

<div class="bs" id="firstdiv">

    <div class="row">        
        <div class="col-xs-12"> <!--Block of code used to show accounts coming from controller on page  -->
            <apex:outputPanel rendered="{!IF(accounts.size==0,false,true)}" id="sectionsPanel" >              
                <apex:map width="100%" height="500px" center="{!userLocation}">   
                    <apex:repeat value="{!accounts}" var="a">
                        <apex:mapMarker icon="http://maps.google.com/intl/en_us/mapfiles/ms/micons/red-dot.png" id="accountMapMarker"
                        position="{latitude: {!a.BillingLatitude}, longitude: {!a.BillingLongitude}}">
                            
                            <apex:mapInfoWindow >
                                <apex:outputPanel layout="block" style="font-weight: bold;  font-size: 14px;">
                                    <apex:outputText >{!a.Name}</apex:outputText>
                                </apex:outputPanel>                              
                                <a href="tel:{!a.Phone}">{!a.Phone}</a>                                                             
                                <apex:outputPanel layout="block">
                                    <apex:outputText ><b>Account Type</b> {!a.Type} </apex:outputText>
                                </apex:outputPanel>
                                
                                <apex:outputPanel layout="block">
                                    <apex:outputText >{!a.BillingStreet}, {!a.BillingCity}, {!a.BillingState}</apex:outputText>
                                </apex:outputPanel>
                                <button type="button" onclick="window.open('{!googleURL}daddr={!a.BillingLatitude},{!a.BillingLongitude}&saddr={!currentPosition}'); return false;" >Get Directions</button>
                                
                               
                            </apex:mapInfoWindow>
                        </apex:mapMarker>
                    </apex:repeat>
             
                       <apex:mapMarker id="UserLocationMapMarker" position="{!currentPosition}" icon="http://maps.google.com/mapfiles/ms/micons/green-dot.png" > 
                       
                        <apex:mapInfoWindow >
                            <apex:outputPanel layout="block" style="font-weight: bold;  font-size: 14px;">
                                <apex:outputText >Current Location</apex:outputText>
                            </apex:outputPanel>
                            <apex:outputPanel layout="block">
                                <apex:outputText >Search Google Maps<BR/>for "current Location"<BR/>in your area</apex:outputText>
                            </apex:outputPanel>
                            <button type="button" onclick="window.open('{!googleURL}q=Garage+Doors&center={!currentPosition}&zoom=20'); return false;">Get Directions</button>
                            
                        </apex:mapInfoWindow>
                    </apex:mapMarker>  
                </apex:map>
            </apex:outputPanel>
        </div>
    </div>
</div> 

<apex:form id="MyId">
    <apex:outputText rendered="{!IF(accounts.size==0,true,false)}"><b>Loading Account Map...</b></apex:outputText>
    
    <apex:actionFunction action="{!findbyNewCurrentLocation}" name="NewCurrentLocation"/>
    
    <apex:actionFunction action="{!redirectByLocation}" name="generateMap" rerender="page"/> 
    <apex:input size="30" id="currentPosition" styleClass="currentPosition"  style="visibility: hidden;"  value="{!currentPosition}" />
    <apex:input size="30" id="resultsAvailable" styleClass="resultsAvailable" style="visibility: hidden;"   value="{!resultsAvailable}" />
   
    <apex:input size="1" id="googleURL" styleClass="googleURL" style="visibility: hidden;" value="{!googleURL}" /> 
    
    
  
    <apex:pageBlock title="Change Current Location" id="Pb">
            <table width="60%" align="center">
                <tr><td>
                    <b><apex:outputLabel value="Street " for="street" style="width:5%" /></b></td><td>
                    <apex:inputtext value="{!street}" id="street1" style="width:65%"/></td><td>
                    <b><apex:outputLabel value="City"  for="city" style="width:10%"/></b></td><td>
                    <apex:inputtext value="{!city}" id="city1" style="width:55%"/></td><td>
                    <b><apex:outputLabel value="State"  for="state" style="width:10%"/></b></td><td>
                    <apex:inputtext value="{!state}" id="state1" style="width:55%"/></td><td>
                    <b><apex:outputLabel value="Country"  for="country" style="width:10%"/></b></td><td>
                    <apex:inputtext value="{!country}" id="country1" style="width:55%"/></td>
                    </tr>
            </table>
  <apex:inputHidden id="Long" value="{!CusLong}" />
  <apex:inputHidden id="Lat" value="{!CusLat}" />
 
        <apex:commandButton value="Change Current Location" styleClass="buttonStyle" onclick = "geocodeAddress();"
                style="font-weight: bold;" rerender="page,MyId,message" status="loadingStatus" id="submit"/>
    </apex:pageBlock>
<apex:actionstatus id="loadingStatus">
          <apex:facet name="start">
              <div style="position: fixed; top: 0; left: 0; right: 0; bottom: 0; opacity: 0.25; z-index: 1000; background-color: black;">
                  &nbsp;
              </div>
              <div style="position: fixed; left: 0; top: 0; bottom: 0; right: 0; z-index: 1001; margin: 15% 50%">
                  <div style="display: inline-block; padding: 2px; background-color: #fff; width: 125px;">
                      <img src="/img/loading.gif" style="float: left; margin: 8px;" />
                      <span style="display: inline-block; padding: 10px 0px;">Please Wait...</span>
                  </div>
              </div>
          </apex:facet>
      </apex:actionstatus>
</apex:form>

</apex:page>
Note: Billing address is used to show location on map. This works on SF1 too.

Controller Code:

public with sharing class accountMappingController {

    public Map<String, Double> userLocation { get; private set; }  
    
    public List<Account> accounts { get; set; } 
    public List<Account> prospects { get; private set; }
    public List<Account> suspects { get; private set; }
    public String googleURL { get; set; }
    public String street{ get; set; }
    public String city{ get; set; }
    public String state{ get; set; }
    public String country {get;set;}
    public string CusLat {get;set;}
    public string CusLong {get;set;}
    
    public accountMappingController (){
        Apexpages.currentPage().getHeaders().put('X-UA-Compatible', 'IE=10');
        
        
    }
    
    public String currentPosition {
        get {
            if(String.isBlank(street) && String.isBlank(city) && String.isBlank(state)){
                if (String.isBlank(currentPosition)) {
                    currentPosition = '0,0';
                }
            }
            return currentPosition;
        }
        set;
    }
    
    public Boolean resultsAvailable {
        get {
            if(accounts == Null) {
                return false;
            }
            return true;
        }
        
        set;
    }
    

    public PageReference findNearby() {
        String lat, lon;
        
        System.debug('Inside findNearby, Position='+street);
        
        List<String> latlon;
      
        latlon = currentPosition.split(',');
       
        lat = latlon[0].trim();
        lon = latlon[1].trim(); 
        
        userLocation = new Map<String, Double>{
            'latitude' =>  decimal.valueOf(lat),
            'longitude' =>  decimal.valueOf(lon)
        };
        
        accounts = new List<Account>(); 

        
        //Limited to 99 Results, Update LIMIT code Lines to change results ratio
        
         // SOQL query to get Accounts
        String queryString1 = 
            'SELECT Id, Name, Type, Phone, BillingLatitude,BillingLongitude,BillingStreet, BillingCity, BillingState ' +
            'FROM Account ' +
            'WHERE BillingLatitude != null AND BillingLongitude != null ' + 
            'ORDER BY DISTANCE(BillingAddress, GEOLOCATION('+lat+','+lon+'), \'mi\') NULLS LAST ' +
            'LIMIT 60';

            
        // run query
        accounts = database.Query(queryString1);
        
        if (0 == accounts.size()) 
            System.debug('No results. Query: ' + queryString1);
         
                 
        return null;
    }
    
    //This method is a copy of findNearby() method with the only difference that current
    //location is calculated on the basis of street, city, and state field on the UI
    public PageReference findbyNewCurrentLocation() {
        try{
            String lat, lon;
                      
            accounts = new List<Account>(); 

            currentPosition = CusLat +','+ CusLong;
            lat = CusLat.trim();
            lon = CusLong.trim();

            userLocation = new Map<String, Double>{
                'latitude' =>  decimal.valueOf(lat),
                'longitude' =>  decimal.valueOf(lon)
            };
           
       
            //Limited to 99 Results, Update LIMIT code Lines to change results ratio
            
             // SOQL query to get Accounts
            String queryString1 = 
                  'SELECT Id, Name, Type, Phone,BillingLatitude,BillingLongitude,BillingStreet, BillingCity, BillingState '+ 
                'FROM Account ' +
                'WHERE BillingLatitude !=null AND BillingLongitude !=null ' + 
                'ORDER BY DISTANCE(BillingAddress, GEOLOCATION('+lat+','+lon+'), \'mi\') NULLS LAST ' + 
                'LIMIT 60'; 
  
            // run query
            accounts = database.Query(queryString1);
            
            
            if (0 == accounts.size()) 
                System.debug('No results. Query: ' + queryString1); 
                    
            return null;
        }
        catch(exception e){ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, Label.ERR_Location_Not_found)); system.debug('Error'+ e);return null;}
        
   
        
    }
    
    //returns location data on the condition of whether street field is empty or not on UI
    public PageReference redirectByLocation(){
     
        
        //if(street !='' && city != '' && state != ''){
        if(!String.IsBlank(street) && !String.IsBlank(city) && !String.IsBlank(state) && !String.IsBlank(country)){
            return findbyNewCurrentLocation();
        }
        else{
            return findNearby();
        }
    }
    
    
    
}
Note: Label is used for showing error message.

Page View:



Sunday, 26 November 2017

Lightning Data Service Basics: Handle Record Changes and Errors

Lightning Data Service Basics: Handle Record Changes and Errors

accEdit Component Code


<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId" access="global" >
<aura:attribute name="record" type="Object" 
      description="The record object to be displayed"/>
    <aura:attribute name="simpleRecord" type="Object" 
      description="A simplified view record object to be displayed"/>
    <aura:attribute name="recordSaveError" type="String" 
      description="An error message bound to force:recordData"/>
    
    <force:recordData aura:id="accountRecord"
        layoutType="FULL"
        recordId="{!v.recordId}"
        targetError="{!v.recordSaveError}"
        targetRecord="{!v.record}"
        targetFields="{!v.simpleRecord}"
        recordUpdated="{!c.recordUpdated}"
        fields="Name"
        mode="EDIT"/>
    
    <!-- Display an editing form -->
        <div class="Record Details">
            <lightning:card iconName="action:edit" title="Edit Account">
                <div class="slds-p-horizontal--small">
                    <lightning:input label="Account Name" value="{!v.accountRecord.Name}"/>
                    <br/>
                    <lightning:button label="Save Account" variant="brand" onclick="{!c.handleSaveRecord}" />
                </div>
            </lightning:card>
        </div>
    <!-- Display Lightning Data Service errors, if any -->
    <aura:if isTrue="{!v.recordSaveError}">

        <div class="recordError">
            {!v.recordSaveError}</div>
    </aura:if>
</aura:component>

accEdit Controller Component


({
    recordUpdated: function(component, event, helper) {
        var eventParams = event.getParams();
        if(eventParams.changeType === "CHANGED") {
            // get the fields that are changed for this record
            var changedFields = eventParams.changedFields;
            //console.log('Fields that are changed: ' + JSON.stringify(changedFields));
            // record is changed so refresh the component (or other component logic)
            var resultsToast = $A.get("e.force:showToast");
            resultsToast.setParams({
                "title": "Saved",
                "message": "The record was updated."
            });
            resultsToast.fire();
        } else if(eventParams.changeType === "LOADED") {
        } else if(eventParams.changeType === "REMOVED") {
           
        } else if(eventParams.changeType === "ERROR") {
         
        }
    },
    handleSaveRecord: function(cmp, event, helper) {
        cmp.find("accountRecord").saveRecord($A.getCallback(function(saveResult) {
            if (saveResult.state === "ERROR") {
                var errMsg = "";
                for (var i = 0; i < saveResult.error.length; i++) {
                    errMsg += saveResult.error[i].message + "\n";
                }
                cmp.set("v.recordSaveError", errMsg);

            } else {
                cmp.set("v.recordSaveError", "");

            }
        }));}
    
})

Note:If you keep getting this error "The 'accEdit' Lightning Component JS Controller does not appear to be setting 'v.recordSaveError' with an error message" then make sure in handleSaveRecord function you are using "cmp" not any other name.

Wednesday, 22 November 2017

Lightning Data Service Basics: Manipulate Records with force:recordData

Lightning Data Service Basics: Manipulate Records with force:recordData

Manipulation of records with forcerecordData featsure. You can do insert update and delete using this.

accDisplay Component Code


<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId" access="global" >
<aura:attribute name="record" type="Object" 
      description="The record object to be displayed"/>
    <aura:attribute name="simpleRecord" type="Object" 
      description="A simplified view record object to be displayed"/>
    <aura:attribute name="recordError" type="String" 
      description="An error message bound to force:recordData"/>
    
    <force:recordData aura:id="accountRecord"
        layoutType="FULL"
        recordId="{!v.recordId}"
        targetError="{!v.recordError}"
        targetRecord="{!v.record}"
        targetFields ="{!v.simpleRecord}"
        mode="VIEW"/>
    <!-- Display a lightning card with details about the record -->
    <div class="Record Details"> 
        <lightning:card iconName="standard:account" title="{!v.accountRecord.Name}" >
            <div class="slds-p-horizontal--small">
                <p class="slds-text-heading--small">
                    <lightning:formattedText title="Name" value="{!v.accountRecord.Name}" /></p>
                <p class="slds-text-heading--small">
                    <lightning:formattedText title="Industry" value="{!v.accountRecord.Industry}" /></p>
                <p class="slds-text-heading--small">
                    <lightning:formattedText title="Description" value="{!v.accountRecord.Description}" /></p>
                <p class="slds-text-heading--small">
                    <lightning:formattedPhone title="Phone" value="{!v.accountRecord.Phone}" /></p>
            </div>
        </lightning:card>
    </div>
        
    <!-- Display Lightning Data Service errors, if any -->
    <aura:if isTrue="{!not(empty(v.recordError))}">
        <div class="recordError">
            {!v.recordError}</div>
    </aura:if>
</aura:component>

accEdit Component Code


<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId" access="global" >
<aura:attribute name="record" type="Object" 
      description="The record object to be displayed"/>
    <aura:attribute name="simpleRecord" type="Object" 
      description="A simplified view record object to be displayed"/>
    <aura:attribute name="recordError" type="String" 
      description="An error message bound to force:recordData"/>
    
    <force:recordData aura:id="accountRecord"
        layoutType="FULL"
        recordId="{!v.recordId}"
        targetError="{!v.recordError}"
        targetRecord="{!v.record}"
        targetFields ="{!v.simpleRecord}"
        fields="Name"
        mode="EDIT"/>
    
    <!-- Display an editing form -->
        <div class="Record Details">
            <lightning:card iconName="action:edit" title="Edit Account">
                <div class="slds-p-horizontal--small">
                    <lightning:input label="Account Name" value="{!v.accountRecord.Name}"/>
                    <br/>
                    <lightning:button label="Save Account" variant="brand" onclick="{!c.handleSaveRecord}" />
                </div>
            </lightning:card>
        </div>
</aura:component>

accEdit Controlller Js Code


({
    handleSaveRecord: function(component, event, helper) {
        component.find("accountRecord").saveRecord($A.getCallback(function(saveResult) {
            if (saveResult.state === "SUCCESS" || saveResult.state === "DRAFT") {
                console.log("Save completed successfully.");
            } else if (saveResult.state === "INCOMPLETE") {
                console.log("User is offline, device doesn't support drafts.");
            } else if (saveResult.state === "ERROR") {
                console.log('Problem saving record, error: ' + 
                           JSON.stringify(saveResult.error));
            } else {
                console.log('Unknown problem, state: ' + saveResult.state + ', error: ' + JSON.stringify(saveResult.error));
            }
        }));}
})

Monday, 20 November 2017

Lightning Components Basics: Connect to Salesforce with Server Side Controllers

Lightning Components Basics: Connect to Salesforce with Server Side Controllers

Campaign List Component Code


<aura:component controller="CampingListController"
implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,force:lightningQuickAction" access="global" >
<aura:handler name="init" action="{!c.doInit}" value="{!this}"/>
    

    <aura:attribute name="items" type="Camping_Item__c[]"/>
    <aura:attribute name="newItem" type="Camping_Item__c" default="{ 'sobjectType': 'Camping_Item__c',
                        'Name': '',
                        'Quantity__c': 0,                                          
                        'Price__c': 0,
                        'Packed__c': false }"/>
    <!--<ol>
        <li>Bug Spray</li>
        <li>Bear Repellant</li>
        <li>Goat Food</li>
    </ol>  -->
    <!-- PAGE HEADER -->
    
    <c:campingHeader />

    <lightning:layout >
        <lightning:layoutItem padding="around-small" size="6">
<div aria-labelledby="newcampaignform">
                <fieldset class="slds-box slds-theme--default slds-container--small">
    <legend id="newcampaignform" class="slds-text-heading--small slds-p-vertical--medium">
                    Add Campaign List
                    </legend> 
                    <form class="slds-form--stacked">
                        <lightning:input aura:id="campaignform" label="Campaign Item Name"
                         name="campaignitemname"
                         value="{!v.newItem.Name}"
                         required="true"/>
                        <lightning:input type="number" aura:id="expenseform" label="Quantity"
                         name="campaignitemprice"
                         min="1"
                         formatter="number"
                         step="0.1"
                         value="{!v.newItem.Quantity__c}"
                         messageWhenRangeUnderflow="Enter quantity that's at least 1."/>
<lightning:input type="number" aura:id="expenseform" label="Price"
                         name="campaignitemprice"
                         min="0.1"
                         formatter="currency"
                         step="0.01"
                         value="{!v.newItem.Price__c}"
                         messageWhenRangeUnderflow="Enter an amount that's at least $0.10."/>
<lightning:input type="checkbox" aura:id="expenseform" label="Packed?" 
                         name="expreimbursed"
                         checked="{!v.newItem.Packed__c}"/>
<lightning:button label="Create Camping" class="slds-m-top--medium"
                                 variant="brand" onclick="{!c.clickCreateItem}"/>
                    </form>
                </fieldset>
            </div>
            
</lightning:layoutItem>
    </lightning:layout>
    <c:campingHeader />

<div class="slds-card slds-p-top--medium">
        <header class="slds-card__header">
            <h3 class="slds-text-heading--small">Items</h3>
        </header>
         
        <section class="slds-card__body">
            <div id="list" class="row">
                <aura:iteration items="{!v.items}" var="items">
                    <c:campingListItem item="{!item}"/>
                </aura:iteration>
            </div>
        </section>
    </div>

</aura:component>


CampaignListController Code


({
     // Load expenses from Salesforce
    doInit: function(component, event, helper) {
    
        // Create the action
        var action = component.get("c.getItems");
    
        // Add callback behavior for when response is received
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                component.set("v.items", response.getReturnValue());
            }
            else {
                console.log("Failed with state: " + state);
            }
        });
    
        // Send action off to be executed
        $A.enqueueAction(action);
    },
    
clickCreateItem: function(component, event, helper) {
        var validCamping = component.find('campingform').reduce(function (validSoFar, inputCmp) {
            // Displays error messages for invalid fields
            inputCmp.showHelpMessageIfInvalid();
            return validSoFar && inputCmp.get('v.validity').valid;
        }, true);
if(validCamping){
var addItm = event.getParam("item");
            helper.createItem(component, addItm);
            var newCampingItem = component.get("v.newItem");
            var campings = component.get("v.items");
            var item = JSON.parse(JSON.stringify(newCampingItem));
            campings.push(item);
            component.set("v.items",campings);
            component.set("v.newItem",{ 'sobjectType': 'Camping_Item__c','Name': '','Quantity__c': 0,
                                           'Price__c': 0,'Packed__c': false });
        }


}
})

Campaign  List Helper Code

({
   
    createItem : function(component, campaign) {
        var action = component.get("c.saveItem");
        action.setParams({
            "item": campaign
        });
        action.setCallback(this, function(response){
            var state = response.getState();
            if (state === "SUCCESS") {
                var expenses = component.get("v.items");
                expenses.push(response.getReturnValue());
                component.set("v.items", expenses);
            }
        });
        $A.enqueueAction(action);
    },
})

Sunday, 19 November 2017

Lightning Components Basics: Input Data using Forms

Lightning Components Basics: Input Data using Forms

Campaign Header Component Code:


<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,force:lightningQuickAction" access="global" >
    <lightning:layout class="slds-page-header slds-page-header--object-home">
        <lightning:layoutItem>
            <lightning:icon iconName="action:goal" alternativeText="My Camping List"/>
        </lightning:layoutItem>
        <lightning:layoutItem padding="horizontal-small">
            <div class="page-section page-header">
                <h1 class="slds-text-heading--label">Campaign List</h1>
                <h2 class="slds-text-heading--medium">My Campaign List</h2>
            </div>
        </lightning:layoutItem>
    </lightning:layout>

</aura:component>

Campaign List Component Code:


<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,force:lightningQuickAction" access="global" >
<aura:attribute name="items" type="Camping_Item__c[]"/>
    <aura:attribute name="newItem" type="Camping_Item__c" default="{ 'sobjectType': 'Camping_Item__c',
                        'Name': '',
                        'Quantity__c': 0,                                          
                        'Price__c': 0,
                        'Packed__c': false }"/>
    <!--<ol>
        <li>Bug Spray</li>
        <li>Bear Repellant</li>
        <li>Goat Food</li>
    </ol>  -->
    <!-- PAGE HEADER -->
    
    <c:campingHeader/>

    <lightning:layout>
        <lightning:layoutItem padding="around-small" size="6">
<div aria-labelledby="newcampaignform">
                <fieldset class="slds-box slds-theme--default slds-container--small">
    <legend id="newcampaignform" class="slds-text-heading--small slds-p-vertical--medium">
                    Add Campaign List
                    </legend> 
                    <form class="slds-form--stacked">
                        <lightning:input aura:id="campaignform" label="Campaign Item Name"
                         name="campaignitemname"
                         value="{!v.newItem.Name}"
                         required="true"/>
                        <lightning:input type="number" aura:id="expenseform" label="Quantity"
                         name="campaignitemprice"
                         min="1"
                         formatter="number"
                         step="0.1"
                         value="{!v.newItem.Quantity__c}"
                         messageWhenRangeUnderflow="Enter quantity that's at least 1."/>
<lightning:input type="number" aura:id="expenseform" label="Price"
                         name="campaignitemprice"
                         min="0.1"
                         formatter="currency"
                         step="0.01"
                         value="{!v.newItem.Price__c}"
                         messageWhenRangeUnderflow="Enter an amount that's at least $0.10."/>
<lightning:input type="checkbox" aura:id="expenseform" label="Packed?" 
                         name="expreimbursed"
                         checked="{!v.newItem.Packed__c}"/>
<lightning:button label="Create Camping" class="slds-m-top--medium"
                                 variant="brand" onclick="{!c.clickCreateItem}"/>
                    </form>
                </fieldset>
            </div>
            
</lightning:layoutItem>
    </lightning:layout>
    <c:campingHeader/>

<div class="slds-card slds-p-top--medium">
        <header class="slds-card__header">
            <h3 class="slds-text-heading--small">Items</h3>
        </header>
         
        <section class="slds-card__body">
            <div id="list" class="row">
                <aura:iteration items="{!v.items}" var="items">
                    <c:campingListItem item="{!item}"/>
                </aura:iteration>
            </div>
        </section>
    </div>

</aura:component>

Campaign List Controller Code


({
clickCreateItem: function(component, event, helper) {
        var validCamping = component.find('campingform').reduce(function (validSoFar, inputCmp) {
            // Displays error messages for invalid fields
            inputCmp.showHelpMessageIfInvalid();
            return validSoFar && inputCmp.get('v.validity').valid;
        }, true);
if(validCamping){

            var newCampingItem = component.get("v.newItem");
            var campings = component.get("v.items");
            var item = JSON.parse(JSON.stringify(newCampingItem));
            campings.push(item);
            component.set("v.items",campings);
            component.set("v.newItem",{ 'sobjectType': 'Camping_Item__c','Name': '','Quantity__c': 0,
                                           'Price__c': 0,'Packed__c': false });
        }

}
})

Campaign List Item Code


<aura:component implements="flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId" access="global" >
<aura:attribute name="item" type="Camping_Item__c" required="true"/>
<aura:attribute name="Name" type="String"/>
    <p>Hello! {!v.item.Name}</p>
<aura:attribute name="Price" type="String"/>
    <p>Price:
<lightning:formattedNumber value="{!v.item.Price__c}" style="currency"/>
    </p>
    <aura:attribute name="Quantity" type="String"/>
    <p>Price:
<lightning:formattedNumber value="{!v.item.Quantity__c}" style="Number"/>
    </p>
    <aura:attribute name="Packed" type="String"/>
    <p>
        <lightning:input type="toggle"                           
                         label="Packed?"                          
                         name="Packed"                        
                         checked="{!v.item.Packed__c}" />
     </p>
    <div>
<lightning:button label="Packed!"
            onclick="{!c.packItem}"/>
    </div>
    
</aura:component>

Happy Coding !!!

Thursday, 21 September 2017

Salesforce Trailhead : Lightning Experience Rollout Specialist

Salesforce Trailhead : Lightning Experience Rollout Specialist

Before I start sharing information about superbadge. I am sharing a link of an unmanaged package. After installing this unmanaged package in your org you will be able to clear App builder and Suggestion Box badges which are pre requisite to unlock this superbadge.

Note: Before installing this package enable lightning, domain and account teams in your org.

https://login.salesforce.com/packaging/installPackage.apexp?p0=04t28000000bALD

Challenge 1:

Login your org go in Setup--> Account--> Button Links and Actions-->Click on account tab
Override with Visualforce page Account Tab

Go in Setup--> Home Page Layouts --> DE Default--> Edit--> Add Destination Success Registration component.

Create Two users with below screen shot information.

Create two Permission sets
LightningExperiencePilotSales 
LightningExperiencePilotCommunications

Lightning Experience Pilot Sales will have only lightning user permission as per screen shot.

Lightning Experience Pilot Communications will have lightning user permission as well as not allow to switch between lightning and classic. As per screen shot.


Setup-->Lightning Experience Migration Assistant-->Get Started-->Next-->Tab Check Readiness-->Click Button check Readiness. This will send a report to you.

Setup-->Monitor-->Optimizer--> Launch . This will send you a report.

Challenge 2:

It is a quiz and below are screen shots of it.









Challenge 3:

For your Visulaforce page of account override code with this one.

<apex:page standardStylesheets="false" standardController="Account" recordSetVar="accounts" tabStyle="account">
<apex:slds />

    <div class="slds-scope">
        <apex:pageBlock >
 
            <apex:pageBlockTable value="{!accounts}" var="a"  styleClass="slds-table" >
                <apex:column headerValue="{!$ObjectType.Account.Fields.Name.Label}">
                    <apex:outputLink value="{!URLFOR($Action.Account.View, a.id)}">{!a.name}</apex:outputLink>
                </apex:column>
             
            </apex:pageBlockTable>
        </apex:pageBlock>
    </div>

</apex:page>


For documents, download Destination Success image from documents and upload it in Files and make it available for users.

Challenge 4:
Create 2 account record types and 2 compact layouts and assign them to appropriate profiles.

Setup--> Path Setting--> Create New Path with below given values. Make sure that you add guidance for success values too and activate path.

**Stage****Fields****Guidance for Success**
QualificationAccount Name, Opportunity Name, Description- Ask the buyer about their favorite footwear.
- Share the most appropriate sections of our catalog.
- Share testimonials from fashionistas.
- Present our most popular ranges to the buyer.
Needs AnalysisExpected Revenue, Main Competitor(s), Next Step, Probability(%)- Ask about the maximum price that the buyer is willing to spend.
- Ask: "Is there anything else you'd like to know or need to proceed with the purchase?"
- Set a date for follow-up.
Negotiation/ReviewAmount, Quantity, Stage- If the buyer seems likely to walk away from the deal, discount 5%.
- Follow up on questions or needs from the buyer.
Closed LostClose Date, Next Step- Create post-mortem notes.
- Set a follow-up date for 1 year from today.
Closed WonClose Date, Delivery/Installation Status- Celebrate in Chatter!
- Check with the Accounts team for payment processing.

Challenge 5:
It is a quiz and below are screen shots of it.









Challenge 6:


  1. Create the App page with name : Key Sales Data
  2. select any template 
  3. add Filter List component from left side component list.
    1. In the Filter List Component select Account and for filter value select 'New This Week'
  4. Add Recent Items component, in object select : opportunity,Lead,Contacts.
  5. In Actions select Log call and New Opportunity component.

Create a lightning App named it Communication. Use Account, Chatter, reports and Dashboards tab only and assign to custom sales profile.

Create a lightning page as record page on group object and use following items on it.
Page name will be Group Record Page.
Highlights Panel
Record Detail
Recommendations
Trending Topic
Activate this page and assign it as Org Default.
Create a chatter group with name All Lusso Scarpe Employees and Description as This group is for all Lusso Scarpe employees to collaborate and receive company announcements. All employees can join the group. Make it a public group.

Challenge 7:

Create a report name Opportunities Pipeline.  Use opportunity and then group by report with stage--> Type--> Probability and sum account field.
No filters all opportunities with all time.
Add funnel chart with stage and record count.
Schedule report-John Wiseman wants to review the Opportunities Pipeline report each week. Set it up so he receives refreshed report results by email every Monday at 8:00am.


Add this chart in lightning page on account object you created in app builder badge. Name this chart as Opps by Stage.
Create a Dashboard name Sales Operations and a dashboard folder name Sales Operations. Make dashboard as logged in user save it sales operation folder and add filter on opportunity stage.
  • Horizontal bar chart called Opp Probability by Stage, showing probability by stage
  • Gauge called Sum of Opportunity Amount, showing the sum of revenue generated by all opportunities
  • Donut called New and Existing Business, showing the sum of revenue generated by new and existing business.

Challenge 8:

Enable knowledge lightning. Before searching for knowledge make sure that you are a Knowledge user. Go on your user record and check the knowledge user checkbox.
Note: Make sure that you enable lightning knowledge.
Now go Setup--> Knowledge Object Setup-->New fields and relationship.
Create a field Question Text(255)
Create a field Answer Rich Text Area(32768)
Now go on Record type--> New.
Create 3 record types Community, FAQ, Product.
Now go on Pagelayouts --> New.
Create 3 pagelayouts faqpagelayout, productpagelayout, and communitypagelayout and assign it to respective layouts.
Add question answer and record type field on above 3 layouts.
Go in Salesforce lightning. Click setup and then go app manager. click on new lightning app.
Create a lightning App and named it Lightning Knowledge.
In utility bar add history component.
Selected tabs will case accounts and contacts.
Go in lightning app builder--> New.
Create a new home page name Org Home.
Add Chatter feed, Today's Tasks, upcoming events, recent items, smart scope recent records.
Activate page and assign it as default for Org.
Note: in recent items component make sure that you have account contact and cases are added as tabs.

Challenge 9:

It is a quiz and below are screen shots of it.


Challenge 10:

Create a lightning component "DestinationSuccess".
In component put this code.

<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,force:lightningQuickAction" access="global" >

 <aura:attribute name="myURL" type="String" default="https://www.salesforce.com/campaign/destination-success/overview/"/>

    

    <ui:outputURL value="{!v.myURL}" label="DestinationSuccess" target="_blank"/>
    
</aura:component>
in controller write this code.
({
    navigate : function(component, event, helper) {
         var address = component.find("address").get("v.myURL");
        var urlEvent = $A.get("e.force:navigateToURL");
    urlEvent.setParams({
      "url": 'https://www.salesforce.com/campaign/destination-success/overview/' + address
    });
    urlEvent.fire();
    }
})
Switch org to lightning if you are not alredy in it. 
Go in setup--> App Manager--> App--> Sales lightning app---> Edit.
Add newly created destination success component in utility bar.
Go on Object manager --> Case --> Button links and Action--> New Action.
Create an action with name UpsellCrosssellOpportunity. Use lightning component create opportunity.
Add this link on case layout.

Create a report with name Campaign Influence Lightning.
Use already built in Campaign Influence report type.
Make it a summary report. Show all campaign. Add filter campaign name equals blank and campaign id equals blank.
Sum Opportunity amount and group by campaign name.
Add following columns as shown in screen shot.

Copy this report id and keep it we will use it later.
Go in Setup --> Object Manager --> Campaign ---> Button links and Action--> New Button or link.
Create a detail page link and name it View Lightning Campaign Influence Report.
Add below Url
/one/one.app#/sObject/00O7F000003ynrX/view?&fv0={!Campaign.Name}&fv1={!Campaign.Id}
replace 00O7F000003ynrX idwith your report id.
Add this link on page.

Now you are done. You successfully competed Superbagde :)

Friday, 15 September 2017

Lightning - Change quote status to approve

Lightning - Change Quote Status to Approve

Few days back i got chance to work on lightning. Al thought it is very small request but wanted to share my code here.

Business Requirement

On Quote object( Custom Object due to some business requirements) clients needs a button called "Approve". so once this button is clicked quote status should be changed to approve. Doing this in classic is a piece of cake but lightning is a bit new for me, so i have to put some time to get familiar with Aura framework.

Solution

Created a lightning component and use it on the object specific action to show a button in lightning.

Handler Code

({ doInit : function(component, event, helper) {
    var qid = component.get("v.recordId");
        var action = component.get("c.saveQuote");
        action.setParams({"QID": qid});

        action.setCallback(this, function(response) {
            var state = response.getState();
            if(component.isValid() && state == "SUCCESS"){
                var c = response.getReturnValue();
                component.set("v.quote", c);
                $A.get("e.force:closeQuickAction").fire();
                $A.get('e.force:refreshView').fire();
            }

            else {
                console.log('There was a problem : '+response.getError());
            }
        });
        $A.enqueueAction(action);


} 
})

Class Code

public class ApproveQuoteController 
{
public static FConnect__Service_Quote__c saveQuote(Id QID){
    FConnect__Service_Quote__c Cquote= [select id,name,FConnect__Account__c,FConnect__Status__c,FConnect__Contact__c,FConnect__Site__c,FConnect__Problem__c
                   from FConnect__Service_Quote__c where id=:QID limit 1];
                       
        Cquote.FConnect__Status__c = 'Approved';

        update Cquote;
        return Cquote;
    }
}

Component Code

<aura:component controller="ApproveQuoteController" implements="force:appHostable,flexipage:availableForAllPageTypes,force:hasRecordId,force:lightningQuickAction" access="GLOBAL" >
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />

</aura:component>