You need to sign in to do that
Don't have an account?
SF Beginner 2019
accountcontact relationship insert and delete
I have this account contact relationship wherein runs in Batch, my issue is that the delete is kinda buggy wherein it sometimes delete and sometimes doesn't delete what could be the possible issue in here? thank you
global class AccountContactTriggerHandler implements Database.Batchable<sObject> { List<AccountContactRelation> ListtoInsert = new List<AccountContactRelation>(); Set<accountContactRelation> ListToDelete = new Set<accountContactRelation>(); Map<String, Account> accCodeMap = new Map<String,Account>(); map<string,Contact> contactAccountMap = new map<string, Contact>(); String query = 'Select Id,Name,License_ID__c FROM Account WHERE License_ID__c != null AND IsPartner = true AND License_ID__c LIKE \'__\''; final String CLSSNAME = 'AccountContactTriggerHandler'; global Database.QueryLocator start(Database.BatchableContext bc) { return Database.getQueryLocator(query); } global void execute(Database.BatchableContext bc,List<Account> batch){ try{ for(Account a : batch) accCodeMap.put(a.License_ID__c, a); for (Contact c : [Select ID, Name, Accountid, Person_ID__c, ABS_Person_ID__c, (SELECT AccountId, ContactId FROM AccountContactRelations) from Contact WHERE (ABS_Person_ID__c != null AND ABS_Person_ID__c LIKE '__') OR (Person_ID__c != null AND Person_ID__c LIKE '__')]){ Set<String> acrIdList =new Set<String>(); List<AccountContactRelation> acrList = c.AccountContactRelations; for(AccountContactRelation acrId : acrList){ contactAccountMap.put(acrId.AccountId, c); // Used for deleting code soql acrIdList.add(acrId.AccountId); } Account a, a2; if(c.Person_ID__c != null) a = accCodeMap.get(c.Person_ID__c); if(c.ABS_Person_ID__c != null) a2 = accCodeMap.get(c.ABS_Person_ID__c); if(a != null && a2 != null && a == a2){ if(!acrIdList.contains(a.Id)){ if(c.Person_ID__c == a.License_ID__c){ AccountContactRelation acr = new AccountContactRelation(); acr.AccountId = a.id; acr.contactId = c.Id; ListtoInsert.add(acr); } } } else { // Checks if the AccountContactRelations already connected to the Contact if(a != null){ if(!acrIdList.contains(a.Id)){ if(c.Person_ID__c == a.License_ID__c){ AccountContactRelation acr = new AccountContactRelation(); acr.AccountId = a.id; acr.contactId = c.Id; ListtoInsert.add(acr); } } } if(a2 != null){ if(!acrIdList.contains(a2.Id)){ if(c.ABS_Person_ID__c == a2.License_ID__c){ AccountContactRelation acr = new AccountContactRelation(); acr.AccountId = a2.id; acr.contactId = c.Id; ListtoInsert.add(acr); } } } } } insert ListtoInsert; //loop only the accounts that are related/connected to AccountContactRelation Object map<string,Account> accountMap = new map<string, Account>(); for(Account Acc: [SELECT id, Name, License_ID__c FROM Account WHERE Id =: contactAccountMap.keySet()]) { accountMap.put(Acc.Id,acc); } //loop the AccountContactRelation to get the records where isDirect is not true for(AccountContactRelation AccountConDelete : [SELECT Id, AccountId, ContactId FROM AccountContactRelation WHERE Accountid =: contactAccountMap.keySet() AND isDirect != TRUE]) { Account acc = accountMap.get(AccountConDelete.AccountId); contact con = contactAccountMap.get(AccountConDelete.AccountId); if(acc.License_ID__c != con.Person_ID__c && acc.License_ID__c != con.ABS_Person_ID__c) { ListToDelete.add(AccountConDelete); } } System.debug('TO BE DELETED: '+ListToDelete); List<AccountContactRelation> AccountConNew = new List<AccountContactRelation>(); for (AccountContactRelation acr : ListToDelete) AccountConNew.add(acr); delete AccountConNew; } catch(Exception e){ } } global void finish(Database.BatchableContext bc) { //DoNothing. } }
Greetings!
I can see that the issue is intermittent.So,If you have any scenario in which you can some of the records are deleted and some are not then I would suggest you to enable the debug logs and see what is causing the issue is the best option for now.
https://help.salesforce.com/apex/HTViewHelpDoc?id=code_add_users_debug_log.htm&language=en_us
Kindly let me know if it helps you and close your query by marking it as best answer so that it can help others in the future.
Warm Regards,
Shirisha Pathuri