function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Suneel#8Suneel#8 

anchor tag not working in Desktop or Salesforce1

I am dispalying marker for every near by account and below anchor tag should navigate to the respective account upon clicking it.I have used the code in Workbook,but it works neither in Desktop nor in Salesforce1.Please help

try{
            if(sforce.one){
                 accountNavUrl = 'javascript:sforce.one.navigateToSObject(\'' + account.Id + '\')';
             }
} catch(err) {
             console.log(err);
             var dId=account.Id;
             accountNavUrl= '\\' + account.Id;
}
console.log('-->'+accountNavUrl );
var content='<a href src="'+accountNavUrl +'" >'+account.Name+ '</a><br/>'+account.BillingStreet +'<br/>' + account.BillingCity +'<br/>' + account.BillingCountry;

Below is the whole code
<apex:page standardController="Account" extensions="AccountMapExt" sidebar="false" showHeader="false">
  <!-- Begin Default Content REMOVE THIS -->
    <apex:includeScript value="https://maps.googleapis.com/maps/api/js?sensor=false" />  

  <style>
        html, body { height: 100%; }    
        .page-map, .ui-content, #map-canvas { width: 100%; height:100%; padding: 0; }
        #map-canvas { height: min-height: 100%; }
  </style>
  <script>
      var lat;
      var lon;
        function initialize(){ 
            if(navigator.geolocation){
                navigator.geolocation.getCurrentPosition(
                    function(position){
                        lat=position.coords.latitude;
                        lon=position.coords.longitude;
                        Visualforce.remoting.Manager.invokeAction('{!$RemoteAction.AccountMapExt.getNearby}',lat, lon,
                              function(result, event){
                                    if (event.status) {
                                           console.log('->'+lat+' '+lon+' '+result);
                                           createMap(lat, lon, result);
                                    } else if (event.type ==='exception') {
                                            //exception case code
                                    } else {
                                    }
                                    },
                                    {escape: true}
                        );                        
                    }
                );
            } 
          
        }    
        function createMap(lat,lon,accounts){
                console.log('Calling map '+ lat+' '+lon);
                var currentPosition = new google.maps.LatLng(lat,lon);            
                var mapDiv = document.getElementById('map-canvas');
                var map = new google.maps.Map(mapDiv, {
                    center: currentPosition, 
                    zoom: 13,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                });
                var positionMarker = new google.maps.Marker({
                    map: map,
                    position: currentPosition,
                    icon: 'https://maps.google.com/mapfiles/ms/micons/green.png'
                });
                
                for(var i=0; i<accounts.length;i++){
                    account= accounts[i];
                    console.log(accounts[i]);
                    setupMarker();
                }                
                
                function setupMarker(){
                    var accountNavUrl;
                    try{
                        if(sforce.one){
                            accountNavUrl = 'javascript:sforce.one.navigateToSObject(\'' + account.Id + '\')';
                            
                        }
                    } catch(err) {
                        console.log(err);
                        var dId=account.Id;
                        accountNavUrl= '\\' + account.Id;
                    }
                    console.log('-->'+accountNavUrl );
                    var content='<a href src="'+accountNavUrl +'" >'+account.Name+ '</a><br/>'+account.BillingStreet +'<br/>' + account.BillingCity +'<br/>' + account.BillingCountry;
                    //Create the callout that will pop up on the marker
                    var infowindow = new google.maps.InfoWindow({content: content});
                    //Place the marker
                    var marker = new google.maps.Marker({
                        map: map,
                        position: new google.maps.LatLng(account.BillingLatitude,account.BillingLongitude)}
                    );              

                    google.maps.event.addListener(marker, 'click', function(){
                        infowindow.open(map, marker);
                    });                          
                }  
                var mapBoundary = new google.maps.LatLngBounds();
                mapBoundary.extend(currentPosition);                    
         }       
         google.maps.event.addDomListener(window, 'load', initialize);           
  </script>

    <body style="font-family: Arial; border: 0 none;">
        <div id="map-canvas"></div>
    </body>    
  <!-- End Default Content REMOVE THIS -->
