You need to sign in to do that
Don't have an account?

Updating Custom Account Field Not Working when Updating several Opportunities At Once
I have a custom field in Accounts which keeps track of the opportunities it has. when I update one opportunity it works correctly. When I update severa opportunties at once, then the custom account field does not get updated although the opportunities themselves are correctly updated. the custom field is AC_Marketing_Category__c. I think it has to do with the account lists, but I am not sure how to change them.
try{ //the trigger will update the account's Accelero Connect Category field if (! trigger.isDelete) { List<Id> oppIds = new List<Id>() ; List<Id> AccountIds = new List<Id>() ; List<Account> AcctToUpdate = new List<Account>() ; for (opportunity op : trigger.New) { oppIds.add(op.Id); AccountIds.add(op.AccountId); } Map<Id,Opportunity> oppMap = new Map<Id,Opportunity>([Select id, StageName from Opportunity where Accountid in :AccountIDs and name = 'Accelero Connect' and Status__c != 'Inactive']) ; Map<Id,Account> acctMap = new Map<Id,Account>([select id, AC_Marketing_Category__c, name, Closed_Won_Opps__c, Type from Account where id in :AccountIDs]); for (opportunity op : trigger.New){ //Find the account for this opportunity which is being updated Account oAccount = acctMap.get(op.AccountId); Opportunity ACOpportunity = oppMap.get(op.Id); string stagelist; if (oppMap.isEmpty()){ //No AC opportunities if (oAccount.Closed_Won_Opps__c == 0 ) { oAccount.AC_Marketing_Category__c = 5; } else { oAccount.AC_Marketing_Category__c = 4; } } else { //There are ACopportunities so see how many of them are closed/won Integer iCountClosedWon = 0; for(Opportunity acMap: oppMap.values()){ if (acMap.StageName == 'Closed Won') { iCountClosedWon += 1; } } if (iCountClosedWon > 0) { oAccount.AC_Marketing_Category__c = 1; } else { if (oAccount.Closed_Won_Opps__c == 0){ oAccount.AC_Marketing_Category__c = 3; //update oAccount; } else { oAccount.AC_Marketing_Category__c = 2; } } } AcctToUpdate.add(oAccount); } update AcctToUpdate; }
try to use parent child relationship query.
select id, AC_Marketing_Category__c, name, Closed_Won_Opps__c, Type, (Select id, StageName fromOpportunity where name ='Accelero Connect' and Status__c != 'Inactive') from Account where idin :AccountIDs
trigger sampleAcctupdate on opportunity(after insert, after update)
{
set<ID> acctIDs= new set<ID>();
for(Opportunity opp:trigger.new)
{
acctIDs.add(opp.accountid);
}
List<Account> updateAccounts= new List<Account>();
for(Account acc2:[select id, name,(select id, name,SampleText__c from Opportunities ),Text1__c from Account where id IN:acctIDs])
{
List<Opportunity> opp1=acc2.Opportunities;
if(opp1.size()>0)
{
for(Opportunity opp3:opp1)
{
if(opp3.SampleText__c=='ddd')
{
acc2.Text1__c='ZZZZ';
}
else
{
acc2.Text1__c='AAAA';
}
}
}
updateAccounts.add(acc2);
}
if(updateAccounts.size()>0)
{
update updateAccounts;
}
}