📅  最后修改于: 2023-12-03 15:13:26.846000             🧑  作者: Mango
Apex DML refers to the Data Manipulation Language statements used in Apex programming language. These statements are used to interact with data in the Salesforce database. Using Apex DML, you can insert, update, delete, and upsert records.
The insert statement is used to add new records to the database. Here's an example:
Account a = new Account();
a.Name = 'Acme';
a.Industry = 'Technology';
insert a;
In this example, we create an Account object, set its fields, and insert it into the database using the insert
statement.
The update statement is used to modify existing records in the database. Here's an example:
Account a = [SELECT Id, Name, Industry FROM Account WHERE Id = '001xxxxxxxxxxxx'];
a.Name = 'Acme, Inc.';
update a;
In this example, we retrieve an Account object from the database, modify its Name
field, and update it in the database using the update
statement.
The delete statement is used to remove existing records from the database. Here's an example:
Account a = [SELECT Id, Name, Industry FROM Account WHERE Id = '001xxxxxxxxxxxx'];
delete a;
In this example, we retrieve an Account object from the database and delete it using the delete
statement.
The upsert statement is used to insert or update records based on a specified external ID field. Here's an example:
Account a = new Account();
a.Name = 'Acme';
a.Industry = 'Technology';
a.External_Id__c = '123456789';
upsert a External_Id__c;
In this example, we create an Account object, set its fields, and upsert it into the database using the upsert
statement based on the External_Id__c
field.
Apex DML statements are essential for interacting with data in the Salesforce database. By using insert, update, delete, and upsert statements, you can manipulate records to suit your business requirements.