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
wixxeywixxey 

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

Best Answer chosen by Admin (Salesforce Developers) 
Avidev9Avidev9
Well I see now. I dont think you will be able to do this.
A Owner field always points to a User or a QUEUE.

All Answers

Avidev9Avidev9

Try this out 

 

public with sharing class newContact {
    public Contact con {
        get;
        set;
    }

    private User accOwner;

    public newContact(ApexPages.StandardController c) {
        con = (Contact) c.getRecord();
        con.AccountId = ApexPages.currentPage().getParameters().get('pid');
        //check if there is a value  
        if (con.AccountId != null) {
            List < Account> accList = [SELECT Id, OwnerId,Name FROM Account WHERE Id =:con.AccountId];
            //check if matching records found    
            if (!accList.isEmpty()) {
               List<user> users = [SELECT Id FROM User WHERE Name =:accList[0].Name LIMIT 1];
if(!users.isEmpty()){
accOwner = users[0];
} } } } public PageReference save() { if (accOwner != null) { con.OwnerId = accOwner.Id; } insert con; return new PageReference('/index/index?id=' + con.AccountId); }

 The above code looks for the account using an SOQL and assigns the ownerId FROM the Account

souvik9086souvik9086

Hi,

Try this 

 

public with sharing class newContact {
    public Contact con {get;set;}
public Account acc{get;set;} public newContact(ApexPages.StandardController c) { con = (Contact)c.getRecord(); con.AccountId = ApexPages.currentPage().getParameters().get('pid');
acc = [SELECT ID,NAME, OWNERID FROM ACCOUNT WHERE id =: con.accountId]; } public PageReference save(){ con.ownerid = acc.ownerid;
insert con; return new PageReference('/index/index?id='+con.AccountId); }

If this post solves your problem, kindly mark it as solution. If it is helpful please throw kudos.

Thanks

vagishvagish

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

wixxeywixxey

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

Avidev9Avidev9
Well I see now. I dont think you will be able to do this.
A Owner field always points to a User or a QUEUE.
This was selected as the best answer
vagishvagish

You can't make account as an owner of any record. Because, ownerid is a lookup field of type user object.

wixxeywixxey
thanx avi for helping

I am basically pointing toward a user , i am not giving a dynamic value
Avidev9Avidev9
Are you saying something like this.
Search for a user with that Account name?
wixxeywixxey
Yes Exactly, the id of the account is recieving.
Avidev9Avidev9
Just updated my answer. please check
wixxeywixxey
Thanx AviDev for All the Help
souvik9086souvik9086

Yes I got it now, It might be too late. But 

wixxeywixxey

Thanx souvik9086 for All the help..