Each element in the SaveResult array corresponds to the sObject array passed as

Each element in the saveresult array corresponds to

This preview shows page 247 - 250 out of 487 pages.

database methods. Each element in the SaveResult array corresponds to the sObject array passed as the sObject[] parameter in the database method, that is, the first element in the SaveResult array matches the first element passed in the sObject array, the second element corresponds with the second element, and so on. If only one sObject is passed in, the SaveResults array contains a single element. A SaveResult object has the following methods: Description Type Name If an error occurred, an array of one or more database error objects providing the Database.Error [] getErrors error code and description. For more 245 Reference Update Operation
Image of page 247
Description Type Name information, see Database Error Object Methods on page 321. The ID of the sObject you were trying to insert or update. If this field contains ID getId a value, the object was successfully inserted or updated. If this field is empty, the operation was not successful for that object. A Boolean that is set to true if the DML operation was successful for this object, false otherwise. Boolean isSuccess DML Statement Example The following example updates the Description__c field on a single invoice statement: Invoice_Statement__c inv = new Invoice_Statement__c( Description__c= 'Invoice 1' ); insert inv; Invoice_Statement__c myInvoice = [SELECT Id, Description__c FROM Invoice_Statement__c WHERE Id = :inv.Id]; myInvoice.Description__c = 'New description' ; try { update myInvoice; } catch (DmlException e) { // Process exception here } Note: For more information on processing DmlException s, see Bulk DML Exception Handling on page 252. Database Method Example The following example updates the Description__c field on a single invoice statement: Invoice_Statement__c inv = new Invoice_Statement__c( Description__c= 'Invoice 1' ); insert inv; Invoice_Statement__c myInvoice = [SELECT Id, Description__c FROM Invoice_Statement__c WHERE Id = :inv.Id]; myInvoice.Description__c = 'New description' ; Database.SaveResult SR = database. update (myInvoice); for (Database.Error err: SR.getErrors()) { // process any errors here } 246 Reference Update Operation
Image of page 248
Upsert Operation The upsert DML operation creates new sObject records and updates existing sObject records within a single statement, using an optional custom field to determine the presence of existing objects. DML Statement Syntax upsert sObject opt_external_id upsert sObject[] opt_external_id opt_external_id is an optional variable that specifies the custom field that should be used to match records that already exist in your organization's data. This custom field must be created with the External Id attribute selected. Additionally, if the field does not have the Unique attribute selected, the context user must have the View All object-level permission for the target object or the View All Data permission so that upsert does not accidentally insert a duplicate record.
Image of page 249
Image of page 250

You've reached the end of your free preview.

Want to read all 487 pages?

  • Summer '18
  • alan li
  • Apex, Bankruptcy in the United States

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture