Can someone help to code beanshell. this is my code can someone help me to check the error for this?

import java.util.;
import java.lang.
;
import sailpoint.object.QueryOptions;
import sailpoint.object.Filter;
import sailpoint.object.Identity;
import java.util.Iterator;
import java.util.ArrayList;

String source = identity.getAttribute(“source”);
String uniqueId = identity.getAttribute(“name”);
String emailOut = uniqueId + “@test.com”;
String emailOut2 = uniqueId + “@test.net”;

if (source.equalsIgnoreCase(“Outsource”)) {
return emailOut;
}
else if (source.equalsIgnoreCase(“CEBU”)) {
return emailOut2;
}

identity.setAttribute(“email”, null);
context.saveObject(identity);
context.commitTransaction();

String firstname = identity.getFirstname().replaceAll(“\.”,“”);
String middlename = identity.getAttribute(“middleName”);
String lastname = identity.getLastname().replaceAll(“\.”,“”);

if (middlename == null || middlename == void) {
middlename = “”;
} else {
middlename = identity.getAttribute(“middleName”).replaceAll(“\s”,“.”).toLowerCase();
}

String[] brokenFName = firstname.split(" ");
int counterFName = brokenFName.length;

String buildEmail = "";
int counterQuery = 0;
for (int i = counterFName; i > 0 ; i--){
buildEmail += brokenFName[counterQuery] + ".";

String checkEmail = (buildEmail + lastname.replaceAll(“\s”,“”)).toLowerCase() + “@test.com”;
String checkEmail2 = (buildEmail + lastname.replaceAll(“\s”,“”)).toLowerCase() + “@test.net”;

int counterDnN = 0;
//int counterDnN2 = 2;

counterDnN = queryFilter(checkEmail);
counterDnN = queryFilter(checkEmail2);

	if (counterDnN == 0 ){
	return checkEmail;
	} if else (counterDnN != 0 ){
	return checkEmail2;
	} 
	
	
	//if else (counterDnN2 == 2 ){
	//return checkEmail2;
    //}		
	counterQuery++;

} 


if (middlename == ""){
checkEmail = (buildEmail + middlename + "." + lastname.replaceAll("\\s","")).toLowerCase() + "@test.com";
counterDnN = queryFilter(checkEmail);

	if (counterDnN == 0 ) {
	return checkEmail;
	} else {
		i = 1;
		String counterLastname = lastnameCounterWithMiddlename((buildEmail + middlename + "." + lastname.replaceAll("\\s","")).toLowerCase());
		return counterLastname;
    } 
} else {
	checkEmail = ((buildEmail + lastname.replaceAll("\\s","")).toLowerCase())+ "@test.com";
	counterDnN = queryFilter(checkEmail);
	
	if (counterDnN == 0 ) {
		return checkEmail;
	} else {
		i = 1;
		String counterLastname = lastnameCounterWithoutMiddlename((buildEmail + lastname.replaceAll("\\s","")).toLowerCase());
		return counterLastname;
	}

}	
if (middlename == ""){
checkEmail2 = (buildEmail + middlename + "." + lastname.replaceAll("\\s","")).toLowerCase() + "test.net";
counterDnN = queryFilter(checkEmail2);

	if (counterDnN != 0 ) {
	return checkEmail2;
	} else {
		i = 1;
		String counterLastname = lastnameCounterWithMiddlename((buildEmail + middlename + "." + lastname.replaceAll("\\s","")).toLowerCase());
		return counterLastname;
    } 
} else {
	checkEmail2 = ((buildEmail + lastname.replaceAll("\\s","")).toLowerCase())+ "@test.net";
	counterDnN = queryFilter(checkEmail2);
	
	if (counterDnN != 0 ) {
		return checkEmail2;
	} else {
		i = 1;
		String counterLastname = lastnameCounterWithoutMiddlename((buildEmail + lastname.replaceAll("\\s","")).toLowerCase());
		return counterLastname;
	}

}
   	 context.decache(identity);

I see a few issues, may not be comprehensive:

Your first two imports are incomplete paths, I think you meant to use an asterisk at the end of java.util. and java.lang., i.e. java.util.; and java.lang.;

This line:

String firstname = identity.getFirstname().replaceAll("\.","");

Is invalid because the first backslash in your regex is only escaping the java string. So first you have to escape the backslash with another backslash, and then you can use the . (dot) literal in your regex. So change it to something like this:

String firstname = identity.getFirstname().replaceAll("\\.","");

It looks like you’ve done that in a number of areas.

Also this line:

} if else (counterDnN != 0 ){

Should be:

} else if (counterDnN != 0 ){

Also, while it’s not required for beanshell, I would recommend specifying the data types on all of your variable declarations.

I also highly recommend mocking your beanshell code up in a java project in VSCode with the sailpoint libraries included. Because of the linting it provides, it makes it a lot easier to find issues like these.

  1. There is no need to explicitly import java.util.; or java.lang. classes, as they are already imported by default

  2. Depending on the type of Rule that this is (looks like an IdentityCreation Rule?), you most likely should not be explicitly saving and decaching the identity object