Code Run samples

This page has different samples for the Code Run activity.

Set DisplayName for user

Sets the DisplayName by concatenating FirstName and LastName handling any empty values in either. You specify [//Target/FirstName] and [//Target/LastName] as parameters and set Destination to be [//Target/DisplayName].

using System;
using System.IO;

public class FIMDynamicClass
{

    public object FIMDynamicFunction(object firstname, object lastname)
    {
return string.Format("{0} {1}", firstname == null ? "" : firstname.ToString().Trim(), lastname == null ? "" : lastname.ToString().Trim()).Trim();
    }

}

Screenshot of the Code Run workflow that sets Displayname with the code above

Generate an account name from DisplayName

Very simple sample that generates an account name from the displayname passed. There is no normalization or verification of duplicates but the sample just shows how you could generate an accountname. You specify [//Target/DisplayName] as the only parameter and set Destination to be [//Target/AccountName].

using System;

public class FIMDynamicClass
{
            public static object FIMDynamicFunction(object displayName)
            {
                string accountName = null;

                if (displayName == null)
                    return null;

                string[] el = displayName.ToString().Split(' ');
                switch (el.Length)
                {
                    case 1:
                        accountName = el[0][0].ToString() + el[0][1].ToString();
                        break;
                    case 2:
                        accountName = el[0][0].ToString() + el[1][0].ToString();
                        break;
                    default:
                        accountName = el[0][0].ToString() + el[1][0].ToString() + el[2][0].ToString();
                        break;
                }

                return string.Format("{0}", accountName);
            }

}

Create a home directory for user

Very simple example of a workflow that creates a homedirectory for the user hit by the workflow. For this sample, you specify [//Target/AccountName] as the only parameter and set Destination to be [//WorkflowData/CreateResult] (result is of no use in this sample).

using System;
using System.IO;

public class FIMDynamicClass
{

    public object FIMDynamicFunction(object accountname)
    {
      System.IO.Directory.CreateDirectory(@"C:\Temp\"+accountname);
        return "OK";
    }

}
Screenshot of the Code Run workflow that creates a home directory with the code above

Returning a date

Sample code for returning a date for updating, i..e EmployeeEndDate to current date/time. For this sample, you could set Destination to be [//Target/EmployeeEndDate]. This code expects no parameters.

using System; 

public class FIMDynamicClass 
{ 
  public string FIMDynamicFunction() 
  { 
    return DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss.000"); 
  } 
}

Simple switch/case returning a string

This code uses a standard switch/case statement to return a employee description based on the input employee type parameter 'employeeType'. For this sample, you could set Destination to be [//Target/EmployeeDescription] (EmployeeDescription attribute is a custom).

using System;

public class FIMDynamicClass
{
    public string FIMDynamicFunction(string employeeType)                 
    {
        switch (employeeType.ToUpper())
        {
            case "E":
            case "EXT":
            case "EX":
                return "External";
            case "CO":
                return "Contractor";
            case "P":
                return "Part time employee";
            case "FT":
                return "Full time employee";
            default:
                return "";
        }
    }
}

Normalizing strings

This piece of code removes special-characters and accents from the Latin alphabet. This is especially useful when you need to normalize names. The character list can be altered as you wish, and be aware that the example also removes numbers from the string. You can add other alphabets if needed.

Given this string:
"0123456789abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ´`+?=})]([/{&%¤$@!½§|¨^~'*/-,;.:'*<>æøåÆØÅöÖôÔõÕäÄâÂãÃáàÀÁóòÓÒéèÈÉËëíìÍÌïÏîÎùúÙÚüÜûÛýÝÿ-_"

The method will return:
"abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZaeoeaaAeOeAaoeOeoOoOaaAaaAaAaaAAooOOeeEEEeiiIIiIiIuuUUueUeuUyYy"

using System;
using System.Text.RegularExpressions;
using System.Globalization;
using System.Text;

public class FIMDynamicClass
    {
        public string FIMDynamicFunction(string InputString)
        {

            return Normalize(InputString);
        }

        public static string Normalize(string strMessage)
        {

            if (!String.IsNullOrEmpty(strMessage))
            {
                //Letters
                strMessage = Regex.Replace(strMessage, "[\u00E9\u00E8\u00EA\u00EB]", "e");      
                strMessage = Regex.Replace(strMessage, "[\u00C9\u00C8\u00CB\u00CA]", "E");     
                strMessage = Regex.Replace(strMessage, "[\u00E1\u00E0\u00E2\u00E3]", "a");     
                strMessage = Regex.Replace(strMessage, "[\u00C0\u00C1\u00C2\u00C3]", "A");     
                strMessage = Regex.Replace(strMessage, "[\u00C4\u00C5]", "Aa"); 
                strMessage = Regex.Replace(strMessage, "[\u00E4\u00E5]", "aa");   
                strMessage = Regex.Replace(strMessage, "[\u00C6]", "Ae"); 
                strMessage = Regex.Replace(strMessage, "[\u00E6]", "ae");   
                strMessage = Regex.Replace(strMessage, "[\u00D9\u00DA\u00DB]", "U"); 
                strMessage = Regex.Replace(strMessage, "[\u00F9\u00FA\u00FB\u00B5]", "u"); 
                strMessage = Regex.Replace(strMessage, "[\u00DC]", "Ue"); 
                strMessage = Regex.Replace(strMessage, "[\u00FC]", "ue");  
                strMessage = Regex.Replace(strMessage, "[\u00F2\u00F3\u00F4\u00F5]", "o"); 
                strMessage = Regex.Replace(strMessage, "[\u00D2\u00D3\u00D4\u00D5]", "O");     
                strMessage = Regex.Replace(strMessage, "[\u00F6\u00F8]", "oe"); 
                strMessage = Regex.Replace(strMessage, "[\u00D6\u00D8]", "Oe");   
                strMessage = Regex.Replace(strMessage, "[\u00EC\u00ED\u00EE\u00EF]", "i"); 
                strMessage = Regex.Replace(strMessage, "[\u00CC\u00CD\u00CE\u00CF]", "I");     
                strMessage = Regex.Replace(strMessage, "[\u0161]", "s"); 
                strMessage = Regex.Replace(strMessage, "[\u0160]", "S");  
                strMessage = Regex.Replace(strMessage, "[\u00DF]", "ss"); 
                strMessage = Regex.Replace(strMessage, "[\u00F1]", "n");  
                strMessage = Regex.Replace(strMessage, "[\u00D1]", "N");  
                strMessage = Regex.Replace(strMessage, "[\u00E7]", "c");  
                strMessage = Regex.Replace(strMessage, "[\u00C7]", "C"); 
                strMessage = Regex.Replace(strMessage, "[\u00FF]", "y");  
                strMessage = Regex.Replace(strMessage, "[\u0178]", "Y");  
                strMessage = Regex.Replace(strMessage, "[\u017E]", "z");  
                strMessage = Regex.Replace(strMessage, "[\u017D]", "Z");  
                strMessage = Regex.Replace(strMessage, "[\u00D0]", "D");  
                strMessage = Regex.Replace(strMessage, "[\u0153]", "oe"); 
                strMessage = Regex.Replace(strMessage, "[\u0152]", "Oe"); 
                //Accents
                strMessage = Regex.Replace(strMessage, "[\u02C6\u02C7\u02C8\u02C9]", "");
                strMessage = Regex.Replace(strMessage, "[\u02D8\u02D9\u02DA\u02DB\u02DC\u02DD]","");
                strMessage = Regex.Replace(strMessage, "[\u0300\u0301\u0303\u0309\u0323]","");
                //Special Characters
                strMessage = Regex.Replace(strMessage, "[(){}£@¤^`´½§|~¨;]", "");
                strMessage = Regex.Replace(strMessage, "[\u00BB\u00AB\u201C\u201D\u201E\u201F\u2033\u2036]", "");
                strMessage = Regex.Replace(strMessage, "[\u003A\u003C\u003D\u003E\u003F]", "");
                strMessage = Regex.Replace(strMessage, "[\u005B\u005C\u005D\u005E\u005F\u005F]","");
                strMessage = Regex.Replace(strMessage, "[\u0021\u0022\u0023\u0024\u0025\u0026\u0027\u0028\u0029\u002A\u002B\u002C\u002D\u002F]", "");
                strMessage = Regex.Replace(strMessage, "[\u2026]", "...");
                //Numbers
                strMessage = Regex.Replace(strMessage, "[0123456789]", "");

                return RemoveDiacritics(strMessage);
            }
            else return "";
        }
        public static string RemoveDiacritics(string s)
        {
            String normalizedString = s.Normalize(NormalizationForm.FormD);
            StringBuilder stringBuilder = new StringBuilder();

            for (int i = 0; i < normalizedString.Length; i++)
            {
                Char c = normalizedString[i];
                if (CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)
                    stringBuilder.Append(c);
            }

            return stringBuilder.ToString();

        }
    }

Last edited Feb 19, 2013 at 5:28 PM by Granfeldt, version 9

Comments

No comments yet.