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
Sheikh Abrar Ul HaqSheikh Abrar Ul Haq 

UPSERT without External ID

Hi All,

 

I need your attention for unknown functionality for me of Salesforce.com/Force.com platform. I am doing UPSERT without External ID on an object. I have a custom object "Sales Order Summary" which is a child object of Account via Master-Detail relationship.

 

I am doing UPSERT with standard Name field of custom object "Sales Order Summary". Before this, Only thing which I knew was doing UPSERT with an External ID on an object. It is working Perfect !!! for me. I am just confirming from you guys (Guru's of Force.com platform) that whether it is ok to do it or I should query the records and perform INSERT / UPDATE based on condition. Please help me out in it.

 

Note: Following things I already know about the UPSERT.

1) UPSERT with External ID (I cannot create External ID on this object due of some reasons).

2) Query records and perform INSERT / UPDATE based on condition.

________________________________________________________________________________________

//Sample Order Summary Code for UPSERT in Apex

 

//List of Sales Order Summary for UPSERT
List<Sales_Order_Summary__c> listOfSalesOrderSummaryToUpsert = new List<Sales_Order_Summary__c>();

//Created a new Account "Test Sales Order Summary" with JDE Address Number = 1001

//JDE Address Number is an External ID on an Account. I am getting particular record.
Account a = new Account (JDE_AddressNumber__c = 1001);

//Create Sales Order Summary records
for(integer i=0; i<=13; i++){
    Sales_Order_Summary__c salesOrder = new Sales_Order_Summary__c();
    salesOrder.Account__r = a;
    salesOrder.Name = 'Sales Order Summary'+'---'+i;
    salesOrder.Amount__c = 300 + i;
    salesOrder.Date__c = System.today() + i;
    listOfSalesOrderSummaryToUpsert.add(salesOrder);
}

//The External_ID_Field is of type Schema.SObjectField, that is, a field token.
Schema.SObjectField schemaField = Sales_Order_Summary__c.Fields.Name;

 

//Creates new sObject records or updates existing sObject records within a single statement, using an optional custom field to determine the presence of existing objects.
Database.UpsertResult[] upsertResult = Database.upsert(listOfSalesOrderSummaryToUpsert, schemaField, false);
System.Debug('>>> Sales Order Summary UPSERT Result <<<'+upsertResult);

 
/*
Note: You cannot use custom fields as a parameter of Schema field.
Schema.SObjectField schemaField = Sales_Order_Summary__c.Fields.Date_c;
Note: If you use then you will get an error
System.SObjectException: Invalid field for upsert, must be an External Id custom or standard indexed field: Date__c
*/

digamber.prasaddigamber.prasad

Hi,

 

I assumet standard field "Name" on said object is unique, if yes, then you should be good to go.

Sheikh Abrar Ul HaqSheikh Abrar Ul Haq

 

Thanks for the reply. Yes you are correct. But I am unable to get any references or documentation available by Salesforce.com. The documentation which is only available to use UPSERT DML operation only via creating a new custom field marked as External ID.

 

Here I am doing UPSERT with the help of standard Name field of custom object. Can you please provide me the references or documentation to do UPSERT via standard Name field?

georggeorg

If you explicitly specify the unique field then system looks for external id attribute on that field. If you remove that explicit reference with Name the code will work perfectly.

 

Thanks,

George

Visit my  blog here

Sheikh Abrar Ul HaqSheikh Abrar Ul Haq

Hi georg,

 

Can you please provide me the references or documentation to do UPSERT via standard Name field?

georggeorg

I just used the this documentation

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dml_upsert.htm 

 

I did a little hands on like upserted an account with name field explicitly referenced the external id field. Which was failed

But without external id explicit reference i was able to upsert the account. I did not check with unique name field, i assume that this will also work in such scenario.

 

Thanks,

George

Katia HageKatia Hage
Any field with the idLookup attribute set to true (which includes Name fields on custom objects, but not all Name fields on standard objects) can be used for record matching in the upsert() call. This is stated as such in the API doc but not in the Apex doc, but applies to Apex as well. See https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_calls_upsert.htm

To check for the idLookup attribute, use http://workbench.