You need to sign in to do that
Don't have an account?
wixxey
Changing the owner of Contact
Hello,
I am having a problem in the code. The Contact object con is defined in code below, it is recieving all the values from the visualforce page. but before inserting this con object i want to change its owner field to the Account it is recieving in line #5. as this con object takes bydefault owner.
public with sharing class newContact { public Contact con {get;set;} public newContact(ApexPages.StandardController c) { con = (Contact)c.getRecord(); con.AccountId = ApexPages.currentPage().getParameters().get('pid'); } public PageReference save(){ insert con; return new PageReference('/index/index?id='+con.AccountId); }
Kindly Guide me if possible
A Owner field always points to a User or a QUEUE.
All Answers
Try this out
The above code looks for the account using an SOQL and assigns the ownerId FROM the Account
Hi,
Try this
If this post solves your problem, kindly mark it as solution. If it is helpful please throw kudos.
Thanks
Hi Wissey,
I would have written a pretty simple trigger on Contact and it would have taken care of owner assignment:
trigger AssignOwner on Contact(before insert, before update) {
List<Id> accountIds = new List<Id>();
Map<Id, Id> accountIdOwnerId = new Map<Id, Id>();
for(Contact con : Trigger.new) {
accountIds.add(con.AccountId);
}
for(Account acc : [select ownerid from Account where Id in: accountIds]) {
accountIdOwnerId.put(acc.Id, acc.ownerId);
}
for(Contact con : Trigger.new) {
con.ownerId = accountIdOwnerId.get(con.accountId);
}
}
Please post if you find any better solution or have any question.
-Vagish
Thanx for the reply souvik
I think you didnt get my point ...let me explain suppose i create an contact with the name XYZ in an Account ABC. so the contact owner value should be ABC. I dont want that both have same owner
A Owner field always points to a User or a QUEUE.
You can't make account as an owner of any record. Because, ownerid is a lookup field of type user object.
I am basically pointing toward a user , i am not giving a dynamic value
Search for a user with that Account name?
Yes I got it now, It might be too late. But Avidev9 was right.
A Owner field alwaysstores userId. We cannot store account name there.
Thanks
Thanx souvik9086 for All the help..