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
arun kumar 577arun kumar 577 

Hi guys, I have urgent requirement please help me on this

I need to write trigger on contact to update owner of contact using this conditions

I just give example:

In contact:
Account number__c=12345;

In Account :
AccounNumber= 12345;
A__c=112-bobby; 
Here A__c is text data type. 
112 is the employee number field value in user object; 
Bobby is username of 112 of employee number; 

Once trigger is applied what we have to show

Which user has 112 of employee number that user should come as contact owner. 

112 is part of A__c field value. 

In user:
Employee number =112;

Finally 
First condition is when contact. Account number=account.account number; 
Based on this we will get account
Based on that account we have A__c field. 
In this field we have employee number -user name. 
Like this
112-bobby

Please let me know if you have any queries 

Thanks
Best Answer chosen by arun kumar 577
Abhishek BansalAbhishek Bansal
Hi Arun,

Please find the required code of your trigger below:
trigger updateOwner on Contact(before insert, before update){
	
	Set<Id> accIds = new Set<Id>();
	
	for(Contact con : trigger.new){
		if(con.AccountId != null){
			accIds.add(con.AccountId);
		}
	}
	
	Map<Id,Account> mapOfAccounts = new Map<Id,Account>([Select A__c,AccounNumber__c from Account where ID IN :accIds]);
	
	Set<String> setOfEmployeeNumber = new Set<String>();
	for(Account acc : mapOfAccounts.values()){
		if(acc.A__c != null){
			setOfEmployeeNumber.add(acc.A__c.split('-')[0]);
		}
	}
	
	Map<String,User> mapOfUser = new Map<String,User>();
	for(User usr : [Select EmployeeNumber from User where EmployeeNumber IN :setOfEmployeeNumber]){
		mapOfUser.put(usr.EmployeeNumber,usr);
	}
	
	for(Contact con : trigger.new){
		if(con.AccountId != null && mapOfAccounts.containsKey(con.AccountId) && con.AccounNumber__c == mapOfAccounts.get(con.AccountId).AccounNumber__c){
			String empId = mapOfAccounts.get(con.AccountId).A__c.split('-')[0];
			if(mapOfUser.containsKey(empId)){
				con.OwnerId = mapOfUser.get(empId).id;
			}
		}
	}
}


Please let me know if you have any issue in this.

Thanks,
Abhishek Bansal.

All Answers

Sharankumar DesaiSharankumar Desai
Hi Arun,

Below is the Trigger code which will work for your requirement as-is.

Please mark as solution if it really helped you.. 
 
trigger ContactTrigger on Contact (before insert) {
    
    try{
        
        LIST<String> contactAccountNumberList = new LIST<String>();        
        Map<String,String> accountnumberAndEmployeeNumberMap = new Map<String,String>();
        Map<String,String> employeeNumberAnduserIdMap = new Map<String,String>();
        
        //-- Build Contacts AccountNumber List for query
        for(Contact eachcontact : Trigger.NEW){            
            if(String.isNotEmpty(eachcontact.Account_Number__c) && String.isNotBlank(eachcontact.Account_Number__c) ){
                contactAccountNumberList.add(eachcontact.Account_Number__c);
            }
        }
        
        if(contactAccountNumberList.size()>0){
            
            //-- Query Account to build employee numbers matching account number
            List<Account> matchingAccountList =   [SELECT Id,A__c,Account_Number__c FROM Account WHERE Account_Number__c IN :contactAccountNumberList];
            for(Account eachaccount : matchingAccountList){             
                if((eachaccount.A__c).contains('-')){
                    accountnumberAndEmployeeNumberMap.put(eachaccount.Account_Number__c, (eachaccount.A__c).split('-')[0]);                
                }
            }
            
            if(accountnumberAndEmployeeNumberMap.size()>0){
                
                //-- Query User matching employee numbers
                List<user> userList =   [SELECT Id,Employee_Number__c FROM User WHERE Employee_Number__c IN :accountnumberAndEmployeeNumberMap.values()];
                for(user eachuser : userList){
                    employeeNumberAnduserIdMap.put(eachuser.Employee_Number__c, eachuser.Id);
                }        
                
                //-- owner Assignment Logic
                if(employeeNumberAnduserIdMap.size()>0){
                    
                    for(Contact eachcontact : Trigger.NEW){            
                        if(accountnumberAndEmployeeNumberMap.containsKey(eachcontact.Account_Number__c)
                           && employeeNumberAnduserIdMap.containsKey(accountnumberAndEmployeeNumberMap.get(eachcontact.Account_Number__c))){
                               
                               //-- Assign owner
                               eachcontact.OwnerId=employeeNumberAnduserIdMap.get(accountnumberAndEmployeeNumberMap.get(eachcontact.Account_Number__c));
                               
                           }            
                    }            
                }                
            }
            
        }
        
    }catch(Exception exec){
        
        System.debug('exception Message'+exec.getMessage());
        //-- perform any operations if required
        
    }
    
}

 
arun kumar 577arun kumar 577
Hi Sharan,

i got this error from this  code Please can you reslove and send me code 

User-added image
Sharankumar DesaiSharankumar Desai
What is the Datatype of eachaccount.A__c field in your Org.

Please share complete setting of this field
arun kumar 577arun kumar 577
Hi Sharan,

i got this code without error but i didnot achieve my requirement with this code.
can you please see once and share code.

A__c is text data type

Thanks 
Sharankumar DesaiSharankumar Desai
This is working code as per your requirement. Please check whether you are using right fields/ whether the code i shared is pointing to right fields
Sharankumar DesaiSharankumar Desai
In the code i shared its pointing to all custom fields Like Account Number,Employee Number
arun kumar 577arun kumar 577
can you please share your email id and then share my exact requirement. write here
Sharankumar DesaiSharankumar Desai
sharandesai.sd@gmail.com
+91 966 33 11 740
Abhishek BansalAbhishek Bansal
Hi Arun,

Please find the required code of your trigger below:
trigger updateOwner on Contact(before insert, before update){
	
	Set<Id> accIds = new Set<Id>();
	
	for(Contact con : trigger.new){
		if(con.AccountId != null){
			accIds.add(con.AccountId);
		}
	}
	
	Map<Id,Account> mapOfAccounts = new Map<Id,Account>([Select A__c,AccounNumber__c from Account where ID IN :accIds]);
	
	Set<String> setOfEmployeeNumber = new Set<String>();
	for(Account acc : mapOfAccounts.values()){
		if(acc.A__c != null){
			setOfEmployeeNumber.add(acc.A__c.split('-')[0]);
		}
	}
	
	Map<String,User> mapOfUser = new Map<String,User>();
	for(User usr : [Select EmployeeNumber from User where EmployeeNumber IN :setOfEmployeeNumber]){
		mapOfUser.put(usr.EmployeeNumber,usr);
	}
	
	for(Contact con : trigger.new){
		if(con.AccountId != null && mapOfAccounts.containsKey(con.AccountId) && con.AccounNumber__c == mapOfAccounts.get(con.AccountId).AccounNumber__c){
			String empId = mapOfAccounts.get(con.AccountId).A__c.split('-')[0];
			if(mapOfUser.containsKey(empId)){
				con.OwnerId = mapOfUser.get(empId).id;
			}
		}
	}
}


Please let me know if you have any issue in this.

Thanks,
Abhishek Bansal.

This was selected as the best answer
arun kumar 577arun kumar 577
Thank you so much abhishek for your time...and your code is working as i expected...