+ Start a Discussion
arun kumar 577arun kumar 577 

urgent requirement on contact ownerId please give me best solution

Hi guys,

I have one requirement like this. I have account that has two custom fields one is A and second one is B. now requirement is when ever any records are inserted and updated in contact then contact owner should be custom field value of A .

for example:
In account object:
account number=12345,
A__c=bobby;
once applied the trigger we need show like this
contact. ownerId=bobby;
when accountId!=null && contact.accountNumber__c==contact.account.accountNumber;

this is what i am expected from you guys.. Please help me..

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

Please find your required trigger code 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, AccountNumber__c from Account where Id IN :accIds]);
	
	for(Contact con : trigger.new){
		if(con.AccountId != null && mapOfAccounts.containsKey(con.AccountId) && con.AccountNumber__c == mapOfAccounts.get(con.AccountId).AccountNumber__c){
			con.OwnerId = mapOfAccounts.get(con.AccountId).A__c;
		}
	}
}


Let me know if you need any further help on this. Please mark this question as Solved if your requirement is achieved.

Thanks,
Abhishek Bansal.

All Answers

Abhishek BansalAbhishek Bansal
Hi Arun,

Please find your required trigger code 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, AccountNumber__c from Account where Id IN :accIds]);
	
	for(Contact con : trigger.new){
		if(con.AccountId != null && mapOfAccounts.containsKey(con.AccountId) && con.AccountNumber__c == mapOfAccounts.get(con.AccountId).AccountNumber__c){
			con.OwnerId = mapOfAccounts.get(con.AccountId).A__c;
		}
	}
}


Let me know if you need any further help on this. Please mark this question as Solved if your requirement is achieved.

Thanks,
Abhishek Bansal.

This was selected as the best answer
arun kumar 577arun kumar 577
Thanks Abhishek for your reply 

but I tried this still i did not get the result as i expected.
Abhishek BansalAbhishek Bansal

Hi Arun,

Is there any error in the code ? I have just provided you the code that will nearly achieve your requirement, if it is not working then you should do some kind of debugging and find out at what extent this code is working for you. Let me know if you need any other help.

Thanks,
Abhishek Bansal.

arun kumar 577arun kumar 577
Hi abhishek,

there is no error but i can't fetch that A__c field value as a contact owner. i dont know where it is strucking...please do this and send me as soon as possible.

thanks
Abhishek BansalAbhishek Bansal

Hi Arun,

What is the type of the field A__c ?

Thanks,
Abhishek Bansal.

arun kumar 577arun kumar 577
It is text field
 
Abhishek BansalAbhishek Bansal
How can we set a text field value in the ownerId field and if A__c is a text field and conatins the name of the User then there are chances that two users have same name so which user will you set as Owner on contact. Please store the user value in a lookup field that points to a particular user and then use this field to set owner on the contact.

Thanks.
arun kumar 577arun kumar 577
yes Abhishek you are right...

If it is A__c is look up data type it will work with your code..

Thank so much Abhishek for your time.
arun kumar 577arun kumar 577
can you please share your number to this email arun111nani@gmail.com. I can directly contact you if i have any requirement. Please dont mind for asking like this.