0

I'm trying to get a code to work that will set map an opportunity owner to the account owner but I'm getting failures for two lines. Any ideas?

Broken lines start with a "**"

trigger SetOppOwnerToAcctOwner 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);
    }

}
1
  • 1
    Is the "failure" you are talking about that the lines are not covered in the code coverage? Or is there some functional error (and if so what is it)?
    – Keith C
    Commented Mar 6, 2014 at 20:19

2 Answers 2

4

It looks like your test data does not contain any Account objects related to your Opportunities if you're not hitting those lines.

Try using test data that looks something like the below (your objects may have more required fields).

It should be noted that this simple use case is probably better accomplished by using a Workflow Field Update rather than a trigger.

Account anAccount = new Account(Name = "Test Account");
insert an account;

Opportunity anOpportunity = new Opportunity(Name = "Test Opportunity", AccountId = anAccount.Id);
insert anOpportunity;
0

The issue must be Test data, make sure that you creating proper data and everything should be fine :)

And here is some code:

User usr_1 = new User(/** required fields **/);
insert usr_1;
User usr_2 = new User(/** required fields **/);
insert usr_2;

Account testAccount = new Account(OwnerId = usr_1.Id, 
                                  /** required fields **/  );
insert testAccount;

Opportunity testOpportunity = new Opportunity(AccountId = testAccount.Id, 
                                              OwnerId = usr_2.Id, 
                                              /** required fields **/ );
insert testOpportunity;

system.assertEquals(usr_1.Id, testOpportunity.OwnerId, 'Wrong Owner : ');

Also here is some alternative trigger code:

for(Opportunity opp : [SELECT Id, Account.OwnerId, AccountId 
                       FROM Opportunity 
                       WHERE ID IN : trigger.new]){
    if(opp.AccountId != null){
        opp.OwnerId = opp.Account.OwnerId;
    }
}

It should replace your logic

4
  • NPE for Opportunity created without Account?
    – Keith C
    Commented Mar 6, 2014 at 20:53
  • Can we see your test class?
    – EricSSH
    Commented Mar 6, 2014 at 23:34
  • Duplicate of adtennant's answer. Commented Mar 7, 2014 at 2:45
  • 1
    Check time before you blame someone please. If it does matter - @adtennant answer could be copy of mine. But in fact - it doesn't :) Commented Mar 7, 2014 at 5:05

You must log in to answer this question.

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