+ Start a Discussion
DeptonDepton 

trigger to get the account owner name

Hi,

 

I have a custom object "xyz" with a look up field to accounts

 

We need to see the name of the account owner, I cannot get it via formula (well, I could, but then I would need to modify the formula each time a new user is created and TEXT( Account__r.Ownership ) didn´t work)

 

I guess this is a simple trigger, to get the account owner name populated in the "xyz" object...

 

 

Any sample trigger you may have please to direct me to the right direction....

 

Thank you!!:) 

 

Best Answer chosen by Admin (Salesforce Developers) 
PkSharmaPkSharma

Hi,

 

You can not get ownername direct by formula field and trigger also ,may this trigger help you.

trigger trgAccOwner on TwoLookUpObject__c (before insert,before update)
{
    Set<Id> accid=new  Set<Id>();
    Set<Id> accownerid=new  Set<Id>();
    for(TwoLookUpObject__c tl:Trigger.new)
    {
        accid.add(tl.account__c);
    }
    map<id,account> mpacc=new map<id,account>([select id,name,ownerid from account where id in :accid]);
    for(account acc:mpacc.values())
    {
        accownerid.add(acc.ownerid);
    }
    map<id,user> mpuser=new map<id,user>([select id,name from user where id in :accownerid]);
    if(mpuser.size()>0)
    {
        for(integer i=0;i<Trigger.new.size();i++)
        {
            Trigger.new[i].AccOwnerName__c=mpuser.get(mpacc.get(Trigger.new[i].account__c).ownerid).name;
        }
    }

}

 

where AccOwnerName__c is the text field for store owner name.

All Answers

PkSharmaPkSharma

Hi,

 

You can not get ownername direct by formula field and trigger also ,may this trigger help you.

trigger trgAccOwner on TwoLookUpObject__c (before insert,before update)
{
    Set<Id> accid=new  Set<Id>();
    Set<Id> accownerid=new  Set<Id>();
    for(TwoLookUpObject__c tl:Trigger.new)
    {
        accid.add(tl.account__c);
    }
    map<id,account> mpacc=new map<id,account>([select id,name,ownerid from account where id in :accid]);
    for(account acc:mpacc.values())
    {
        accownerid.add(acc.ownerid);
    }
    map<id,user> mpuser=new map<id,user>([select id,name from user where id in :accownerid]);
    if(mpuser.size()>0)
    {
        for(integer i=0;i<Trigger.new.size();i++)
        {
            Trigger.new[i].AccOwnerName__c=mpuser.get(mpacc.get(Trigger.new[i].account__c).ownerid).name;
        }
    }

}

 

where AccOwnerName__c is the text field for store owner name.

This was selected as the best answer
DeptonDepton

Thank you!!!!!!!!!!!!!!

 

:))

Mark SchaferMark Schafer

I'm looking to do the same thing and am brand new to Apex. Thanks for posting this.

 

I'm actually receiving the following error when I try to create the trigger in my Sandbox. The only thing I changed is the name of the target text field to match my custom field and "account__c" to simply "account" because I received an error about that and though a standard field doen't need the __c suffix, but I could be wrong.

 

Compile Error: Incompatible element type SOBJECT:Account for collection of Id at line 7 column 9 

 

I wasn't sure if ([select id,name,ownerid from account where id in :accid]) was code or an instruction. I left it as is, beucase wondered if it should be replaced by something. 

 

Thanks for any help!

Mark SchaferMark Schafer

And I also switched TwoLookUpObject__c to Opportunity. To be more specific, I'm looking to pull the Account owner name into a custom text field on the Opportunity object.

DeptonDepton
can you copy and paste your code im the "insert code" table 

like i am doing

I will be easier to understand!

:)

 

Mark SchaferMark Schafer

New to this- can do. I can up with something like this: 

 

trigger trgAccOwner on Opportunity (before insert,before update)
{
    Set<Id> accid=new  Set<Id>();
    Set<Id> accownerid=new  Set<Id>();
    for(TwoLookUpObject__c tl:Trigger.new)
    {
        accid.add(tl.account);
    }
    map<id,account> mpacc=new map<id,account>([select id,name,ownerid from account where id in :accid]);
    for(account acc:mpacc.values())
    {
        accownerid.add(acc.ownerid);
    }
    map<id,user> mpuser=new map<id,user>([select id,name from user where id in :accownerid]);
    if(mpuser.size()>0)
    {
        for(integer i=0;i<Trigger.new.size();i++)
        {
            Trigger.new[i].AccOwnerName__c=mpuser.get(mpacc.get(Trigger.new[i].account.ownerid).name;
        }
    }
}

 

Thanks for the help!

Gdickens3Gdickens3

Did you resolve this code?  I am getting the same error on that line.