</apex:page>

 
Best Answer chosen by Suneel#8
Suneel#8Suneel#8
Thanks for sharing the idea Sandeep.I refactored the code as below and it worked!
var accountNavUrl;
                    try{
                        if(sforce.one){
                            accountNavUrl='<a href="javascript:sforce.one.navigateToSObject(\''+account.Id+'\');">';
                        }
                    } catch(err) {
                        console.log(err);
                        accountNavUrl='<a href="../' + account.Id + '" target="_parent">';                        
                    }
                    console.log('-->'+accountNavUrl );
                    var content=accountNavUrl+account.Name+ '</a><br/>'+account.BillingStreet +'<br/>' + account.BillingCity +'<br/>' + account.BillingCountry;

 

All Answers

sandeep sankhlasandeep sankhla
Hi suneel,

Please try with below code..
 
if( (typeof sforce.one != 'undefined') && (sforce.one != null) ){
                sforce.one.editRecord(recordId);
                
                //sforce.one.navigateToURL('/' + recordId);
                
            } else{
                window.location.href = urlWindow;                                           
            }
Please check with above and let me know if it helps you..

Thanks,
Sandeep
 
Suneel#8Suneel#8
Thanks for the quick reply Sandeep.I tried with your code.It is directly taking me to the Edit page after it launches Google maps without my intervention.But I want the navigation to happen upon clicking the link.Please let me know how to capture the navigation link in a variable like below so that I can use it in the anchor tag that can be dispayed in information window.Also I need to goto detail page not edit page.Strangely  I am not able to do this in Desktop also. window.location.href= '\\' + account.Id; doesn't alter the behaviour and page is simply refreshing .Please advise

accountNavUrl = 'javascript:sforce.one.navigateToSObject(\'' + account.Id + '\')';
var content='<a href src="'+accountNavUrl +'" >'+account.Name+ '</a><br/>'+account.BillingStreet +'<br/>' + account.BillingCity +'<br/>' + account.BillingCountry;
sandeep sankhlasandeep sankhla
Hi Suneel,

I have shared my old code which redirects to edit page, you can assign your urlk detail page url to redoirect..

for detail page you can use.
sforce.one.navigateToSObject(recordId);

 
sandeep sankhlasandeep sankhla
Hi Suneel,

You can easily customize it..onclickj of any link you can simply call one javascript function and there you can use above code to redirect
sforce.one.navigateToSObject(recordId);.

then it should work..

That was my old which I shared for just a reference so ...

Let me know if you have nay question..

Thanks,
Sandeep
Suneel#8Suneel#8
Thanks for sharing the idea Sandeep.I refactored the code as below and it worked!
var accountNavUrl;
                    try{
                        if(sforce.one){
                            accountNavUrl='<a href="javascript:sforce.one.navigateToSObject(\''+account.Id+'\');">';
                        }
                    } catch(err) {
                        console.log(err);
                        accountNavUrl='<a href="../' + account.Id + '" target="_parent">';                        
                    }
                    console.log('-->'+accountNavUrl );
                    var content=accountNavUrl+account.Name+ '</a><br/>'+account.BillingStreet +'<br/>' + account.BillingCity +'<br/>' + account.BillingCountry;

 
This was selected as the best answer
Suneel#8Suneel#8
Sandeep,

Though I have got this working with above approach,I am inclined to try your idea as well.I am able to implement your idea in Salesforce1 but it is not working in Desktop.Could you please check below code and suggest any corrections.window.location.href or the format of the input doesn't seems to work. Thanks for staying with me!
var content='<div id="myInfoWinDiv"><a href src="'+accountNavUrl +'" >'+account.Name+ '</a></div><br/>'+account.BillingStreet +'<br/>' + account.BillingCity +'<br/>' + account.BillingCountry;
                   
                    google.maps.event.addListener(infowindow,'domready',function(){
                        j$('#myInfoWinDiv').click(function() {
                            if(sforce.one){
                                javascript:sforce.one.navigateToSObject( account.Id );                            
                            }else{
                                //window.top.location = '/' + account.Id;
                                window.location.href= "/' + account.Id ";
                            }    
                        });
                    });

 
sandeep sankhlasandeep sankhla
Hi,

What you want exactly..for desktop you can simply use window.open...