0

I have the following code below

     Data_Source__c datasource=new Data_Source__c(External_Id__c='SQLServer');
     Table__c table=new Table__c(External_Id__c='Employee');
     Column__c column =new Column__c(Id='a072x000000a1ZXYYY',Data_Source__r=datasource, Table__r=table);
     List<Column__c> columns=new List<Column__c>();
     columns.add((Column__c)column);
     List<Database.SaveResult> results = Database.update(columns);

I have an Column Object which has a lookup field to Table and Data Source objects.

I am trying to update the Column object's lookup field using external ids of Table and Data Source objects.

Let's presume that Data Source external id is invalid but Table object external id is valid. When the updating of the column records are done, I want the Table data to be populated as a lookup for the column but throw an error for Data Source.

But when I am trying to update, both table and Data Source are not populated and throwing error, since data source external id is invalid.

How can I overcome this, please help me here?

1 Answer 1

1

There's no way that I know of to allow partial update for a single record, if one of the fields is invalid. However what you could do is use your SaveResult list and create a retry mechanism.

First, change your update statement to allow partial commit if some records fail:

List<Database.SaveResult> results = Database.insert(columns, false);

Next, iterate over your results, look for errors and collect in which fields they've occurred:

List<String> errorFields = new List<String>();

for(Database.SaveResult result : results){
   if (!result.isSuccess){
      for(Database.Error error : result.getErrors()){
        errorFields.addAll(error.getFields());
      }
   }
 }

From here, you can pretty easily analyze your collected invalid fields, clear their External Id value, and execute a second attempt of Database.update().

You must log in to answer this question.

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