-
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
Salesforce Editions Comparison: Choosing the Right Edition for Your Business
A Detailed Walkthrough of Salesforce Editions Comparison Salesforce offers multiple CRM editions like Essentials, Professional, Enterprise, and Unlimited – each of these Salesforce editions are…
Navigating the Salesforce Data Migration Maze: Best Practices and Configuration Tips
The word “Migration” is always associated with shifting something to other places where accuracy, safety, and speed are of utmost importance. However, when it comes…
Supercharge Support Agents with These Lesser-Known Salesforce Service Cloud Features
We are living in a time when customer experience matters the most for a business. A disappointed customer will take no longer a minute to…
Popular Salesforce Videos
TOP 20 Salesforce Integration Interview Questions and Answers 2019 | Salesforce Integration
In this video, Wisdom Jobs and Tutorials explains the 20 most popular questions asked during Salesforce Integration interviews. These questions cover topics like what web…
Salesforce Online Training Videos For Beginners | Campaign Management
Learn all about the Salesforce campaign management in this video. Watch and learn. If you have any doubts or questions, please let us know in…
Salesforce CPQ Product Bundles
Salesforce CPQ product bundles. See how product bundles work with features and options. Also including an option constraint to prevent incorrect configuration. Watch this video…
Popular Salesforce Infographics
Infographic: The Do’s and Don’ts of Maximizing Your ROI with CRM
Get the tips you need to succeed! This infographic alerts you about th Do's & Don'ts of maximizing your ROI with CRM. Scroll through this…
Top 12 Salesforce Trends in 2024
Embark on a journey through the future of Salesforce in 2024 and beyond. Uncover the latest trends shaping CRM excellence, providing crucial insights to navigate…
How Sales Reps are Staying at the Top of Their Game | Salesforce Guide
Sales reps around the world had to rapidly adopt new methods to work around lockdowns and travel restrictions. In some cases, this meant learning new…