forked from deepikakhanna/SalesforcePlatformDeveloper1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tutorial11DatabaseAllorNoneParameter
39 lines (35 loc) · 1.8 KB
/
tutorial11DatabaseAllorNoneParameter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//http://mytutorialrack.com/
//Check the complete course here https://mytutorialrack.com/salesforce-platform-developer-1-certification-course/
//Insert Operation Using Database methods
//Insert Customer Records First using simple DML Statement. This Customer Record will be used when we will create Invoice Records
APEX_Customer__c objCust = new APEX_Customer__C();
objCust.Name = 'Test';
insert objCust;//Inserting the Customer Records
//Insert Operation Using Database methods
APEX_Invoice__c objNewInvoice = new APEX_Invoice__c();
List<apex_invoice__c> InvoiceListToInsert = new List<apex_invoice__c>();
objNewInvoice.APEX_Status__c = 'Pending';
objNewInvoice.APEX_Customer__c = objCust.id;
objNewInvoice.APEX_Amount_Paid__c = 1000;
InvoiceListToInsert.add(objNewInvoice);
//We will add a new Invoice record
APEX_Invoice__c objNewInvoice2 = new APEX_Invoice__c();
objNewInvoice2.APEX_Status__c = 'Pending';
objNewInvoice2.APEX_Amount_Paid__c = 1000;
InvoiceListToInsert.add(objNewInvoice2);
Database.SaveResult[] srList = Database.insert(InvoiceListToInsert, true);//Database method to insert the records in List
// Iterate through each returned result by the method
for (Database.SaveResult sr : srList) {
if (sr.isSuccess()) {
// This condition will be executed for successful records and will fetch the ids of successful records
System.debug('Successfully inserted Invoice. Invoice ID: ' + sr.getId());//Get the invoice id of inserted Account
}
else {
// This condition will be executed for failed records
for(Database.Error objErr : sr.getErrors()) {
System.debug('The following error has occurred.'); //Printing error message in Debug log
System.debug(objErr.getStatusCode() + ': ' + objErr.getMessage());
System.debug('Invoice oject field which are affected by the error: ' + objErr.getFields());
}
}
}