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
Sunil mSunil m 

How to know whether upsert statement inserted or updated record after executing upsert statement

Hi all,

I used upsert sObject; statement , after executing this statement I need to know upsert is inserted or updated record

Best Answer chosen by Admin (Salesforce Developers) 
IceEngineIceEngine

yes, there is a class call upsertResult, and use isCreated() method to determine whether is insert or update

like this

Database.UpsertResult[] urList = Database.upsert(list, opt);
for (Database.UpsertResult ur : urList) {
    if (ur.isCreated()) {
        //dosomething
    }
}

 more infomation:

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

All Answers

IceEngineIceEngine

hello,

in trigger events, there are insert trigger and update trigger,

you can create two seprerate triggers to do different actions,

or you can create one  insert,update trigger , in this case, you can use if (Trigger.isInsert) and if (Trigger.isUpdate) to perform different actions.

 

Sunil mSunil m

Actually I don't have requirement to implement trigger here, My requirement is If upsert stmt performs insert operation I need to add this new record to List if updated then I need to replace existing record in List . So I need to know that in controller.

IceEngineIceEngine

yes, there is a class call upsertResult, and use isCreated() method to determine whether is insert or update

like this

Database.UpsertResult[] urList = Database.upsert(list, opt);
for (Database.UpsertResult ur : urList) {
    if (ur.isCreated()) {
        //dosomething
    }
}

 more infomation:

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

This was selected as the best answer
m.elmoussaouim.elmoussaoui

Hello,

you could use Database.upsert for your DML and check the isCreated of the UpsertResult.

 

Good luck.

Sunil mSunil m
Thank you, Problem solved..
BALA_RAMBALA_RAM

hi sunil

 

when ever we use Upsert statement, in salesforce Inserting and Updating records automatically.