BLTH for UserID creation

Document ID : KB000100765
Last Modified Date : 11/06/2018
Show Technical Document Details
Introduction:
I am looking for a blth where i need to create a user ID based on following conditions
UserID=(First Character of First Name + First character of Last Name + 'a' + DepartmentNumber)
For Example:
First name: Randeep
Last Name: Singh
Deaprtment: 0200
UserID = rsa0200 
if the generated user id already exist then we can increment the character at 3 position to b.
in that case new UserID will be rsb0200
Question:
How to do?
Answer:
package com.ca.identitymanager.guiph01;

import com.netegrity.imapi.BLTHAdapter;
import com.netegrity.imapi.BLTHContext;
import com.netegrity.ims.exception.IMSException;
import com.netegrity.llsdk6.imsapi.exception.NoSuchObjectException;
import com.netegrity.llsdk6.imsapi.managedobject.User;
import com.netegrity.llsdk6.imsapi.provider.UserProvider;

public class BLTHsetUID extends BLTHAdapter {
    public void handleValidation(BLTHContext blthContext) throws Exception {
        User user = blthContext.getUser();
        
        String FirstName = user.getAttribute("%FIRST_NAME%");
        String LastName = user.getAttribute("%LAST_NAME%");
        String departmentNumber = user.getAttribute("departmentNumber");

        if (FirstName.isEmpty() || LastName.isEmpty() || departmentNumber.isEmpty()) {
            // this message will be presented on the screen
            IMSException imsEx = new IMSException();
            imsEx.addUserMessage("Failed to build an UID, first name, last name and department number are required");
            throw imsEx;
        }
        String UID = FirstName.substring(0, 1) + LastName.substring(0, 1) + 'a' + departmentNumber;
        UserProvider userbis = blthContext.getUserProvider();
        
        for (char c='a'; c<='z'; c++){      
          try {
                UID = FirstName.substring(0, 1) + LastName.substring(0, 1) + c + departmentNumber;
                userbis.findUser(UID, null);
                //UID already exists, search for next computed one
                if (c=='z') {
                    // this message will be presented on the screen
                    IMSException imsEx = new IMSException();
                    imsEx.addUserMessage("Failed to find an UID, all UIDs already exist");
                    throw imsEx;
                }
          } catch (NoSuchObjectException nso){break;}
        }

        try {
           blthContext.getUser().setAttribute("%USER_ID%", UID);
        } catch (Exception ex) {
            // this message will be presented on the screen
            IMSException imsEx = new IMSException();
            imsEx.addUserMessage("Failed to set an UID" + ex.getMessage());
            throw imsEx;
        }
    }
}
Additional Information:

Adapt with your department Number attribute name or well known name and other specific requirements.
Do not hesitate to download and review the programming java doc.
https://docops.ca.com/ca-identity-manager/14-1/EN/files/410665376/410665377/1/1479144421210/im_javadoc.zip