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
Valentin F.Valentin F. 

Refresh leaflet map when changing record

When I change record, my map is not refreshing and I got the following error : 
'[Map container is already initialized.]'

The workaround I found was to put map as a global variable and then, in my init function :
if(map != null && map != undefined) {
   map.remove();
}
This way I've no more errors but I've to refresh twice the record page to get the map displaying. When I change page I get a blank map but it doesn't recreate.
User-added image
Thank you for helping me !
Best Answer chosen by Valentin F.
Valentin F.Valentin F.
Salesforce uses XHR technology to keep pages in memory ; let's talk about Leads for example.
Whenever you visit the list of leads, it keeps it in memory and just applies a css style display: none.
When you're on a lead, the others leads you've visited before and the leads' list still are present on the page you're on. Just using the display: none.

The problem with the map was that it was displayed but not in the active content. Each layout dispose of a class "one-content" (for each lead for example) and the one you see get the "visible" class too. This way, the map id was used everytime for a different lead and whenever I tried to retrieve it to display my map, it was displayed to the most "relevant" map id. However, sometimes the most relevant map id for Salesforce wasn't the one of the active layout and I couldn't see it even if it was displayed in another part of the page with display: none.
User-added image

The solution I found was to give an unique id, thus "map + currentRecordId". This way I could get the good id and display my map inside of it.