-
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
API Versions in Salesforce - All You Need to Know
As a feature of a controlled Metadata API delivery and source control procedure, it is essential to see how API forms impact functioning examples, particularly…
Unpacking Salesforce Experience Cloud: A Comprehensive Analysis
Introduction to Salesforce Experience Cloud Salesforce Experience Cloud, formerly branded as Salesforce Community Cloud, is a powerful tool created by Salesforce that provides businesses the…
Application Event in Lightning Component in Salesforce
As we have discussed Component Events in my previous blog which is also one of the events helps to communicate between two components. Now you…
Popular Salesforce Videos
Salesforce Platform Quick Tip: How to Make Key Information Easier to Find
Want to flag things on a Salesforce record, so that your users notice? Like flagging a super important customer record, or alerting them that a…
Google Gantt Chart with Colorful Bars in Salesforce Lightning
In this video, you will learn to implement the Gantt Chart with colorful bars in your Salesforce org. We will use Google Gantt Chart scripts…
5 Pillars of a Successful Salesforce DevOps Process
Looking to improve your release and issue resolution times using Salesforce DevOps methodologies? DevOps is a software development and delivery process enabling seamless collaboration between…
Popular Salesforce Infographics
How To Upgrade Your Business With Salesforce?
Salesforce has been stretching the possibilities of business growth right from its inception in 1999. Its fierce business model attracted people who eventually moved to…
How Does Salesforce Use Slack To Collaborate and Connect?
Salesforce and Slack bring together all your teams with the apps and data they need in a single, collaborative workspace. Plus, Slack Connect functionality lets…
Top 5 Affordable Salesforce Integration Apps for SMBs
Salesforce is a CRM platform that allows small businesses to manage all of their sales, marketing and customer service communications in one place. Launched back…