DirWiz Logo
Knowledgebase

Article Tags

Custom Mapping: Javascript functions for eval.js

2019-08-22 10:30:45
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.

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';
}
}
Share this article: Twitter reddit