Based on Names we need to get id

Based on Names we need to get id

I have a form named as " contact"  i have field " client name"

if i gave client names as  "Ashok Kumar" then client id must be as " AK-1",
     2) Arjun Kumar - AK-2 
    if the first letters are same the number should be increased like ws-1,ws-2,ws-3 if not it should be in the format XX-1


Below I have attached  a screenshot of contact report  i need an output in this format  SS-1,SS-2,KJ-1 , but i got the output as SS-1,SS-2,KJ-3

 

Here is the code which i have tried, but i am not getting the expected output.


firstVal = input.Lead_Name.getprefix(" ").subtext(0,1);
lastVal = input.Lead_Name.getsuffix(" ").subtext(0,1);
stringval = (firstVal + lastVal).toUppercase();
contactObj = Contact_Form[ID != null];
checkPreffix = Contact_Form[Client_id == stringval];
//collection
if(contactObj.count() == 0 && checkPreffix == checkPreffix)
{
idVal = stringval + "-" + 1;
//info idVal;
}
else if(contactObj.count()>=1)
{
contactsObj = Contact_Form[ID != null].Client_id sort by Added_Time desc;
val = contactsObj.getsuffix("-");
temp = val.toLong();
temp = temp + 1;
idVal = stringval + "-" + temp;
info idVal;
}