function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
thomastthomast 

Field notation for upsert DML statement

The upsert example in the Manipulating Records with DML (https://developer.salesforce.com/trailhead/force_com_programmatic_beginner/apex_database/apex_database_dml) lesson is:
upsert sObjectList Account.Fields.MyExternalId;
Is there a "__c" missing at the end of that? Can someone explain the difference between using the [Object].Fields.[FieldName] notation and the fieldname-only notation that is used in the Apex Code Developers' Guide? When would I use one over the other? Here's the example DML statement from the Developers' Guide:
upsert acctList myExtIDField__c;


 
Balaji Chowdary GarapatiBalaji Chowdary Garapati
@thomast:

  In salesforce, the standard field's API name doesnt have "__c"  appended whereas all custom fields that you created will have an appended "__c" for the API Name.

In above example, Assuming Email field(standard) as external id for contact, that example has been demonstrated.  You can specify a custom field which is marked as external id at the object level in an upsert call, which verifies if the mentioned value in that column exists or not while upserting towards the existing records, based on result, an update or insert of record will happen.

Hope it helps..

Thanks,
balaji
thomastthomast

I understand all that. In the first example, it refers to a field "MyExternalId" with no __c suffix. There is no standard field on Account called that. 

Additionally, I understand what an upsert does. My question was about the different syntax used in the two examples to specify the field to check when upserting. The first uses a dot notation with the Fields element, and the other calls a custom field directly by name, with the SObject type implied by the type of the records in the list. When & why would you use the longer dot-notation syntax?

Balaji Chowdary GarapatiBalaji Chowdary Garapati
The optional field is a field token. For example, to specify the MyExternalID field, the statement is:
 upsert sObjectList Account.Fields.MyExternalId;

If you were referring to the above usage, i see what you are trying to ask!, I think Publisher of that documentation forgot to mention that MyExternalID is your desired External Id field API name  which will have " __c" appended in case of custom and doesnt in case of Standard.
I clearly did not understand what you mean by "Dot Notation" and "Direct Name".[safe mark /condition statement]

I would like to provide more info on what i knew about upsert: 
Upsert operation can be performed in two different ways

1) using the keyword "Upsert" :
                    This is similar to what you are read in the above document for eg., UPSERT sobjectList or UPSERT sobjectlist sobjectfield which specifies the identifier to be used for upsert which aborts the whole transaction even if one record fails.

2) using the Database instance:
                    This is is way which you use database classes instance to do perfrom DML operations where you can specify the way you would like to process the DML either you want to commit the successful records  or abort the the whole transaction if one fails.

In case of specfying an external id to use instead of Salesforce unique "id" field, both the methods require a parameter of type   "Schema.SObjectField ", for which in the demo, they used "Contact.fields.Email" which is nothing but  the short hand for statement "Schema.sObjectType.ObjectAPINAME.fields.FIELDAPINAME.getSObjectField()" which return the SobjectField Class Instance of that field. 

As i said Database instance can have more parameters which can alter the behvaiour of how the DML transaction is made, for reference go through the below link:

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_database.htm#apex_System_Database_upsert


Hope this helps.

Thanks,
Balaji