3

I'm trying to find a way to map the opportunity owner field to the account owner field when a new opportunity is created. So if an opportunity is created by Jon for Account XYZ, which is owned by Jill, then the opportunity owner would be automatically swapped to Jill on creation.

Unfortunately I can't do this with a standard workflow. Any ideas?

1
  • if you are developer, you can do that by trigger for sure.
    – brovasi
    Commented Mar 5, 2014 at 22:01

1 Answer 1

3

Maybe this will get you started :)

trigger setOwner on Opportunity (before Insert){

    Set<ID> acctIDs = New Set<ID>();
    Map<ID,ID> accToOwner = New Map<ID,ID>();

    for(Opportunity opp : trigger.new){

        acctIDs.add(opp.accountID);

    }

    for(Account a : [Select OwnerID From Account Where ID IN :acctIDs])
        accToOwner.put(a.id,a.ownerID);


    for(Opportunity opp : trigger.new){
        if(accToOwner.containsKey(opp.AccountID))
            opp.OwnerID = accToOwner.get(opp.AccountID);
    }


}
2
  • Awesome! Got 77% coverage with this (minus the last curly bracket). Thanks so much!
    – Alex
    Commented Mar 6, 2014 at 20:10
  • Good to know. Mark it as the answer if it solves your issue so other with the same issue or use case will know it helped.
    – Eric
    Commented Mar 7, 2014 at 14:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .