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
mariagusmariagus 

Not Serializable: LIST<Database.UpsertResult> error

Hi all,

 

I was trying to know where my upsert was failing and which record was making the mistake.

 

After doing some research, I found that Database.Upsert method gives me the information that I need but when I tryied to run it on my batch apex process, where my upsert is, I got this error: "  Not Serializable: LIST<Database.UpsertResult> 

 

  "

 

Does anybody know how can I fix it?

 

This is part of my code:

 

List<Database.UpsertResult> upsertResults;

Schema.SObjectField externalIdField = SalesAgreement__c.Fields.Id;

upsertResults = Database.upsert(salesAgreementDtoList,externalIdField,false)

externalIdField = SalesAgreementLineItem__c.Fields.Id;

upsertResults = Database.upsert(salesAgreementLineItemDtoList,externalIdField,false);

 

Many thanks in advance

Best Answer chosen by Admin (Salesforce Developers) 
tharristharris

Some types (including Database.UpsertResult) cannot be serialized with a stateful batch apex job, and an exception is thrown at runtime when it encounters one of these. (It is trying to save the state of your job after each invocation of start/execute/finish so that the values can be restored later.)

 

Assuming upsertResults is a member variable of the class, you should be able to fix this problem by declaring it inside the method or adding the transient keyword to the declaration. For example:

 

transient List<Database.UpsertResult> upsertResults;

 

This indicates that the value of upsertResults should not be serialized along with the other member variables of the class.

All Answers

tharristharris

Is upsertResults an instance variable of your batchable class? And does your class implement Database.Stateful?

 

-Thomas

mariagusmariagus

Thanks for your quick replay

 

Yes, my batch apex implements Database.Batchable<sObject>, Database.Stateful

 

Regarding your first question ... I do my batch on an Opportunity object. I need to create a SalesAgreement__c per each Opportunity.

If the Opportunity has a SalesAgreement__c already, it has to be updated.

 

That's why at the end of the process I want to upsert my SalesAgreement__c and its details.

 

tharristharris

Some types (including Database.UpsertResult) cannot be serialized with a stateful batch apex job, and an exception is thrown at runtime when it encounters one of these. (It is trying to save the state of your job after each invocation of start/execute/finish so that the values can be restored later.)

 

Assuming upsertResults is a member variable of the class, you should be able to fix this problem by declaring it inside the method or adding the transient keyword to the declaration. For example:

 

transient List<Database.UpsertResult> upsertResults;

 

This indicates that the value of upsertResults should not be serialized along with the other member variables of the class.

This was selected as the best answer
mariagusmariagus

Thanks!!!

 

Using transient it worked properly :)