check-In and Checkout feature along with Geo location and activity time in salesforce

Requirement:
Let\’s Suppose there is manufacturer industry and in this industry, there are 40 Sales Rep who do Sales activity for the company. so Each Sales Rep has there PJP( Permanent journey Plan). In this journey, each Sales Rep has to go and follow the Route Planned from start point to end point for Order Booking from Retailer.

Problem:
Regional Sales Manager would like to track the position(geolocation) of the Sales Rep. By what time he has started the journey and where has ended with the order booking. as well as there checkIn Time and CheckOut Time with geolocation.

Solution:
In my solution, I would like to show how CheckIn and CheckOut will work through visualforce page.
This checkIn , Checkout feature is to tag the Geolocation of the user.

Solution Step by Step:

1. Create Contact Check In Object API Name Contact_Check_In__c
2. Define following field on this Object

Address Address__c Text Area(255)
CheckIn Date Time CheckIn_Date_Time__c Date/Time
Checkout Address Checkout_Address__c Text Area(255)
Checkout Date time Checkout_Date_time__c Date/Time
Contact Contact__c Lookup(Contact)
Contact Check In Name Name Auto Number
GeoLocation GeoLocation__c Geolocation
Opportunity Opportunity__c Lookup(Opportunity

 

  • Create a VF Page with the following code:

VF Page Name: OpportunityCheckIn and following apex class: OpportunityCheckInController

<apex:page standardController=\"opportunity\" extensions=\"OpportunityCheckInController\" lightningStylesheets=\"true\">
  <apex:includeScript value=\"{!$Resource.gmap}\"/>
  <apex:slds />

  <input type=\"button\" value=\"Check In\" onclick=\"checkIn()\" 
class=\"slds-button slds-button_stateful .slds-is-selected slds-button_success\"/>
  <input type=\"button\" value=\"Check Out\" onclick=\"checkOut()\" 
class=\"slds-button slds-button_stateful .slds-is-selected slds-button_destructive\"/>
  
  
      function checkIn(){
          
          var oppId=\'{!OppId}\';
          
          GMaps.geolocate({
          success: function(position) {
            alert(\'Doing Checkin for following lat: \' + position.coords.latitude +\' and long: \' + position.coords.longitude );
            
            var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
            var address=\'\';
            var geocoder = geocoder = new google.maps.Geocoder();
            geocoder.geocode({ \'latLng\': latlng }, function (results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    if (results[1]) {
                        address=results[1].formatted_address;
                                     Visualforce.remoting.Manager.invokeAction(
                            \'{!$RemoteAction.OpportunityCheckInController.
addCheckIns}\',oppId,position.coords.latitude,  position.coords.longitude, 
address,function(result, event) {console.log(result);alert(\'Check In Done.
 Successfully!\');}
                            );
                    }
                }
            });
            
           
  //alert(\'long\' + position.coords.longitude);
            
            map.setCenter(position.coords.latitude, position.coords.longitude);
          },
          error: function(error) {
            alert(\'Geolocation failed: \'+error.message);
          },
          not_supported: function() {
            alert(\"Your browser does not support geolocation\");
          },
          always: function() {
            alert(\"Done!\");
          }
        });
        
      }
      
      function checkOut(){
          
          var oppId=\'{!OppId}\';
          
          GMaps.geolocate({
          success: function(position) {
            alert(\'Doing CheckOut for following lat: \' + position.coords.latitude +\' and long: \' + position.coords.longitude );
            
            var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
            var address=\'\';
            var geocoder = geocoder = new google.maps.Geocoder();
            geocoder.geocode({ \'latLng\': latlng }, function (results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    if (results[1]) {
                        address=results[1].formatted_address;
                                     Visualforce.remoting.Manager.invokeAction(
                            \'{!$RemoteAction.OpportunityCheckInController.addCheckOuts}\',oppId,position.coords.latitude,  position.coords.longitude, address,function(result, event) {console.log(result);alert(\'Check Out Done. Successfully!\');}
                            );
                    }
                }
            });
            
 
            //alert(\'long\' + position.coords.longitude);
            
            map.setCenter(position.coords.latitude, 
                                     position.coords.longitude);
          },
          error: function(error) {
            alert(\'Geolocation failed: \'+error.message);
          },
          not_supported: function() {
            alert(\"Your browser does not support geolocation\");
          },
          always: function() {
            alert(\"Done!\");
          }
        });
        
      }

   https://maps.googleapis.com/maps/api/js?key=AIzaSyBqqej11sosokXGaQTk_-Zw9AIXMVkXoAE&callback=loadMap
  <div id=\"map\"/>
</apex:page>
Controller:
public class OpportunityCheckInController {

    public Id OppId{get;set;}

    public OpportunityCheckInController(ApexPages.StandardController controller) {
     OppId=\'0067F000006Qd3NQAS\';
     OppId=controller.getRecord().Id;
    }


    @RemoteAction
    public static void addCheckIns(string OppId,decimal lat, decimal lng,string address){
        Contact_Check_In__c ConCheckIn=new Contact_Check_In__c();
        ConCheckIn.CheckIn_Date_Time__c=system.now();
        ConCheckIn.GeoLocation__latitude__s=lat;
        ConCheckIn.GeoLocation__longitude__s=lng;
        //ConCheckIn.Contact__c=ContId;
        ConCheckIn.Opportunity__c=OppId;
        ConCheckIn.Address__c=address;
        insert ConCheckIn;
    }
    
    @RemoteAction
    public static void addCheckOuts(string OppId,decimal lat, decimal lng,string address){
        Contact_Check_In__c ConCheckIn=new Contact_Check_In__c();
        ConCheckIn.Checkout_Date_time__c=system.now();
        ConCheckIn.GeoLocation__latitude__s=lat;
        ConCheckIn.GeoLocation__longitude__s=lng;
        //ConCheckIn.Contact__c=ContId;
        ConCheckIn.Opportunity__c=OppId;
        ConCheckIn.Checkout_Address__c=address;
        insert ConCheckIn;
    }
    

Now embed this VF page on your Opportunity Page. your opportunity Page will look this.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome content in your inbox, every month.

We don’t spam! Please check your mail for confirmation.

Leave a Comment

Your email address will not be published. Required fields are marked *

Select Language »