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.
- 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
- 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.
- 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“.
- Step 7: Select the browser control, go to the Design tab > Options section, and un-check the Delay Page Load check box.
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.
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.
I believe the 1970 limit will be resolved this calendar year, in the May or August release.
Nice post, Doc, shows a world of creative options!
Thanks, glad you like it. And hopefully that 1970 limitation is really going to be corrected.
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#
Thanks Wendy, that could be a solution for the limit of 1970.
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!
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?
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.
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!
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.
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.
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.
Hi,
Could you please specify? Which is the object where you have created that custom field?
Hi Luis,
Can we use ‘JavaScript API’ in the Chat Session object ?
Thanks,
Vig