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

Must query entire org, yet too many records
I've run into a bit of a problem with the governor limits on the SOQL query rows returned. Here's my situation.
The code needs to query the organization for all accounts whose type is "Customer". Then, it needs to find the largest existing Account Number, and increment it by 1, storing that new value into an account that's type is updated to Customer, or a new Customer account.
Trigger:
Test Code:
Now, my problem is that when I attempt to deploy it from the Sandbox, I get an error "Too many query rows". Upon trying to LIMIT my query, I've discovered that the LIMIT occurs before the ORDER BY, so if I limit the query, I don't get all the records and may not actually get the highest account number (depending on how Salesforce returns the records). But if I don't limit the query, I get errors of Too many query rows.
Any advice?
The code needs to query the organization for all accounts whose type is "Customer". Then, it needs to find the largest existing Account Number, and increment it by 1, storing that new value into an account that's type is updated to Customer, or a new Customer account.
Trigger:
Code:
trigger Account_Number_Inc on Account (after insert, after update) { if(Trigger.new[0].Account_Number__c == null && Trigger.new[0].Type == 'Customer') { list<Account> AcctNums = [SELECT Account_Number__c From Account where Account_Number__c != null and Type = 'Customer' ORDER BY Account_Number__c DESC NULLS LAST]; System.assert(AcctNums != null); Long curhigh = long.valueOf(AcctNums[0].Account_Number__c); curhigh++; Account acc = new Account(Id=Trigger.new[0].Id); acc.Account_Number__c=String.valueOf(curhigh); update(acc); } }
Code:
public class AccountNumberTriggerTest { static testMethod void testAccountTrigger() { Integer highestnum = integer.valueOf([Select Account_Number__c from Account where Account_Number__c != null and Type = 'Customer' ORDER BY Account_Number__c DESC NULLS LAST][0].Account_Number__c); Account b = new Account(name='blah2', type='Customer'); insert b; Integer acctnum = integer.valueOf([Select Account_Number__c from Account where id =:b.id][0].Account_Number__c); System.assertEquals(highestnum+1, acctnum); } }
Now, my problem is that when I attempt to deploy it from the Sandbox, I get an error "Too many query rows". Upon trying to LIMIT my query, I've discovered that the LIMIT occurs before the ORDER BY, so if I limit the query, I don't get all the records and may not actually get the highest account number (depending on how Salesforce returns the records). But if I don't limit the query, I get errors of Too many query rows.
Any advice?
As mentioned above, the LIMIT executed before the ORDER BY, so as a result I only really get the first record returned by Salesforce, unordered.
I tested it in my dev org, and have two accounts already in existance. One with a number of 561, and one with a number of 1223. When I create a third, it got assigned 562, instead of the correct value of 1224. Hence my problem.
concurrent inserts will be both receive the same number, because your code can't see the other ongoing uncommitted transaction.
The reason we can't use an auto-number field is because we only need the Account Number field to update based on Type="Customer". To my knowledge you can't set criteria for auto-numbers.