You need to sign in to do that
Don't have an account?
Walter@Adicio
How do I delete 1000 records at once?
I know there is a wealth of information out here on this and I am studying, but I haven't figure it out yet.
Do I just need to use a loop like this?
for(List<Account> a : [select id from Account where IsDeleted=false]){delete a;}
Not quite - the syntax is slightly wrong, and more importantly, as it stands, you could add more than 1000 items to the list which would cause an error.
Two options:
//For a 1000 or less Account [] a = [select id from Account where IsDeleted=false limit 1000]; delete a; //or List<Account> adelete = new List <Account>{}; For (Account k : [Select Id from Account limit 10000]){ adelete.add(k); if (adelete.size() > 998) { delete adelete; adelete.clear(); } } //You need this final clean up as the final list you creates is likely less than 998 delete adelete;
All Answers
Not quite - the syntax is slightly wrong, and more importantly, as it stands, you could add more than 1000 items to the list which would cause an error.
Two options:
//For a 1000 or less Account [] a = [select id from Account where IsDeleted=false limit 1000]; delete a; //or List<Account> adelete = new List <Account>{}; For (Account k : [Select Id from Account limit 10000]){ adelete.add(k); if (adelete.size() > 998) { delete adelete; adelete.clear(); } } //You need this final clean up as the final list you creates is likely less than 998 delete adelete;
ThomasTT
Datetime strStartDate; Datetime strEndDate; Integer BATCH_TO_PROCESS = 10; DateTime t = DateTime.now(); integer year = t.year(); strStartDate = Datetime.ValueOf(year + '-01-01 00:00:00'); strEndDate = Datetime.ValueOf(year + '-12-31 23:59:59'); Set<Id> ContactIdSet = new Set<Id>(); for(List<Contact> ContactList : [select Number_of_Cases__c, Number_of_Contracts__c, ID from Contact where createddate >= :strStartDate and createddate <= :strEndDate]) { for(Contact objContact : ContactList) { ContactIdSet.add(objContact.id); } if(intBatchCount<BATCH_TO_PROCESS) { //This is asynchronous method which processes updates BatchContactUpdate(ContactIdSet); ContactIdSet.clear(); //Increase batch count intBatchCount++; } } @future public static void BatchContactUpdate(Set<Id> pm_ContactIds) { List<Contact> contactsToUpdate = new List<Contact>{}; List<Contact> contacts = [select Id, Number_of_Cases__c, Number_of_Contracts__c from Contact where id IN :pm_ContactIds]; for(Contact c: contacts) { intCountForContact ++; if(pm_bIsIncludeNumberOfCases) c.Number_of_Cases__c = [select count() from Case where Contact.id = :c.id]; if(pm_bIsIncludeNumberOfContracts) c.Number_of_Contracts__c = [select count() from Contract where CustomerSigned.id = :c.id or Contract_Owner__c = :c.id]; //Add to contact update list contactsToUpdate.add(c); } //Update all records together update contactsToUpdate; }
What wrong am I doing here? I have a Visualforce page with custom controller containing above code which is being called on click of the “Run” button.
It throws error as “Too many SOQL queries:101” error if the contact records in the current year are more than 900 on average.
What I can’t figure out is why I am getting limitation error of 100 SOQL queries even though I am using future annotation. If I do not use future annotation, I can process only 100 records.
Any suggestion on how to overcome this situation? (I am using approach suggested here: http://wiki.developerforce.com/index.php/Apex_Code_Best_PracticesI am afraid that this approach is limited only for calls inside trigger. )
Thanks,
Chandrakant M