0

I am having issues with Upserting data. Here is my code:

global static void MyMethod(string param1, string objName, string param2) {

    CustomObject__c record = new CustomObject__c();
    record.Param1__c = param1;          
    record.Name = objName; 
    record.Param2__c = param2;
    record.User__c = UserInfo.getUserId();
    record.UpsertField__c = param1 + '_' + UserInfo.getUserId();
    //upsert record UpsertField__c;

    List<CustomObject__c> myRecords = new List<CustomObject__c>();
    myRecords.add(record);

    Schema.SObjectField f = CustomObject__c.UpsertField__c;
    Database.UpsertResult[] urList = Database.upsert(myRecords, f);
    for (Database.UpsertResult ur : urList) {
        if (ur.isCreated) {
            //set counter to 1 on custom object, something like:
            record.Counter__c = 1;
        } else {
            //increment counter by 1
            record.Counter__c++;
        }
    }
}

Error:

Error: Compile Error: Variable does not exist: isCreated at line 25 column 20

My understaing is that 'isCreated' is part of UpsertResult class. Please help me what I'm doing wrong here

1 Answer 1

3

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 = 1; as there are no custom fields possible on UpsertResult

4
  • I meant it was record.Counter__c. So is there a way to update that field on the custom object that is upserted (if that's a word)
    – user569925
    Commented May 18, 2017 at 14:02
  • @user569925 - Just set it prior to the dml.
    – Eric
    Commented May 18, 2017 at 14:12
  • how do I know if I need to set it to 1 or increment that field?
    – user569925
    Commented May 18, 2017 at 14:15
  • @user569925 - You will need a separate DML to do it in code. I misread what you were trying to do.
    – Eric
    Commented May 18, 2017 at 14:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .