Skip to main content
26 votes
Accepted

Database.upsert - how to find which records have inserted/updated based on ExternalId

The order of the results are the same as the order of the original list. As long as you know the order of the original list (e.g. you didn't do something like Database.upsert(mapRecords.values(), ...
sfdcfox's user avatar
  • 497k
10 votes
Accepted

updating a records using external Id in Apex

You need to use the external id in the update call upsert flist Externald__c; For part two of your question, seeing that you want to relate the parent and child foo records you can t do it the way ...
Gareth Jordan's user avatar
7 votes

How to prevent an upsert from overwriting fields with existing values with null?

The underlying issue here is that, no matter what your approach is, you have to somehow deal with the fact that Description__c is being set on the SObject instance you're using to upsert. Even if it'...
Derek F's user avatar
  • 62.7k
6 votes
Accepted

Salesforce REST upsert for externalId cause DUPLICATE_VALUE error

After a month of discussions with Indian support of all tiers, we've got next: Salesforce can't proceed too fast UPSERT queries. If you send two requests with delay about a second or less you'll get ...
KorbenDallas's user avatar
6 votes
Accepted

Trying to turn HTTP 'POST' to 'PUT'; Desire Upsert, Not Just Insert

I'm not sure where you're getting the idea that PUT is the correct verb ("method") here; it should be a PATCH; you'll need to use the _HttpMethod query parameter to set the correct method. Also, to ...
sfdcfox's user avatar
  • 497k
6 votes

Update an object field

Someone will only have to come include that field in the query if they actually need to do something with it in your code. You do not need to (and absolutely should not) include every field on your ...
gNerb's user avatar
  • 9,508
5 votes
Accepted

Upserting Data using Data Loader with External ID

I will put 2 scenarios over here. I have created following csv file to upload data into my org, where External Site Id is External Id (unique is not selected) External Site key is External Id and ...
Santanu Boral's user avatar
5 votes
Accepted

Update an object field

If you already have an Id value, you do not have to do a query to update the value. Instead you construct an instance using the Id you have and set the field value you need and then pass that object ...
Keith C's user avatar
  • 136k
5 votes
Accepted

How to use upsert in REST callout JSON

To make upsert update records you have to specify either record Id or External Id. When neither is specified, Salesforce treats the record as new and inserts it. Example with record Id: Product2 ...
Eduard's user avatar
  • 5,181
5 votes
Accepted

How to use sfdx force:data:bulk:upsert on an object without an external ID?

Have you tried using Id as external ID field? The documentation uses it in an example: $ sfdx force:data:bulk:upsert -s MyObject__c -f ./path/to/file.csv -i Id -w 2 In addition to that, if you ...
Folkert's user avatar
  • 2,722
5 votes
Accepted

Update (never insert) a record by External Id using REST API

A reasonable workaround I found is to use a Composite request: in step 1, retrieve record Id, in step 2, update record by Id using normal PATCH method. POST /services/data/v53.0/composite { "...
mmm321321's user avatar
  • 659
4 votes
Accepted

Does Upsert operation in apex take more time against huge data?

It will have approximately the same performance as it would to query the records by External ID and make the decision manually. In other words, there's no performance benefit from not using upsert. ...
sfdcfox's user avatar
  • 497k
4 votes
Accepted

Add dynamic lookup on Contact to sObject with external Id

You need to make sure that you use the correct relationship name. Here is a simple example that proves this functionality works just fine with a generic SObject record. SObjectField lookup = Contact....
Adrian Larson's user avatar
  • 151k
4 votes

UpsertData and DeleteData not working

I recently faced a very similar issue. It turned out that the UpsertData function was trying to create a row without providing a value of a required variable. To avoid this, one needs to make sure ...
Radek Janhuba's user avatar
4 votes

How to use sfdx force:data:bulk:upsert on an object without an external ID?

I know of at least three options for you. sfdx force:data:tree:import should work fine for your situation if you're willing to switch to JSON format for your data storage. The easiest way to do this ...
David Reed's user avatar
  • 93.2k
3 votes
Accepted

Can the exact type of an Apex list be checked to choose between upsert and insert/update?

You can't use upsert if the list is generic, even if it only contains one type of records. Basically, you can use getSObjectType(), and if it's null, you can't upsert: if(sobs.getSObjectType() == ...
sfdcfox's user avatar
  • 497k
3 votes
Accepted

UpsertResult isCreated is not working

isCreated is a method that returns a boolean value, not a boolean property. As such this is what you want to do ur.isCreated() UpsertResult Class Additionally, this will fail as well ur.Counter__c =...
Eric's user avatar
  • 54.2k
3 votes
Accepted

Avoid Duplicate Id in a List

You should consider using the Map class instead of a List. In fact, if you instantiate some maps like this: Map<Id, Bundle__c> bndlList = new Map<Id, Bundle__c>(); The filter on ...
Martin Lezer's user avatar
  • 7,295
3 votes
Accepted

Error when upserting an ID in a lookup field

I think your setParams is not right, you should pass a Object as argument to it which contains all necessary data as key value pair, it should be: action.setParams({ "rent": newRent, "a": bikeID });
Praveen's user avatar
  • 10.1k
3 votes
Accepted

Upsert contact conditionally

In step 2, you have to query the contact. Something like this: Contact c = new Contact(LastName='Test', ExternalId__c='12345'); if([SELECT COUNT() FROM Contact WHERE ExternalId__c = :externalId] == 0)...
sfdcfox's user avatar
  • 497k
3 votes

DML not allowed on User

Use the Database.upsert static method instead of the upsert DML keyword: //failure (DML not allowed on User) upsert user ExternalId__c; //success Database.upsert(new User(), User.ExternalId__c, true)...
Matt and Neil's user avatar
3 votes

Upsert failed - DUPLICATES_DETECTED

Upsert needs a unique identifier in order to upsert correctly. If you don't specify a field, it defaults to Id. Since you apparently don't have an Id to use, you can either create your own, or you ...
sfdcfox's user avatar
  • 497k
3 votes
Accepted

How many records can UPSERT add or update in single request in salesforce..?

The limit will be 10000. This is same for all DML statement in single transaction. As Per Salesforce docs So you can see that upsert is also counted as DML statement and for DML statement we have ...
Tushar Sharma's user avatar
3 votes
Accepted

Upserting campaign members for campaigns

Apex sObjects do not have Ids until they are inserted. Once you perform the insert DML operation, the Id field is populated by Salesforce in your variables. Any object that is inserted as part of an ...
David Reed's user avatar
  • 93.2k
3 votes

How can I upsert a homogeneous List<SObject>, in a good way?

I know this is bit ugly, but serializing and deserializing converts sObject to the actual type they are. List<Account> accountList = (List<Account>) JSON.deserialize(JSON.serialize(...
Pranay Jaiswal's user avatar
3 votes
Accepted

How can I upsert a homogeneous List<SObject>, in a good way?

You need the actual object to be a concrete sobject list type. You can do it either by Type.forName or by String. SObject[] results = doUpsert(source, List<Account>.class); ... public SObject[...
sfdcfox's user avatar
  • 497k
3 votes
Accepted

Upserting on Dynamic List in Apex

You're performing a cast on a value that you then throw away. List<SObject> toUpsert = (List<SObject>)Type.ForName('List<' + objectName + '>').newInstance(); toUpsert = dynList; ...
David Reed's user avatar
  • 93.2k
3 votes
Accepted

How to use simple salesforce (python library for salesforce) to add ShippingAddress field?

Disclaimer: I don't have any experience working with simple-salesforce and neither I am associated with it anyway. But based on how the Address Compound Field works and just by looking at simple-...
Jayant Das's user avatar
  • 30.6k
3 votes
Accepted

Upsert causes custom and standard Duplicate Rules to fire

There's no such thing as a "standard duplicate rule" in the sense that Salesforce would block the creation of a Lead based on a Contact email address. This was configured by an administrator. Your ...
sfdcfox's user avatar
  • 497k
3 votes
Accepted

The Query Cost of UPSERT

Without seeing your org, it is impossible to say for sure, but almost certainly what is happening here is that you have triggers consuming this governor. Most likely would be triggers on ...
Adrian Larson's user avatar
  • 151k

Only top scored, non community-wiki answers of a minimum length are eligible