0

In the following example, where it says "xxxxxx", is it possible to write to a field on ACCOUNT (parent) within this code, somehow referencing it from the Opportunity that is currently being touched? I know I have AccountID to work with in the Opp itself, but the only time I'm touching Account is when I create the KeySet of (bulkified) Account(s) being dealt with - I can't use acctLoop since of course it's a closed FOR by that point.

Map<String, Account> acctMap = new Map<String, Account>();

for (Account acctLoop : System.Trigger.new) 
{ 
    acctMap.put(acctLoop.Id, acctLoop);
}

for ( Opportunity oppLoop1 : [ SELECT Id FROM Opportunity WHERE AccountId IN :acctMap.KeySet() ] ) 
{
oppLoop1.Name = 'testing xxx';  // testing..
update(oppLoop1);
--- xxxxxx ---
}

I was thinking of POSSIBLY trying to open the FOR loop to encompass the Opp Loop, and clear the acctMap at tne end of the main FOR loop (overkill, but then I'd have access to acctLoop I believe). Would that be a hack way to get it done? Is there a better way? Can I do something akin to "(opp.accountid).FieldName = 'xxx'"?

ADDED: The other idea would be to omit the Map and just wrap the Opp FOR inside the Acct FOR, and reference "AccountId IN acctLoop.Id"... if that makes sense.

1
  • You cannot wrap the Opp FOR inside the Acct FOR since you would end up running a query inside the Opp's for loop!!!
    – Rao
    Jan 4, 2013 at 21:33

1 Answer 1

1

Looking into ur code the

[ SELECT Id FROM Opportunity WHERE AccountId IN :acctMap.KeySet() ]

why are you even constructing the map of accountid and accounts, are you using the map elsewhere?

if not the query will work without the map as

[ SELECT Id FROM Opportunity WHERE AccountId IN : Trigger.new ]

to answer your question try using
/edit:

list<opportunity> opp = [ SELECT Id,account.name FROM Opportunity WHERE AccountId IN : Trigger.new ]

for(opportunity o :opp){
o.account.name = 'My test pass';
}

Hope this helps!!!

8
  • Map is force of habit from Bulkifying things on my end. I can try what you say and see what happens....
    – AMM
    Jan 4, 2013 at 21:33
  • You dont need the map in this scenario if the above code is all you have !!! Trigger.new is a list of all the changes that are to be committed to you DB and will have all the info related to the account(s) you work on at any given context.
    – Rao
    Jan 4, 2013 at 21:34
  • I think there was confusion on my "testing" variable. It's on the OPP, not on the Account. BUT... if I'm not mistaken, your code still gives me insight. Working........
    – AMM
    Jan 4, 2013 at 21:37
  • My use of your "list" is obviously off because it's unrecognized. I've barely ever touched "list" yet. :-/
    – AMM
    Jan 4, 2013 at 21:39
  • 1
    I added an "update(o)" to it and it seems to work.. so I think I'm good to go once I test a couple more things. Thanks SO much!
    – AMM
    Jan 4, 2013 at 21:47

You must log in to answer this question.

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