Skip to main content

Hi all,

 

Switching from Classic to Lightning and trying to replicate a button (javascript) feature. 

 

I have a custom object with a button called 'CheckIn', and would like to update a record with the current user geolocation and datetime. I have my componet and controller whereby I can get the existing record ID to display in the componet.  I can get the current location from browser and system datetime, but can't get all to display in the component window in order to update the record. I need to combine the recordid from' doInit' and lat/lng from 'getlocation' to display in lightning component.

 

The final piece in order to make this seamless to user is hopefully update the record automatically, without user have to click a save button.  Is this possible in Lightning?  In Classic, the button is javascript and record updates when Check-In is clicked.   

 

Component

 

<aura:component controller="user_current_location" implements="force:lightningQuickAction,force:hasRecordId" access="global">

<aura:attribute name="visit" type="Visit__c"/>

  <aura:handler name="init" value="{!this}" action="{!c.doInit}" /> 

   <aura:handler name="init" value="{!this}" action="{!c.getLocation}" />

    

    <div class="slds-page-header" role="banner">

    <h1 class="slds-page-heading--label">Check-In Update</h1>

    </div>

    

    <lightning:input type="text" name="id" aura:id="id" label="Name" value="{!v.visit.Name}" ></lightning:input>

    <lightning:input type="number" name="latitude" aura:id="latitude" label="Check-In Latitude" value="{!v.visit.Check_in_GeoLocation__latitude__s}" ></lightning:input>

    <lightning:input type="text" name="longitude" aura:id="longitude" label="Check-In Longitude" value="{!v.text}"></lightning:input>

    <lightning:input type="date" name="checkindate" aura:id="checkindate" label=" {!v.visit.Check_in_Date_Time__c}" ></lightning:input>

</aura:component>

 

Controller

({

  doInit : function(component, event, helper) {

        console.log('init');

         var action = component.get("c.getVisit");   

         action.setParams({"visitId" : component.get("v.recordId")});

         action.setCallback(this, function(response) {

         console.log(response.getReturnValue());

         component.set("v.visit", response.getReturnValue());

         });

 

        $A.enqueueAction(action);

    },

 

   getLocation: function(component, event, helper){

   if (navigator.geolocation) {

       navigator.geolocation.getCurrentPosition(function(position) {

           var lat = position.coords.latitude;

           var lon = position.coords.longitude;

 //           alert(lat);

 //           alert(lon);

            component.set("v.lat", lat ) ;

}

})

 

apexc

public with sharing class user_current_location {

 

    @AuraEnabled

    public static Visit__c getVisit(Id visitId){

 

        return [SELECT Name, Id, Check_in_GeoLocation__latitude__s, Check_in_GeoLocation__longitude__s, Check_in_Date_Time__c FROM Visit__c

        WHERE Id = :visitId ];

    }

}

 

Thank you.

1 条评论
0/9000