-
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

Why Should You Choose Salesforce Revenue Cloud & CPQ!
Streamline Your Lead to Cash Business Processes in a Single Unified Platform Salesforce Revenue Cloud Suite is part of the Salesforce Customer 360 platform. It is…

Automate Call Logs Using RingCentral for Salesforce
Hi All, RingCentral one way solution for calling from Salesforce. This app is available on AppExchange, you just need to install it and make some administrator…

From Nice-to-have to Necessity : The Role of Personalization in Banking | Salesforce
Over 79% of U.S. consumers perceive their relationship with banks as purely transactional. And banks across the country are striving to change that equation. Technology…
Popular Salesforce Videos
Work With Custom Lightning Components | Salesforce Tutorial
Creating a custom Lightning component requires programming skills. Or, you can install an existing Lightning component from the AppExchange. You can create a custom Lightning…
Salesforce Flow to Create Feedback form on Case | How to use Slider, Radio button on Screen Flow
Use Case: In this video, we are creating a screen flow to get feedback from the users when the case is closed. Learnings: 1. How…
TrailheaDX 2021: Salesforce's Parker Harris Explains How Low-code is Reshaping Software Development
Salesforce CTO and co-founder Parker Harris talk about TrailheaDX 2021 and how low-code is dramatically changing the process of business software development. Watch and learn.
Popular Salesforce Infographics

10 Steps for Hassle-Free Insurance CRM Software Installation | Salesforce
CRM system? It’s a platform that connects your different departments, from marketing to sales to customer service, and organizes their notes, activities, and metrics into…

Your Guide to Salesforce Certifications
In the age of customer experience, businesses now rely on their CRM systems more than ever. As the world leader in CRM software, Salesforce knows…

What Can Salesforce’ Non Profit Success Pack Provide You With?
The Nonprofit Success Pack NPSP (formerly known as "Nonprofit Starter Pack) is a set of managed packages developed and maintained by Salesforce.org and made available…