Showing posts with label Database Class. Show all posts
Showing posts with label Database Class. Show all posts

Tuesday, August 28, 2018

How to get the Failure Record Ids From Database.SaveResult[]

if You Need To check The Success Record Ids and Failure Record Ids.
If DML Operation is Success Will Get Record Id From Database By Using getId()  Method in Database.SaveResult Class.
If any DML Operation Fails while Processing The Record no way to Get the Failure Record Id in
Database.Error Class

To Get the Failure Record Id, We can use List index Property to get the Id of the failed record.

Check Bellow code
list<Account> lstAccToUpdate = new list<Account>();
list<Account> lstAccounts = [Select id, Name, Industry From Account limit 10];
for(Account aIterator : lstAccounts) {
    aIterator.Industry = 'Banking';
    lstAccToUpdate.add(aIterator);
}
if(lstAccToUpdate != null && !lstAccToUpdate.isEmpty()) {
    Database.SaveResult[] result = Database.update(lstAccToUpdate,false);
    for(Integer i=0; i < result.size(); i++) {
        if(result.get(i).isSuccess()) {
            System.debug('Records are updated Successfully');
        } 
        else if(!result.get(i).isSuccess()) {
            Database.Error errors =  result.get(i).getErrors().get(0);
            System.debug('Error Occurs While Processing The Record'+errors.getMessage());
            System.debug('Failure Record Ids While Updating'+result.get(i).Id);
        }
    }
}

Let us know if you have any queries.
Happy Learning!!

Monday, August 27, 2018

How To Debug The Database.getQueryLocator(query)

If you Need to debug The Data in Database.getQueryLocator(query) use Bellow Sample Code to Debug the Data.
The Return Type Of The Database.getQueryLocator(query) is Database.QueryLocator.
Open Anonymous Window in Developer Console Copy and Paste Bellow Code.

Sample Code

String strQuery;
strQuery = 'Select Id.Name,Industry From Account limit 10';
Database.QueryLocator qu = Database.getQueryLocator(strQuery);
// Get an iterator
Database.QueryLocatorIterator it = qu.iterator();
// Iterate the records
while(it.hasNext()) {
    Account a = (Account)it.next();
    System.debug('====Account Values===='+a);
}

Result:
Let us Know If You Have any Queries.
Happy Learning !!!