-
How to update error message back to processed record in Salesforce batch apex?
Can anyone guilde me how to update the error message back to the processed record in salesforce if any error occurs in batch apex.
I am processing records from SIS_Staging__c object to create or update contact based on condition, if any error occurs for any record then I want to update the error field on SIS_Staging__c object.
Below is my code of batch apex where I used database.savepoint method to rollback the changes if any error occurs.
can anyone guide me on this, I don't have any idea how to process error message back to processed record please.
global class newbatchapex implements Database.Batchable<sObject>, Database.stateful{ global Database.QueryLocator start(Database.BatchableContext BC) { return DataBase.getQueryLocator([SELECT Id,Name,Status__c,SIS_Student_ID__c ,First_Name__c,Last_Name__c,Email__c,Mailing_Country__c,SIS_Application_Id__c,AdmTyp__c,Resident_Tuition__c,ActnRsn__c,Admit_Term__c,Home_Country__c,Plan__c,Prog1__c,Prog__c, ProgActn__c FROM SIS_Staging__c ]); } global void execute(Database.BatchableContext BC , List <SIS_Staging__c> staginglist) { Set<String> uniquekeySet = new Set<String>(); for(SIS_Staging__c stagingObj : staginglist){ uniquekeySet.add(stagingObj.SIS_Student_ID__c); } List<Contact> contactToUpdate = new List<Contact>(); List<Contact> contactToInsert = new List<Contact>(); Map<String,Contact> studentIdTOContact = new Map<String,Contact>(); Savepoint sp = Database.setSavepoint(); List<Contact> conList2 = [SELECT ID,SIS_Student_ID__c,FirstName,LastName FROM CONTACT WHERE SIS_Student_ID__c IN:uniquekeySet]; for(Contact conObj : conList2){ studentIdTOContact.put(conObj.SIS_Student_ID__c,conObj); } for (SIS_Staging__c acct : staginglist){ if(studentIdTOContact.containsKey(acct.SIS_Student_ID__c)){ Contact con = studentIdTOContact.get(acct.SIS_Student_ID__c); con.FirstName = acct.First_Name__c; con.LastName = acct.Last_Name__c; con.hed__AlternateEmail__c = acct.Email__c; con.SIS_Student_ID__c = acct.SIS_Student_ID__c; con.hed__Country_of_Origin__c = acct.Home_Country__c; con.Current_Country_of_Study__c = acct.Mailing_Country__c; con.SIS_Application_Id__c = acct.SIS_Application_Id__c; contactToUpdate.add(con); }else{ Contact con = new Contact(); con.FirstName = acct.First_Name__c; con.LastName = acct.Last_Name__c; con.hed__AlternateEmail__c = acct.Email__c; con.SIS_Student_ID__c = acct.SIS_Student_ID__c; con.hed__Country_of_Origin__c = acct.Home_Country__c; con.Current_Country_of_Study__c = acct.Mailing_Country__c; con.SIS_Application_Id__c = acct.SIS_Application_Id__c; con.LeadSource = acct.Status__c; contactToUpdate.add(con); } } try{ //insert contactToInsert; upsert contactToUpdate; } catch(exception e){ string s = 'Error: ' + e.getMessage(); database.rollback(sp); }
Log In to reply.
Popular Salesforce Blogs
5 Features That Salesforce Professionals Want You to Know About
Salesforce has been instrumental in maintaining superior customer relationships for businesses. As Salesforce keeps adding new tools to its ecosystem, customer management has become easier…

What is the Apex Testing Framework in Salesforce in 2023?
The Apex testing framework enables you to write and execute tests for your Apex classes and triggers on the Lightning Platform. Apex unit tests ensure…

Deployment on Production Environment in Salesforce
When we move any code or component into a production environment we need to keep the below terms for our understanding: These terms are-: Change…
Popular Salesforce Videos
Avoid Common Security Mistakes in Salesforce
Are you using Salesforce in the most secure way? Security is not a “set it and forget it” thing. We conduct Salesforce Security Risk Assessments…
Record Triggered Flows with Real Time Scenarios | Salesforce Tutorial
In this video, you will understand 1. What is Record triggered flow? 2. How and when to use Record triggered flows? 3. Real-time scenarios 4.…
Copy to Clipboard Functionality in Salesforce - Lightning Component Projects Series Part 2
In this video, I'm creating a lightning component Project for functionality i.e Copy to Clipboard. Watch this video and learn. If you have any doubts…
Popular Salesforce Infographics

Features of Salesforce Spring’ 23 Release That Must Not Miss
The new updates in the Salesforce ecosystem will bolster the marketing power of the businesses. Companies are now more effectively building their marketing strategies which…

DreamOle Event - Salesforce
A group of volunteers within the Spanish Salesforce Trailblazer Community. Their Mission is to put together a Community Conference that would help the Spanish Community…

Salesforce: Giving Buzz In Numbers
Salesforce has been coined as the world’s top CRM and there is always a constant buzz around it’s latest developments. So let’s check out what…