Wednesday 15 May 2013


Upsert

Upsert combines the behavior of the Insert and Update operations on up to 200 records
of the same object type. First, it attempts to locate a matching record using its unique
identifier or external identifier. If one is found, the statement acts as an Update. If not, it
behaves as an Insert.



The syntax of the Upsert statement is identical to Update and Insert, but adds a second,
optional argument for specifying an external identifier. If an external identifier is not
provided, the record’s unique identifier is used.The code in Listing 5-35 upserts a record
in the Resource object using the field Resource_ID__c as an external identifier. If a
Resource record with a Resource_ID__c value of 1001 exists, it is updated. If not, it is
created.

Listing 5-35 Upserting a Record
Resource__c r = new Resource__c(Resource_ID__c = 1001, Name = 'Terry Bull');
upsert r Resource_ID__c;

No comments:

Post a Comment