APEX学习笔记(四)-Apex - 数据库方法

Apex - 数据库方法

Database.insert(listName,False),其中false表示不允许部分更新。

INSERT插入操作

//Insert Operation Using Database methods
APEX_Invoice__c objNewInvoice = new APEX_Invoice__c();
List InvoiceListToInsert = new List();
objNewInvoice.APEX_Status__c = 'Pending';
objNewInvoice.APEX_Customer__c = objCust.id;
objNewInvoice.APEX_Amount_Paid__c = 1000;
InvoiceListToInsert.add(objNewInvoice);
Database.SaveResult[] srList = Database.insert(InvoiceListToInsert, false);//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());
	}
}
}

UPDATE更新操作

//Code to update the records using the Database methods
List invoiceList = [SELECT id, Name, APEX_Status__c, createdDate FROM APEX_Invoice__c WHERE createdDate = today];//fetch the invoice created today
List updatedInvoiceList = new List();
for (APEX_Invoice__c objInvoice: invoiceList) {
	if (objInvoice.APEX_Status__c == 'Pending') {
		objInvoice.APEX_Status__c = 'Paid';
		updatedInvoiceList.add(objInvoice);//Adding records to the list
	}
	
}
Database.SaveResult[] srList = Database.update(updatedInvoiceList, false);//Database method to update 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 updated Invoice. Invoice ID is : ' + sr.getId());
}
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());
	}
}
}

你可能感兴趣的:(SFDC)