public void addVisibilite() {
Map<Id, List<Id>> accountContactRelationIdsMap = new Map<Id, List<Id>>();
for (AccountContactRelation accountContactRelation : (List<AccountContactRelation>) Trigger.new) {
String oldRoles = (Trigger.isUpdate) ? ((AccountContactRelation) Trigger.oldMap.get(accountContactRelation.Id)).Roles : null;
if (accountContactRelation.Roles != null && accountContactRelation.Roles.contains(labelVisibilitePortail) && (Trigger.isInsert ||
(Trigger.isUpdate && (oldRoles == null || !oldRoles.contains(labelVisibilitePortail))))) {
if (!accountContactRelationIdsMap.containsKey(accountContactRelation.AccountId)) {
accountContactRelationIdsMap.put(accountContactRelation.AccountId, new List<Id>());
}
accountContactRelationIdsMap.get(accountContactRelation.AccountId).add(accountContactRelation.ContactId);
}
}
Set<Id> setIds = accountContactRelationIdsMap.keySet();
Database.executeBatch(new toInsertOrderBillPriceShares_Batch('SELECT Id, FAB_AccountBill__c, FAB_AccountOrder__c FROM FAB_Order_Bill_Price__c WHERE FAB_AccountOrder__c IN : setIds OR FAB_AccountBill__c IN : setIds'));
And here's my batch ;
public toInsertOrderBillPriceShares_Batch(List<AccountContactRelation> oldRelations, List<AccountContactRelation> relations ) {
for (AccountContactRelation accountContactRelation : relations) {
String oldRoles = (Trigger.isUpdate) ? ((AccountContactRelation) Trigger.oldMap.get(accountContactRelation.Id)).Roles : null;
if (accountContactRelation.Roles != null && accountContactRelation.Roles.contains(labelVisibilitePortail) && (Trigger.isInsert ||
(Trigger.isUpdate && (oldRoles == null || !oldRoles.contains(labelVisibilitePortail))))) {
if (!accountContactRelationIdsMap.containsKey(accountContactRelation.AccountId)) {
accountContactRelationIdsMap.put(accountContactRelation.AccountId, new List<Id>());
}
accountContactRelationIdsMap.get(accountContactRelation.AccountId).add(accountContactRelation.ContactId);
}
}
System.debug('Firaaas Account contact relation'+accountContactRelationIdsMap);
}
global Database.QueryLocator start(Database.BatchableContext BC) {
return Database.getQueryLocator([
SELECT Id, FAB_AccountBill__c, FAB_AccountOrder__c
FROM FAB_Order_Bill_Price__c
WHERE FAB_AccountOrder__c IN : accountContactRelationIdsMap.keyset() OR FAB_AccountBill__c IN : accountContactRelationIdsMap.keyset()
]);
Execute batch :
Database.executeBatch(new toInsertOrderBillPriceShares_Batch(Trigger.old, Trigger.new));
My batch doesn't work preperly , don't know where the problem in my code Maybe i need to change decalaration of Old roles String oldRoles = (Trigger.isUpdate) ? ((AccountContactRelation) Trigger.oldMap.get(accountContactRelation.Id)).Roles : null; ?