0

Hello guys i wrote to update class for case object and this object have a many records thats why i cant execute Soql queries, I encountering error: "So many soql queries",i searched i found one way, i can write schedule in execute anonymous window, But! i need the create changing variable rather when i execute anonymous window i wanna get +75 more --OFFSET--

public class CaseUpdateRecords {


public static void UpdateRecordsCase()
{
    List <Case> cssProduct = new List<Case>();    

CssProduct = [Select Id,Recordtype.Id,Yerine_Verilen_Urun__c,Yerine_Verilen_r_n_Stok_Kodu__c from case 
where RecordType.Id = '0121t000000dHfWAAU' AND Yerine_Verilen_Urun__c != '' 
Limit 75 OFFSET '***When execute anonymous here will change +75***'];

    system.debug(CssProduct); //yukarıdaki atılan listenin logu

    for(Case css_i : CssProduct)
    {
        if (css_i.Yerine_Verilen_Urun__c != null)
        {
        Map<Id,Product2> PrdMap = new Map<Id,Product2>(); //Product Map oluşumu product Id'ye göre Product Code'u çekmek için
        PrdMap = new Map<Id,Product2>([Select ProductCode From Product2 Where Id = : css_i.Yerine_Verilen_Urun__c]);
        system.debug(PrdMap);
        system.debug('Case Id = ' + css_i.Id);
        system.debug('Yerine Verilen Stok Kodu 1. Hali'); //Yerine verilen ürün stok içersinde bulunan StockCode alanının ilk hali
        system.debug(css_i.Yerine_Verilen_r_n_Stok_Kodu__c);
        css_i.Yerine_Verilen_r_n_Stok_Kodu__c = PrdMap.get(css_i.Yerine_Verilen_Urun__c).ProductCode;//StockCode Aktarma**
        System.debug('Yerine Verilen Stok Kodu 2. Hali');
        system.debug(css_i.Yerine_Verilen_r_n_Stok_Kodu__c); //Yerine verilen ürün stok içersinde bulunan StokCode alanının ikinci hali

        }
    }

    update CssProduct;

   }
}

1 Answer 1

1

You can use variables in your SOQL expression to change the limit and offset e.g.

Integer anOffset = 75;
Integer aLimit = 75;

List<Contact> contacts = [SELECT Id FROM Contact LIMIT :aLimit OFFSET :anOffset];

So, you could pass something like that through when you run your class.

However, the maximum offset is 2000 (https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_offset.htm). So, if you have many records, this approach won't scale.

You would probably be best looking at using a batch class instead. That way, Salesforce takes care of breaking a large query into bite-sized chunks. See Using Batch Apex

1
  • Auch i be sad now :( , i didint know about 2000 offset problem , now i have to found another process thanks Aidan Sep 4, 2019 at 10:10

You must log in to answer this question.

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