0

I was wondering if there is any way of updating an account field in APEX if i have the account ID in the Triggered Object. Right now i am running into the issue that I can not do the following (Trigger is on the Opportunity).

List <Account> AccountsInHierarchy = [SELECT Field_A, Field_B, Field_C
    FROM Account
    WHERE Ultimate_Parent_ID__c = Trigger.new.Account.Ultimate_Parent_Id__c];

In order to work around this I simplified the Query by taking out the "WHERE" clause and using an if statement in the TRIGGER-FOR-LOOP. Whenever I do this, though, I get the following error:

System.LimitException: Too many query rows: 50001

I have already tried limiting - using the query LIMIT function - the numbers of records returned by my SOQL Query but the error persisted. Any help would be much appreciated.

  • Is this actually compiling? Your account list variable name is broken up into 3 words and you are getting an account field from a list of Opportunities (trigger.new is a List of sObjects) – brezotom Apr 26 '18 at 23:52
1

The syntax of your SOQL query as shown in your question is not valid. I'm surprised if this actually compiles, so it's not clear to me how you obtain the specific limits error you are seeing. Based on your edit, it sounds like you attempted to run the query without filters and perform filtering in Apex, which in an organization of any size will throw a limits error.

You appear to be trying to use an Apex bind variable to reference a value within your SOQL, but the required colon : before the Apex expression is missing.

Additionally, the value that you're attempting to bind to doesn't make sense. Trigger.new is a List<Opportunity>, which doesn't have the property Account - that belongs to each individual Opportunity.

I suspect what you want to do is something like this:

Set<Id> accountIds = new Set<Id>();
Set<Id> ultimateParentIds = new Set<Id>();

// Iterate over Opportunities to collect their Accounts
for (Opportunity o : Trigger.new) {
    accountIds.add(o.AccountId);
}

// Query for the Accounts' Ultimate_Parent_Id__c field.
for (Account a : [SELECT Ultimate_Parent_Id__c FROM Account WHERE Id IN :accountIds) {
    ultimateParentIds.add(a.Ultimate_Parent_Id__c);
}

// Finally query for the ultimate parent accounts.
List <Account> AccountsInHenter = [SELECT Field_A, Field_B, Field_C
FROM Account
WHERE Ultimate_Parent_ID__c IN :ultimateParentIds];

Since you cannot directly access fields of related objects via the Trigger.new context variable, you must first query for Accounts to obtain the Ultimate_Parent_ID__c field contents of the Accounts associated with these Opportunities. Then, you'd accumulate those values in a Set<Id> and perform a second query against Account to locate those records.

  • Thanks David this makes total sense! It did not compile that way I was just trying to explain my issue and what I was trying to achieve. Thanks for the prompt response, this makes a lot of sense. I'll give it a go now but i'm sure its gonna work this way! Appreciate your help! – user56138 Apr 27 '18 at 0:02

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.