-
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
How To Play Audio & Video Files in Salesforce Through Media Files Player
Salesforce leaves no stone unturned when it comes to customer satisfaction and customer relationship. Ever since cloud computing technology has become a part of our…
5 Ways Salesforce CPQ can Transform Your Sales Process
You understand the value of streamlining your sales process as a salesman. You can spend more time completing business if you spend less time making…
Top 10 Salesforce Winter '23 Release Features
The most awaited release of the year is finally here. The Salesforce Winter '23 release provides succinct, high-level explanations of improvements and new modified features…
Popular Salesforce Videos
Entrepreneurship in the Salesforce Ecosystem
The Salesforce Women’s Entrepreneur Group is a group of women who have founded and own businesses in the Salesforce ecosystem. Our goal is to inspire,…
Salesforce Macros - All You Need To Know!
Macros in Salesforce allows you to perform repetitive actions in Service Cloud for the records of Account, Contact, Case, and Lead (Supported for Feed based…
Create Custom Auto Number Field With Your Criteria Using Flows | Salesforce
Have you ever want to have an auto number field but it only populates when certain criteria were met on a record? Now you can…
Popular Salesforce Infographics
Here’s What Makes Up Salesforce’s Customer Base
Recently Mintigo took a close look at Salesforce’s customer data. Their analysis sheds some light on what their customers are up to and how the customers have…
Back to Basics: Salesforce Data Loading
Discover best practices for Salesforce data loading straight from the dataloader.io community with this useful Salesforce infographic. Users can quickly and easily delete or move…
CRM Predictions 2018
The coming year will be a stirring one for businesses, esp while talking about CRM technology. With the arrival of the prognosticating season, let’s quickly…