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
SeAlVaSeAlVa 

Upsert failed. First exception on row X with id a0XD00... ; first error: INVALID_ID_FIELD, invalid record id: []

Hi everyone, 

I'm having the following Error

Upsert failed. First exception on row X with id a0XD00... ; first error: INVALID_ID_FIELD, invalid record id: []

The line that causes the error is 
 

upsert aNewMap.values();
aNewMap is a Map which values are records that might or might not have ID.

The point is,
1) why it says that "invalid record id: []" if it says the error before.
2) an upsert call without specifying a field, should take the ID field to know if its an insert or upsert call. if has no ID, why it does not make an Insert?

Kind regards
Swati GSwati G
Hi,

The id which is present in your sobject is of different type.

For example,
Suppose Sobject is of Account and you are using Contact's Id in Id field of account which will cause exception.

Please check if this is the case.


Vinit_KumarVinit_Kumar
Is this happening in a Trigger or a Class ??

Post the code and the issue again and we can take a look ...
SeAlVaSeAlVa
private static void addPersonAccountToQueue(Map<Id, Account> thePersonAccounts){

    Map<String, myQueue__c> aNewMap = new Map<String, myQueue__c>();
    List<myQueue__c> anOldList = [
          SELECT Id, ObjectID__c FROM myQueue__c
          WHERE ObjectID__c IN :thePersonAccounts.keySet() LIMIT 50000]; 
    Map<String, myQueue__c> anOldMap = new Map<String, myQueue__c>(); 
    for (myQueue__c anElement: anOldList)
    {
      anOldMap.put(anElement.ObjectID__c, anElement);
    }
    for (Account anObject: thePersonAccounts.values())
    {
      if (aJSON != null)
      {
        myQueue__c anElement = anOldMap.get(anObject.Id);
        
        if (anElement == null)
        {
          anElement = aNewMap.get(anObject.Id);
          if (anElement == null)
          {
            anElement = new myQueue__c();
          }
        }
        anElement.ObjectType__c = 'PersonAccount';
        anElement.ObjectID__c = anObject.Id;
        aNewMap.put(anObject.Id, anElement);
      }
    }
    if (aNewMap.size() > 0)
    {
      upsert aNewMap.values();
    }
  }
Thanks for your help ;)
Swati GSwati G
is ObjectID__c field is related to account object?
Swati GSwati G
Try to open the record on which you are getting error and check if it is of myQueue__c record.
Vinit_KumarVinit_Kumar
Change the declaration of your map from 

Map<String, myQueue__c> aNewMap = new Map<String, myQueue__c>();

to

Map<Id, myQueue__c> aNewMap = new Map<Id, myQueue__c>();

and similarily

from
Map<String, myQueue__c> anOldMap = new Map<String, myQueue__c>();

to

Map<Id, myQueue__c> anOldMap = new Map<Id, myQueue__c>();

and then test it
SeAlVaSeAlVa
@Swati G, myQueue prefix is a0X, so it is an ID of the queue. ObjectID__c of the record starts with 001 (18 characters ID)

@Vinit_Kumar, we are storing the 18 characters ID, is there any issue with it?. I agree with you, it is better to use ID fields whenever possible, but as it was another developer who implement it and it has been working fine for the last 2 years, so I did not wanted to change it).