Google Maps for Organisations

A few weeks ago, in this post, I explained how to embed Google Maps on RightNow CX, in order to quickly see the Contacts’s address using the information in the RightNow database.

Some of you asked if this was possible to do with Organisations. The answer was: unfortunately it is not as straight forward as with Contact, and there is two reasons for that:

a) The Address field in Organisation – “Oaddr” – is different than that in the Contact. It is a collection of addresses that has two types: Billing and Shipping. These are controlled by objects “Organization Addresses” and “Organization Address Types”.

b) Address values in Organisation are not available to get directly. “Oaddr” is expressed as string XML data of the following form:

<Addr Street=”123 main st” City=”Bozeman” ProvId=”23″ PostalCode=”59718″ CountryId=”1″ />.

The suggestion to get this done was to use a Desktop Integration with RightNow Connect JavaScript API in 4 stages:

  1. Get and parse the Organisation address
  2. Retrieve the values (Street, City, Province, Postal Code, Country)
  3. Build a Google Maps URL with the values
  4. Navigate to Google Maps using “window.location”

The solution is then:

  • Step 1: Create an HTML file with the JavaSCript code to build the Google Maps URL

<html>
<head>
<script type=“text/javascript”>

function getAddress()
{
var selection=document.getElementById(‘address_type’).value;
//0 for billing addr and 1 for shipping addr
var address = window.external.Org.OAddresses;
var org=window.external.Org;
var provinceName;
var countryName;

//Parsing the XML into object
var xmlDoc = new ActiveXObject(“Microsoft.XMLDOM”);
xmlDoc.async = “false”;
xmlDoc.loadXML(address);

//Retrieving all the fields of selected Organisation Address
var desiredAddress=xmlDoc.getElementsByTagName(“OAddresses”)[0];

var street=desiredAddress.getElementsByTagName(“Addr”)[selection].getAttribute(“Street”);
var city=desiredAddress.getElementsByTagName(“Addr”)[selection].getAttribute(“City”);
var provId=desiredAddress.getElementsByTagName(“Addr”)[selection].getAttribute(“ProvId”);

var postalCode=desiredAddress.getElementsByTagName(“Addr”)[selection].getAttribute(“PostalCode”);
var countryId=desiredAddress.getElementsByTagName(“Addr”)[selection].getAttribute(“CountryId”);

//Fetching Province Name corresponding to provinceID retrieved
var provLabels = org.GetNameValues(“ProvId”);
xmlDoc.loadXML(provLabels);
var x=xmlDoc.getElementsByTagName(“Item”);
for (i = 0; i < x.length; i++)
{
var provAttlist=x.item(i).attributes;
var provAtt=provAttlist.getNamedItem(“Id”);
if (provAtt.value==provId)
{
provinceName= provAttlist.getNamedItem(“Name”).value;
}
}

//Fetching Country Name corresponding to countryId retrieved
var countryLabels = org.GetNameValues(“CountryId”);
xmlDoc.loadXML(countryLabels);
var y=xmlDoc.getElementsByTagName(“Item”);
for (i = 0; i < y.length; i++)
{
var countAttlist=y.item(i).attributes;
var countAtt=countAttlist.getNamedItem(“Id”);
if (countAtt.value==countryId)
{
countryName= countAttlist.getNamedItem(“Name”).value;
}
}

//Building the google maps URL
var finalAddress=street+” “+city+” “+postalCode+” “+ provinceName+” “+countryName;
window.location=”http://maps.google.co.in/maps?hl=en&q=”+finalAddress;
}

function ondataupdated(obj)
{
getAddress();
}

</script>
</head>
<body onload=getAddress()>
</body>
</html>

  • Step 2: Upload the HTML file to a server via FTP (I normally use FileZilla).
  • Step 3: Add a new tab to the Organisations Workspace and drag and drop a browser control into the tab

rn1

  • Step 5: Select the browser control, go to the Design tab > Options section, and put the URL path of the HTML file into the URL field.

rn2

  • Step 6: Select the browser control, go to the Design tab > Options section, and un-check the Delay Page Load check box.

rn3

Now, whenever you open an Organisation, and navigate to the “Map” tab, you will automatically see the Organisations’s address on the map.

Thank you to Bhagwan Singh Mer (Lead Consultant at Wipro Technologies) and Jens Lundell (Oracle RightNow Workflow Development Manager) for their help on this requirement. Special thank you to Saurabh Gupta (Consultant at Wipro Technologies) as it was him that scripted the JavaScript code.

Calculated field using JavaScript API

I have created a “Date of Birth” Custom Field (Type: Date) on the Contact object, and my requirement was to create a field that would display the Age of the Contact, based on the “Date of Birth” field.

For that I used the Connect Desktop Integration (JavaScript API), and did the following:

  • Step 1: Create a Custom Field (Type: Integer) in the Contact object with the name “Age”, and add the field to the Contact workspace.

img1

  • Step 2: Create an HTML file with code to calculate the Age and set the “Age” field.

<html>
<head>
<script type=“text/javascript”>
function SetAge()
{
var dobInt = window.external.Contact.GetCustomFieldByName(“c$date_of_birth”);
if (dobInt == –1)
{
var textna = “N/A”;
window.external.Contact.SetCustomFieldByName(“c$age”, textna);
return;
}
var dob = new Date(dobInt * 1000);
var now = new Date();
var yearAge = now.getFullYear() – dob.getFullYear();
var monthNow = now.getMonth() + 1;
var monthDob = dob.getMonth() + 1;
if (monthNow < monthDob || (monthNow == monthDob && now.getDate() < dob.getDate()))
yearAge–;
window.external.Contact.SetCustomFieldByName(“c$age”, yearAge);
}
function ondataupdated(obj)
{
SetAge();
}
</script>
</head>
<body onload=SetAge()>
</body>
</html>

  • Step 3: Upload the HTML file to a server via FTP and take the URL.
  • Step 4: Add a new tab to the Contact Workspace and drag and drop a browser control into the tab

img2

  • Step 5: Select the browser control, go to the Design tab > Options section, and put the URL of the HTML file into the URL field.

img3

  • Step 6: Select the new tab, go to the Design tab > Behavior section, click the Hidden button and make the tab hidden “On New” and “On Open.

img4

  • Step 7: Select the browser control, go to the Design tab > Options section, and un-check the Delay Page Load check box.

img5

Now, whenever you open a contact that has “Date of Birth” populated, the script will calculate the Age and set the “Age” Custom Field with the right value.

img6

You might also want to make the “Age” field read only so that its value is only set by the script and not by the user.

Important Note: The date fields only allow to go back to 1970, which in this case of Date of Birth is a bit limited.

Thank you to Jens Lundell (Oracle RightNow Workflow Development Manager) for his help on this requirement and to Mark Kehoe (Independent Oracle RightNow Consultant) for raising the date limitation issue.