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.

13 thoughts on “Calculated field using JavaScript API

  1. that one should had already been corrected

    In the meantime, if you do wish to have a date field which goes back further, plain text will be the resolution.

    Custom field plain text of 10 positions
    with default value ##-##-####
    and a mask M#M#F-M#M#F-M#M#M#M#

  2. Hi Luis,
    Just discovered your blog and I’ll definitely be following it closely. This post looks like one I’ll try to leverage. We’re just getting CX May’13 configured for initial deployment and we keep running into scenarios where we want to perform calculations using business days but what we want to do either isn’t possible or requires “weird” configuration (i.e. using business rules and escalations). So one thing I want to do is create a “Due Date” field for incidents where the Due Date is Now+X business days. Based on your post I know think this could be possible–I just need to go learn Connect and Javascript!

    1. Hi Ryan,

      I’m glad you like the blog and find it useful.

      Why don’t you use the Due Date field that already exists OOTB and configure the SLA to set the date when the incident should be resolved?

      1. Luis, thanks for the response…sorry it took so long to get back to this we basically put this requirements aside for a while. Finally getting back to it. Our due dates are based on the type of ticket e.g. the categorization, and the clock restarts whenever the customer udpates the ticket . So if it’s ticket type A we may have 10 business days to resolve or 5 business days for Type B. If Type A ticket on day 2 we request more info from the customer, if they respond on day 3 then we actually restart the clock and now have 10 days from day 3 to resolve. So that’s why I don’t think the OOTB Due Date field will work, but maybe I’m missing something.

  3. Hi Luis,

    I started working on this example in my test site but the Age field isn’t populating. It appears the age.html file is loading but whenever I set or default the Date of Birth field it doesn’t populate the Age field. Any advice? thanks!

    1. Hi Ryan,

      Make sure you have the Connect JavaScript API enabled (double check with Oracle).

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

      1. Just asked Oracle to look into this for us. I also got info that the Feb 14 release is scheduled to have some new feature related to calculating business days. I’m still trying to get more information but fingers cross that Oracle has finally provided some long-requested functionality.

        Side-note: I checked the option to “Notify me of follow-up comments via email” but don’t recall ever getting such email.

  4. Hi Luis,

    Could you please tell me how to call a custom field in customer portal using Javascript? Syntax If any. Would be grateful if you could help me in this regard.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s