-
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 Spring 2022 Updates - Features to Know About
Take a Look at the Following Beta and Pilot Features: Use Knowledge Search Filters to Work More Efficiently (Pilot): – When a user uses the…
No Code Salesforce and WhatsApp Integration
Businesses are increasingly using messaging as their primary channel for customer communication. Each week, more than a billion users connect with a business account using…
Relationship Queries in SOQL | Salesforce Tutorial Guide
Relationship field contains at least 2 Objects i.e a Child Object and a Parent Object. These queries are used to fetch Data either from the…
Popular Salesforce Videos
What Are Roll-Up Summary Fields in Salesforce? | Video Tutorial
A roll-up summary field calculates values from related records, such as those in a related list. You can create a roll-up summary field to display…
Enable Salesforce Einstein Lead Scoring with Less Data - Winter 21
Previously we needed a minimum of 1000 new leads and 120 lead conversions in the previous 6 months to calculate the lead scoring. With Winter…
How Link Cartridge Development Can Your Business Storefront | Salesforce Link Cartridge
LINK Cartridge is a container for packaging and deploying program code and data, it’s structured in folders and subfolders for maximum efficiency.
Popular Salesforce Infographics
Do the Work: 8 Steps to Sales Call Preparation [CHECKLIST]
There is no shortage of sales tips and tricks to implement out there, we've certainly published our fair share, but at the end of the…
Salesforce Marketing Cloud Features, Benfits and Advantages
Elevate Your Strategy: Discover the 4 Power Moves with Salesforce Marketing Cloud 1. 𝐏𝐫𝐞𝐜𝐢𝐬𝐢𝐨𝐧 𝐓𝐚𝐫𝐠𝐞𝐭𝐢𝐧𝐠 Laser-focused campaigns for maximum impact. 2. 𝐃𝐚𝐭𝐚-𝐃𝐫𝐢𝐯𝐞𝐧 𝐈𝐧𝐬𝐢𝐠𝐡𝐭𝐬 Real-time analytics…
What are Salesforce Lightning Web Components?
Lightning Web Components ( LWCs ) are User Interface (UI) framework and utilized for creating applications with desktop and mobile technologies. This framework acts as…