Apex Programming Language: Create business applications using Apex to extend and improve the usefulness of the Salesforce Platform by Apex Publishing

Apex Programming Language: Create business applications using Apex to extend and improve the usefulness of the Salesforce Platform by Apex Publishing

Author:Apex Publishing [Publishing, Apex]
Language: eng
Format: azw3, epub
Published: 2020-08-29T16:00:00+00:00


' Records has been deleted' );

Undelete Operation

You can undelete the record which has been deleted and is present in Recycle bin. All the relationships which the deleted record has, will also be restored.

Example

Suppose, the Records deleted in the previous example need to be restored. This can be achieved using the following example. The code in the previous example has been modified for this example.

// fetch the invoice created today

List <apex_invoice__c> invoiceList = [ SELECT id, Name , APEX_Status__c,

createdDate FROM APEX_Invoice__c WHERE createdDate = today];

List <apex_invoice__c> updatedInvoiceList = new List <apex_invoice__c> ();

APEX_Customer__c objCust = new APEX_Customer__C();

objCust. Name = 'Test' ;

// Inserting the Customer Records

insert objCust;

for ( APEX_Invoice__c objInvoice: invoiceList) {

if ( objInvoice. APEX_Status__c == 'Pending' ) {

objInvoice. APEX_Status__c = 'Paid' ;

updatedInvoiceList. add ( objInvoice);

}

}

// DML Statement to update the invoice status

update updatedInvoiceList;

// Prints the value of updated invoices

System . debug( 'List has been updated and updated values are' + updatedInvoiceList);

// Inserting the New Records using insert DML statement

APEX_Invoice__c objNewInvoice = new APEX_Invoice__c();

objNewInvoice. APEX_Status__c = 'Pending' ;

objNewInvoice. APEX_Amount_Paid__c = 1000 ;

objNewInvoice. APEX_Customer__c = objCust. id;

// DML which is creating the new record

insert objNewInvoice;

System . debug( 'New Invoice Id is ' + objNewInvoice. id);

// Deleting the Test invoices from Database

// fetch the invoices which are created for Testing, Select name which Customer Name

// is Test.

List <apex_invoice__c> invoiceListToDelete = [ SELECT id FROM APEX_Invoice__c

WHERE APEX_Customer__r. Name = 'Test' ];

// DML Statement to delete the Invoices

delete invoiceListToDelete;

system. debug( 'Deleted Record Count is ' + invoiceListToDelete. size());

System . debug( 'Success, ' + invoiceListToDelete. size() + 'Records has been deleted' );

// Restore the deleted records using undelete statement

undelete invoiceListToDelete;

System . debug( 'Undeleted Record count is ' + invoiceListToDelete. size()+ '. This should

be same as Deleted Record count' );



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.