You need to sign in to do that
Don't have an account?
Abhishek Sharma 527
exception handling in trigger
Hello there, I have created custom roll up summary and now I was asked to add exception handling in that, as it's properly working code, I'm not sure where to put these try - catch block
can anyone plz help in this.
// my code
public class Contact_count_Handler
{
public static void InsertMethod(List<Contact> newList){
List<Account> accList = new List<Account>();
Set<id> accountIds = new Set<id>();
if(newList!=null) {
for(contact c: newList)
{
if(c.accountId!=null)
{
accountIds.add(c.accountId);
}
}
if(!accountIds.isEmpty()){
for(Account acc :[Select id,Number_of_linked_contact__c,(Select id,name from contacts) from Account where Id in : accountIds]){
acc.Number_of_linked_contact__c= acc.contacts.size();
acclist.add(acc);
}
if(acclist.size()>0){
update accList;
}
}
}
}
public static Void updateMethod(list<Contact> newList,map<Id,Contact>oldmap ){
List<Account> accList=new List<Account>();
if(oldmap != null){
Set<Id> setAccIds = new Set<Id>();
for(Contact con : newList){
if(con.AccountId != null && con.AccountId != oldMap.get(con.Id).AccountId){
setAccIds.add(con.AccountId);
setAccIds.add(oldMap.get(con.Id).AccountId);
}
}
if(!setAccIds.isEmpty()){
for(Account acc :[Select id,Number_of_linked_contact__c,(Select id,name from contacts) from Account where Id in : setAccIds]){
acc.Number_of_linked_contact__c= acc.contacts.size();
acclist.add(acc);
}
if(acclist.size()>0){
update accList;
}
}
}
}
public static Void deleteMethod(list<Contact> newList){
List<Account> accList=new List<Account>();
if(newList != null){
Set<Id> setAccIds = new Set<Id>();
for(Contact con : newList){
if(con.AccountId != null){
setAccIds.add(con.AccountId);
}
}
if(!SetAccIds.isEmpty()){
for(Account acc :[Select id,Number_of_linked_contact__c,(Select id,name from contacts) from Account where Id in : setAccIds]){
acc.Number_of_linked_contact__c= acc.contacts.size();
acclist.add(acc);
}
if(acclist.size()>0){
update accList;
}
}
}
}
}
can anyone plz help in this.
// my code
public class Contact_count_Handler
{
public static void InsertMethod(List<Contact> newList){
List<Account> accList = new List<Account>();
Set<id> accountIds = new Set<id>();
if(newList!=null) {
for(contact c: newList)
{
if(c.accountId!=null)
{
accountIds.add(c.accountId);
}
}
if(!accountIds.isEmpty()){
for(Account acc :[Select id,Number_of_linked_contact__c,(Select id,name from contacts) from Account where Id in : accountIds]){
acc.Number_of_linked_contact__c= acc.contacts.size();
acclist.add(acc);
}
if(acclist.size()>0){
update accList;
}
}
}
}
public static Void updateMethod(list<Contact> newList,map<Id,Contact>oldmap ){
List<Account> accList=new List<Account>();
if(oldmap != null){
Set<Id> setAccIds = new Set<Id>();
for(Contact con : newList){
if(con.AccountId != null && con.AccountId != oldMap.get(con.Id).AccountId){
setAccIds.add(con.AccountId);
setAccIds.add(oldMap.get(con.Id).AccountId);
}
}
if(!setAccIds.isEmpty()){
for(Account acc :[Select id,Number_of_linked_contact__c,(Select id,name from contacts) from Account where Id in : setAccIds]){
acc.Number_of_linked_contact__c= acc.contacts.size();
acclist.add(acc);
}
if(acclist.size()>0){
update accList;
}
}
}
}
public static Void deleteMethod(list<Contact> newList){
List<Account> accList=new List<Account>();
if(newList != null){
Set<Id> setAccIds = new Set<Id>();
for(Contact con : newList){
if(con.AccountId != null){
setAccIds.add(con.AccountId);
}
}
if(!SetAccIds.isEmpty()){
for(Account acc :[Select id,Number_of_linked_contact__c,(Select id,name from contacts) from Account where Id in : setAccIds]){
acc.Number_of_linked_contact__c= acc.contacts.size();
acclist.add(acc);
}
if(acclist.size()>0){
update accList;
}
}
}
}
}
Please use below code:-
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh
All Answers
You have to write all the code in try block and add the catch block as below. I am writing for single method.
Let me know if you face any issues.
If this solution helps, Please mark it as best answer.
Thanks,
Please use below code:-
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh