Skip to Content
Technical Articles
Author's profile photo Amshuman Aravind Sagar

Set Geo Coordinates using Update Interaction Contact (best record) BAdI in Marketing Cloud

Introduction

Using below steps you can add Longitude and Latitude to any contact before it is created in system.

The logic is ignored if the incoming payload already has longitude/latitude information in it.

the BAdI logic will read the Geo Location from the available free database openstreetmap.org based on the contact’s City and Country and will update the contact

 

Implementation

Go to marketing cloud

open app Custom Communication Scenario

go to outbound services, give description and service id like shown in below screenshot

save and publish

go to communication system and create a new communication system with system ID, Name, Host Name and Port like below.

make sure to give User for outbound communication as None

now go to Communication arrangement

create a new one with the scenario ID which was created in first stem

use the system created in above step

it ill select the user as None from communication system

and it will show the outbound service from scenario

now go to Custom fields and Logic App

go to tab custom Logic

click on + icon and create a new BADI

select the business context as Marketing: Contact and Corporate

and choose the Badi Update Interaction Contact(best record)

and create the badi with some meaningful description

1 use the communication scenario and outbound service to access the service

2 loop at the contact data available in BAdI and check if country is filled and longitude&Latitude are initial.

3 build the query by selecting city and country value from contact

4 send the request to service and receive the address fields as response

5 set contact’s longitude and latitude from the incoming response

DATA: lr_ic_root_new TYPE REF TO cuan_s_ce_ic_rt_badi.
DATA: ls_body TYPE string.
"create http client to access the outbound service
DATA(lo_client) = cl_ble_http_client=>create(
                    communication_scenario = 'YY1_API_GPS_OPENSTREETMAPS'
                    outbound_service       = 'YY1_APIOPENSTREETMAPS_REST').
"creation of the service request
DATA(lo_request) = cl_ble_http_request=>create( )->set_method( if_ble_http_request=>co_method-get ).
 
"process all updated interaction contacts
LOOP AT new_contact_root REFERENCE INTO lr_ic_root_new.
if lr_ic_root_new->country is not initial and lr_ic_root_new->latitude = 0 and lr_ic_root_new->longitude = 0.
*fetch the data on city level
  DATA(lv_query) = '?format=json' && '&city=' && lr_ic_root_new->city1 && '&country=' && lr_ic_root_new->country.
  "extend the uri by the specified query parameters
  lo_request->set_resource_extension( lv_query ).
  TRY.
      "send a request and receive a response"
      DATA(lo_response) = lo_client->send( lo_request  ).
    CATCH cx_ble_http_exception .
      lr_ic_root_new->yy1_street2_mps = 'exception'.
  ENDTRY.
  "get the body of the response
  ls_body = lo_response->get_body( ).
    ""lr_ic_root_new->yy1_street2_mps = ls_body .
    "find the appropreate coordinates from the answer
    lr_ic_root_new->latitude = substring_before( val = substring_after( val = ls_body sub = '"lat":"') sub = '"' ).
    lr_ic_root_new->longitude = substring_before( val = substring_after( val = ls_body sub = '"lon":"') sub = '"' ).
endif.
ENDLOOP.

 

How to Test the functionality

To test the feature, simple go to Contacts app and create a contact with valid city and country and save.

to enable the fields longitude and latitude on contact Profile, open the contact, click on Adapt UI from the right side menu

right click anywhere below the Additional Data

click on Add Field from the popup

and select longitude and latitude

save, save and exit.

 

 

How to Test in Postman

it is also possible to call this free service from postman to do quick test and compare the results.

use below link in Postman, you can change the country and city and test further. also you can add a street to receive more precise coordinates.

https://nominatim.openstreetmap.org/?format=json&street= &city=Bengaluru &country=IN

 

Conclusion

If you are receiving contacts to Marketing cloud without Geo Coordinates and if you still wish to segment the contacts based on their Geo Location, you can enable this feature.

Depending on the data that you receive, you can easily change the above implementation to use different address fields like Street/Pincode to determine the Geo Coordinates.

 

Please share yours thoughts or feedback by leaving a comment below. ๐Ÿ™‚

 

Assigned Tags

      5 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Tobias Schneider
      Tobias Schneider

      Hi Amshuman,

      very nice blog with great step by step description. Really cool feature, will surely try it out! ๐Ÿ™‚

      Would be great to read more of this ๐Ÿ™‚

      Kind Regards

      Tobias

      Author's profile photo Amshuman Aravind Sagar
      Amshuman Aravind Sagar
      Blog Post Author

      Tobias Schneider Thank you ๐Ÿ™‚

      Will try to post more ๐Ÿ™‚

      Author's profile photo Christian Langmann
      Christian Langmann

      Hi Amshuman Aravind Sagar

      Thank you for this guide - this is really helpful!
      I was already thinking about addressing nominatim to get osm data.

      I was wondnering, if you could give us some information regarding the performance of contact imports? I would say the contact import will take much more longer with this ABAP logic.

      What would you suggest for initial import scenarios where you would need to import for example 500.000 contacts? Is there any restriction regarding API requests to nominatim?

      Best regards
      Christian

      Author's profile photo Amshuman Aravind Sagar
      Amshuman Aravind Sagar
      Blog Post Author

      Christian Langmann Thank you ๐Ÿ™‚

      i tried with limited set of data and could not see any difference in the performance.

      i am using the free version of openstreetmap.org to achieve above requirement and it has few limitations. You can call their free API max once per second and also max 10000 calls per day.

      If you are planning for 500000 contacts then you might need some other API or paid version ofย  openstreetmap.org ๐Ÿ™‚

      Please let me know if this works. i am curious too ๐Ÿ™‚

      Author's profile photo Farhan Ahmed
      Farhan Ahmed

      Hello Amshuman Aravind Sagar,

      I just wanted to drop by and express my gratitude for writing this wonderful blog post. It's truly a gem and has been incredibly helpful for me. Thank you so much for sharing your insights and knowledge.

      However, I've encountered a little roadblock while trying to implement the steps you provided. I believe I'm following the instructions correctly, but I keep encountering an error. Here are the steps I'm following, along with the error message I'm receiving:

      1. Step 1: Communication Scenario - YY1_API_TEST_REST
      2. Step 2: Communication System and user - CTPLABAP
      3. Step 3: Communication Arrangement - YY1_API_TEST_REST
      4. Step 4: then I have created BADI as per my requirement and using the code as per provided by you. I just changed the communication Scenario and outbound service.

      Error Message: Dereferencing of the NULL reference.

       

      I was wondering if you could spare a moment to provide some guidance or point me in the right direction to resolve this issue. Your expertise would be highly appreciated.

      Once again, thank you for your valuable blog post, and I'm looking forward to hearing from you.

      Best regards,

      Farhan Ahmed