Rabu, 05 April 2017

Soql for loop

😀 lanjutan dari artikel menghindari governor execution limits apex salesforce.com

Method ini bisa digunakan jika code berjalan dalam mode syncronously, ada batasan limit heap size sebesar 6 mb, dan juga jika data set nya besar misal 40000 row, limit heap size akan tercapai.

Note: batasan record untuk soql adalah 50000 ribu record.


//akan mengakibatkan error jika return querynya besar
Account[] accts = [SELECT id FROM account];

gunakan soql for loop seperti dibawah ini untuk menghindari limitasi heap size
Account[] accts = new Account[];

for (List<Account> acct : [SELECT id, name FROM account
                            WHERE name LIKE 'Acme']) {
    // Your logic here
    accts.add(acct);
}

update accts;

Efisiensi soql query

😀 lanjutan dari artikel menghindari governor execution limits apex salesforce.com

Limit jumlah query pertransaksi di apex sejumlah 100, jadi hati-hati untuk penggunaan query, contohnya hindari query didalam loop, alternatifnya kita bisa query semua data yang diperlukan terlebih dahulu diluar loop, sehingga fungsi loop hanya untuk mengolah data extrak dari query sebelumnya.


dibawah adalah contoh code yang kurang efisien, menyebabkan error max 100 query per transakasi atau 150 dml.

trigger accountTestTrggr on Account (before insert, before update) {
   
   //For loop to iterate through all the incoming Account records
   for(Account a: Trigger.new) {
        
      List<Contact> contacts = [select id, salutation, firstname, lastname, email 
                        from Contact where accountId = :a.Id];
     
      for(Contact c: contacts) {
        //akan mengabibatkan error jika query lebih dari 100
        //contoh query ke object lain
        ObjectLain__c ol = [select id,name from ObjectLain__c where contactId = :c.id];
        //akan mengakibatkan error jika contact lebih dari 150                        
         update c;
      }       
   }
}

Dibawah adalah contoh code yang lebih efisien, yaitu query langsung account dengan contact yang kemudian di oleh didalam loop tanpa query object contact.

trigger accountTestTrggr on Account (before insert, before update) {
  //This queries all Contacts related to the incoming Account records in a single SOQL query.
  //This is also an example of how to use child relationships in SOQL
  List<Account> accountsWithContacts = [select id, name, (select id, salutation, description, 
                                                                firstname, lastname, email from Contacts) 
                                                                from Account where Id IN :Trigger.newMap.keySet()];
   
  List<Contact> contactsToUpdate = new List<Contact>{};
  // For loop to iterate through all the queried Account records 
  for(Account a: accountsWithContacts){
     // Use the child relationships dot syntax to access the related Contacts
     for(Contact c: a.Contacts){
      System.debug('Contact Id[' + c.Id + '], FirstName[' + c.firstname + '], LastName[' + c.lastname +']');
      c.Description=c.salutation + ' ' + c.firstName + ' ' + c.lastname; 
      contactsToUpdate.add(c);
     }       
   }
      
   //Now outside the FOR Loop, perform a single Update DML statement. 
   update contactsToUpdate;
}

Jumat, 31 Maret 2017

Menghindari governor execution limits di apex code salesforce.com

he he, ceritanya mau nulis artikel ini biar nanti tidak lupa kalau ada implementasi / coding khususnya apex code di salesforce.com. Berawal dari pekerjaan kantor maintenece produknya salesforce dari pelanggan, ceritanya fixing bug dari codingan orang lain neh,,, berasa pengen mual neh haha, entah dikejar deadline atau bonus, ehhhhhh# jadi kualitas code yang dihasilkan kurang bagus. Saya berpendapat seperti ini karena memang banyak error ditemukan oleh user di server productionnya.

Diartikel ini saya akan membahas salah satu bug fix, error yang disebabkan oleh governor limit khususnya apex salesforce, error ini akan ditemukan jika proses eksekusi data akan melibatkan banyak data, sebagai contoh ya, user mengupdate record opportunity yang mempunyai child opportunity line item, error tidak akan ditemui jika child (opportunity line item) hanya mempunyai jumlah yang sedikit, jika banyak akan mengakibatkan error karena limit dari salesforce itu sendiri.

Beberapa contoh code untuk menghindari governor limit:
Bulkifying DML calls
singkatnya adalah untuk mengupdate data secara bersama yang dikumpulkan di sebuah tampungan list, contohnya dari pada kita mengupdate tiap record satu persatu di dalam loop, lebih efisien jika kita mengumpulkan data yang akan diupdate di dalam list kemudian mengupdate secara bersama data di list tersebut (bulkifying).

// Contoh yg kurang efisien, akan menyebabkan limit jika list lebih dari 150
for(opportunityLineItem li : liList) {
    if (li.Units > 10) {
        li.Description = 'Banyak rek!!';
    }
    
    update li;
}


//Contoh bulkify code
List<OpportunityLineItem> updatedList = new List<OpportunityLineItem>();

for(OpportunityLineItem li : liList) {
    if (li.Units > 10) {
        li.Description = 'Banyak rekk!!';
        updatedList.add(li);
    }
}

update updatedList;

Efisiensi soql query
Soql for loop