1

I want to create a OwnerId method which takes in an opportunity as input , stores all OwnerId's from that Opportunity in a list and returns that list. Here is what I have so far:

public static OwnerId initOwnerId(Opportunity newOwner){
    List OwnerId = new List();
    for(List OwnerId : newOwner){
        OwnerId.add(newOwner.OwnerId);
    }
    insert OwnerId;
    return OwnerId;
}

Is this wrong? I'm not confident that this is the right way to do it. Any help is appreciated.

  • As Thomas mentioned, there is only ever one Owner on a record. If you can share your requirement we might be able to help you come up with a better solution. – Mike Chale Jun 4 '13 at 16:40
5

There are alot of things wrong with your approach, including:

  • Any object only has one OwnerId.
  • OwnerId is not a type that can be returned.
  • A single Opportunity cannot be the collection in a for loop.
  • OwnerId is not an object that can be inserted.

Here is one such method that provides the functionality you're looking for (in bulk), but I HIGHLY recommend you review the Apex docs and work through tutorials, or you will be asking alot of unnecessary questions. Check out the Apex workbook as one such source for tutorials.

public static Set<Id> getOwnerIds(List<Opportunity> opps) {
    Set<Id> ownerIds = new Set<Id>();
    for (Opportunity opp : opps) {
        ownerIds.add(opp.OwnerId);
    }
    // in case of non-inserted opportunities...
    ownerIds.remove(null);
    return ownerIds;
}
| improve this answer | |
  • Good advice Thomas – Martin Borthiry Jun 4 '13 at 16:35
  • 2
    Thanks, you're completely right. I made the mistake of thinking I'd need ownerId when what I actually need to create is a user method class. Sorry for the confusion, but thanks for the tutorials, I'm sure they'll be helpful – SSadat Jun 4 '13 at 17:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.