Hot answers tagged

19

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(), field);), you'll know the order of the results. If you do not know the order, because it's in a map, you can still get the Id from the result (mapRecords.get(result....


12

You won't be able to delete the leads because your trigger is firing before insert. The leads aren't in the database yet. What you should probably do here is add an error to the records that have duplicates. for(Lead lead : trigger.new){ if(BObjMap.containskey(lead.email)){ lead.email.addError('Lead with that email address already exists'); }...


10

You need to use the external if 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 you were trying but I also think that you need to create the parent separately, however I think you either need to build a map to successfully relate the parent ...


8

The last modified date of the every record will definitely change ,since an update of record will eventually happen even if field values are not changed . If you need to explicitly code such scenarios ,i would definitely recommend to do an explicit update and update only those records whose field values have changed . if(previous_value!=new-Value){ //...


8

The answer is no, it does not. Upsert is not operating within a degree of Snapshot Isolation sufficient to allow it to behave in a way that is completely cummutative with insert and update. In English, it means it is not a bullet-proof substitute for update and insert. Your question includes reference to the importance of order of operation within an insert,...


8

By default if field is not specified then Salesforce uses ID field for upsert. If you want to explicitly specify other field, it should be defined as external Id. You may refer this post to understand more about upsert operation and external Id. Update : Explicitly using Name in upsert can fix it.


8

So, if I understood correctly, you are passing the same key twice in the list, correct? If that's the case, this is documented Upserting Records If the key is matched multiple times, then an error is generated and the object record is neither inserted or updated.


7

What follows is a trivial trigger that will delete a lead that is considered "duplicate" based on email address. Ideally, you would want more criteria, such as choosing at minimum last name, email, phone, and company, but this is just to get the creative juices flowing. Note that the magic happens by calling clone on the record; we can't DML on Trigger.new ...


6

I ran into a similar challenge on a previous project where existing PBE rows could not be "updated" if you are sending CurrencyIsoCode, Product2Id, or Pricebook2Id as part of your upsert call. If you look at the API guide, the updateable flag on those three fields is false. The workaround I implemented was as follows: Run initial upsert call with all ...


6

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 assign the correct account, you need the account's External ID (or it's actual other-org ID), and specified in the payload. // Define upsert field and value ...


5

Here's some the gaps filled in. I am assuming that Competitor__c is your MultiSelect field, that Win_Reason__c is a Picklist and that Unsolicited_Bid__c is a Checkbox. If not, adjust accordingly. ------------------------------------------------------ -- Field Type * Data Type -- ----------------------------------------------------...


5

Upsert has two modes of operation. The first is to create a record if no ID is specified, or update the record if an ID is specified, and the second is to take an "external ID" and create a record if no matching external ID is found, or update the record if exactly one external ID is found. In this case, you're using the former mode of operation, and that's ...


5

That is correct, you cannot upsert on a List of SObject when you specify external ID. Error: "Upsert with a field specification requires a concrete SObject type" Please vote for the idea here : https://success.salesforce.com/ideaView?id=08730000000aNhVAAU Conceptually what you require is : DYNAMIC TYPECASTING. But I am not sure if dynamic typecasting can ...


5

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 object in the query. If you did find yourself in a situation in which you need every field, there are ways using dynamic apex or Field Sets to obtain all of the ...


5

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 prod = new Product2(); prod.Id = '<salesforce id here>'; upsert prod; Example with record External Id: Product2 prod = new Product2(); prod....


4

Salesforce sets these IDs back to list that is being upserts. For example Upsert xxx; now after this line you can put a loop to display ids like below - for(TypeXXX afterUpsert : xxx){ system.debug('*** Id = ' + afterUpsert.id); }


4

According to the documentation here http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_custom_settings.htm, you can upsert custom settings. --KC


4

I believe you would need to use the Database.upsert method to obtain the results as shown here, in this example: public class DmlSamples { /* This method accepts a collection of lead records and creates a task for the owner(s) of any leads that were created as new, that is, not updated as a result of the upsert operation */ ...


4

When changing record types from business to person you cannot specify any other field values. I believe what is happening is that you are trying to populate the record type of a BUSINESS account with the value of the Person account record type along with the other fields and this is not allowed. Or alternately you are trying to change a person account of ...


4

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. This is because External Id is indexed, so records can be identified very quickly.


4

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 that all the non-nullable variables are passed in the function call. This might not be a general answer to the posted question (Eliot Harper's answer is better in ...


3

You cant create fields on attachment as you said so only way i would do an upsert is to query for the attachment with existing parent Id and If found update the attachment body else do an insert. Here is the pseudo code for same . Map<Id,Attachment> MapParentIdByAttchment=new Map<Id,Attachment>(); for(Attachment attch:[Select Id,ParentId from ...


3

You are iterating through and then returning "singlegroup" before doing any DML - so the DML won't be seen, and since you aren't returning a PageReference (or a null) the VF is probably not doing much either.


3

An upsert allows the DML operation to choose between an "insert" and an "update" operation. If a record already exists, it updates it. If the record is new, or doesn't already exist, it inserts it. In your situation, if the record already exists and there are no changes to it, then you don't need to update it. You only need to insert new records. I don't ...


3

Try Database.UpsertResult cr = Database.upsert(cts[0], f, false); since you're only inserting one record instead of an array. Or recode to something like the following: Contact cts; string searchQuery = 'SELECT id, firstName FROM contact WHERE Membership_number__c = 123456789 limit 1'; cts = Database.query(searchQuery); System.debug('Cts contains data: ' + ...


3

For reasons I don't understand upsert does not support this pattern - as the error message explains insert, update and delete do. Your present design will break when there are many tickets because you are doing an upsert per ticket and will run into the DML governor limit of a maximum of 150 DML operations per request/transaction. A way around that is to ...


3

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) { // new contact c.Email = 'test@test.com'; } else { // existing contact } upsert c Contact.ExternalId__c;


3

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 also unique Scenario 1: By default, Batch size of dataloader is "200" So, if we perform upsert based on External Site Id or External Site Key, then in ...


3

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 });


3

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.AccountId, external = Account.Counsyl_Id__c; SObjectType childType = Contact.sObjectType, parentType = Account.sObjectType; String relationshipName = lookup....


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