function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Saurabh Sood 12Saurabh Sood 12 

How assign empId to employee based upon company name

I have created two objects one is Company and other is an employee. The company has one field that's company name. The employee has three fields emp_name, emp_company and emp_id and the Employee has lookup relationship with the company.

How to assign empId based on Company name 
Suppose company Name is: Delloite then empId should be Del001, the next user will have Del002
If another company is there company name is: Wipro then empId Should be Wip001 and next Wip002

 
 
v varaprasadv varaprasad
Hi Saurabh,

Please check once below sample code : 
Change below code based on your objects.
 
trigger ContactId on Contact (before insert) {
    public static integer delId;
    set<id> accids = new set<id>();
    
    for(Contact con : trigger.new){
        if(con.Accountid != null){
            accids.add(con.Accountid);          
        }        
    }
    
    list<account> accnames = [select id,name from account where id in : accids];
    system.debug('==accnames=='+accnames);
    
    integer counter = [select count() from contact where accountid in : accids];
    
    
    for(account acc : accnames){
        if(acc.name == 'Techmahendra'){
            for(contact cc : trigger.new){
                cc.Title = 'Techm00'+(counter+1);  
                system.debug('==cc.Title=='+cc.Title);
             
            }  
        }
         else if(acc.name == 'Wipro'){
            for(contact cc : trigger.new){
                cc.Title = 'Wipro00'+(counter+1);  
                system.debug('==cc.Title=='+cc.Title);
             
            }  
        }
        
    }
}

Hope it helps you.
Please let me know in case of any concerns. 


Thanks
Varaprasad
 
Paul S.Paul S.
I've used a custom object to store company prefixes and counts.  In your case, each time a contact was updated you'd determine what the prefix would be, go find that prefix (from prebuilt map of prefix => count), and increment the count as needed.  I'd also incorporate a check to make sure the empId was null...you don't want to change the empId each time the contact record is edited.
Saurabh Sood 12Saurabh Sood 12
@Varaprasad  you hard coded  acc name suppose 1000 acc's are there we have to hardcoded 1000 times so that solution is good but not good for implementation thanks for solution if you have another idea counter on comment plz
 
Saurabh Sood 12Saurabh Sood 12
@paul s: I think there would be better way to solve with help of trigger i'm working on that rether to create another object to store prefix or count 
Paul S.Paul S.
Fair enough...do the employee Ids need to be unique even across companies?  For example, can one person who works at Dell and another that works at Deloitte both have a Del001 employee Id?
Saurabh Sood 12Saurabh Sood 12
Yes, for Dell first employee, empId should be Dell001 and for Deloitte, empId should be this  Delo001. 
Paul S.Paul S.
Makes sense...my point was just to say if you try to derive the employee Id just from the account name (i.e., don’t use that separate object that keeps track of what’s been used), and assuming Ids need to be unique across companies, you’d need to modify your code to look for all accounts with a name similar to the one of the given contact.
v varaprasadv varaprasad
Hi Saurabh,


Please check once below code : 
 
trigger ContactId on Contact (before insert) {
    
    set<id> accids = new set<id>();
    
    for(Contact con : trigger.new){
        if(con.Accountid != null){
            accids.add(con.Accountid);          
        }        
    }
 
    Map<id,Account> mapOfAccs = new Map<id,Account>([select id,name from account]);
    
    system.debug('==mapOfAccs=='+mapOfAccs);
    
    integer counter = [select count() from contact where accountid in : accids];
    for(contact cc : trigger.new){
        if(cc.Accountid != null){
            String accname = mapOfAccs.get(cc.Accountid).Name;
            system.debug('==accname=='+accname);
            string creatId = accname.substring(0, 4);
            system.debug('==creatId=='+creatId);
            cc.Title = creatId+(counter+1);  
            system.debug('==cc.Title=='+cc.Title);
        }
        
        
    }
}

Hope it helps you.

Please let me know in case of any other assistance.


Thanks
Varaprasad