Custom Mapping: Javascript functions for eval.js

Created: 2015-03-30 15:19:59
Modified: 2023-01-13 09:20:11
Tags: Custom Mapping Javascript UnitySync

The details below pertain only to Javascript for use with UnitySync v2.4 or later. If you are using a version prior to v2.4, review this article for additional information.

In …\UnitySync\-v2.4\global, determine if you already have an eval.js file. If you do not, you will find the sample file eval-example.js Copy this sample file to eval.js, then right-click to open and edit your eval.js. This contains one sample subroutine.

Below are additional sample subroutines and the appropriate custom mapping syntax to call them.

smtp_domain

This function takes a string as input (email address) and returns the value after the @ character.

function smtp_domain(smtpaddr)  
{  
    var parts=smtpaddr.split('@');  
    return parts[1]===undefined ? '' : parts[1];  
}  

Example mapping to call smtp_domain: Attribute=&smtp_domain('^mail^')&

swapchars

This function will convert dash and space to period and ‘contr’ to blank

function swapchars(mystring) 
{ 
var str = mystring.replace(/-/g,'.');     
      str = str.replace(/   /g,'.');         
      str = str.replace(/Contr/g,'');     
return str;
} 

Example mapping to call swapchars: description=&swapchars('^description^');&

onlydigits

This function will return a value containing only digits.

function onlydigits(mystring)
{
var str = mystring.replace(/\D/g, '');
return str;
}

Example mapping to call onlydigits: department=&onlydigits('^department^')&

NOTE: This may also be accomplished without eval.js. Instead use only this single custom mapping:
department=&'^department^'.replace(/\D/g, '');&

titlecase

This function will return the string using Title Case.

function titlecase(str)
{
 fixme=str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
 return fixme;
}

Example mapping to call titlecase: description=&titlecase('^description^');&

remove_space

This function will strip all spaces from the attribute.

function remove_space(str)
{
return str.replace(/\s/g,'');
}

Example mapping to call remove_space: sn=&remove_space('~sn~')&

NOTE: This may also be accomplished without eval.js. Instead use only this single custom mapping: sn=&'~sn~'.replace(/\s/g,'');&

remove_space_and_apostrophe

This function will strip all spaces AND apostrophe from the attribute.

function remove_space_and_apostrophe(str)
{
return str.replace(/\s/g,'');
}

Example mapping to call remove_space_and_apostrophe: sn=&remove_space_and_apostrophe('~sn~')&

NOTE: This may also be accomplished without eval.js. Instead use only this single custom mapping (Note the use of double and single quote in this mapping): sn=&"~sn~".replace(/\s/g,"").replace(/'/g,"");&

fix_string

This function will return the string after removing or swapping the specified characters. In this example, the various characters are replaced with a blank (effectively removing that character).

function fix_string(orig) 
{ 
var newstr=orig.replace(/#/g,''); 
newstr=newstr.replace(/-/g,''); 
newstr=newstr.replace(/'/g,''); 
newstr=newstr.replace(/-/g,''); 
newstr=newstr.replace(/\//g,''); 
return newstr; 
}

Example mapping to fix string: description=&fix_string('^description^')&

grab_container

This function accepts a single text value (i.e. DistinguisedName or DN) and removes everything before the first instance of “OU=”. The remainder of the string is returned. If “OU=” is not found, the entire string is returned.

function grab_container(fulldn) 
{ 
var parts=fulldn.substring(fulldn.indexOf('OU=')); 
return parts; 
} 

Example mapping to call grab_container: extensionAttribute1=&grab_container('^distinguishedName^');&

If this is the DN of the source object:
CN=enichols,OU=My Users,DC=2k10,DC=test

Following Sync, the Destination extensionAttribute1 will be:
OU=My Users,DC=2k10,DC=test

IfThenElseReturn

This function will accept multiple values, perform if/then/else to determine desired output value and return that value. Can be modified for more input parameters and/or text values.

example mapping:
description#1024=&IfThenElseReturn('^department^','^office^','^l^','^c^')&

function IfThenElseReturn(var1,var2,var3,var4) 
{ 

if (var1 == "1001") {
    return var2 + '- internal';

 } else if (var1 == "1002") {
   return var3 + '- local'';

 } else if (var1 == "1003") {
   return var4 + '- international';

 } else {
   return var1 + '- unknown';
}
}

NOTE: Functions may also be called without parameters. This would require the use of getsrc and getint.**

i.e. Description=&return_two_values_not_passed()&

ReturnYMD

This function will take as input an AD Date string (i.e. AccountExpires) and return YYYY/MM/DD

// Input: accountexpires 100-nanosecond intervals since 1601/01/01 UTC

// Return: YYYY/MM/DD or “6”

function ReturnYMD(s)
{
 if (s == 9223372036854775807) {    //default, not set, Return "6"
   return "6";}
 else if (s == 0) {                  //Never Expire,  Return "6"
   return "6";}
                                //Else...Continue and return YYYY/MM/DD
 var t=((s/10000000) - 11644473600);
 var d=new Date(0);
     d.setUTCSeconds(t);
 var x = d.toISOString();

 var yrmoday = x.slice(0, 10);          //grab first 10 digits YYY-MM-DD
     yrmoday = yrmoday.replace(/-/g,'/');  // replace char - with char /

return yrmoday;              // Return YYYY/MM/DD
}

Example mapping to call ReturnYMD:

description=&ReturnYMD("^accountexpires^");& 

formatDisplayname

Note: This function utilizes getsrc instead of passing in parameters from the original mapping.

function formatDisplayName()
{ 
  var sn = getsrc('sn');
  var gn = getsrc('givenname');
  var nn = getsrc('nickname');
  var ecd = getsrc('exportcontroldata');
  var mn = getsrc('middlename');
  var name = ''; 

  if (typeof(sn) != "undefined") {name = sn;}
  if (typeof(nn) != "undefined") {name = name + ', ' + nn;}
  else if (typeof(gn) != "undefined") {name = name + ', ' + gn;}

  if ((typeof(nn) == "undefined") && (typeof(mn) != "undefined" )){name = name + ' ' + mn.substring(0,11);}

  if (ecd == 'allowed') {name = name + '    Allowed'}
  else {name = name + '     Export License Required US'}

  return name;
}

Example mapping to call formatDisplayName

displayname#256=&formatDisplayName()&

ParseDisplayName

Takes as input a string (displayname, presumed format “First.Last”). Returns displayname in format “Last, First”. If NO period (.) in string, returns original string.

function ParseDisplayName(origdisplayaname)  
{

  var parts=origdisplayaname.split('.');  

  if (parts[1]===undefined) return origdisplayaname;
     return parts[1] + ', ' + parts[0];
}   

Example mapping to call ParseDisplayName

Displayname=&ParseDisplayName("^displayname^")&

Debugging javascript

For more information on debugging, please see Debug logging in custom eval.js functions

Knowledgebase

Directory
  1. Directify - Self Service

  2. Mimic - Replication

  3. UnitySync - Sync
Password
  1. emPass - Sync
Obsolete
  1. Profiler
  2. SimpleSync