1

I have a many to many relation. Account obj, AccountAdress junction, Adres obj. The junction object has a startdate an enddate to make an address active for an Account.

I want to use a batch process to update all Accounts with the Adress which is active on the junction.

What would be the most effective soql to achieve this. Do I query from the junction object to get the accounts and adresses and pas them into maps and match them and update. Or do query from account and update if a active adress is found

List<Accounts> accList = [Select id from account];//Batch soql
List<Adress> updateAdresses = [SELECT Id, street 
    FROM Adress
    WHERE Id IN 
        (SELECT Adress 
        FROM AcountAdress 
        WHERE account__c IN : accList
        )
];

or

[SELECT id, startdate, enddate, account__c,addres__c FROM AcountAdress where today>startdate AND today<startdate];
 create map for account and loop trough AccountAdress and update with Adress

2 Answers 2

1

I would say your query should be based on the junction object. Therefore you don't need a map, because a junction object itself has the Map characteristic (key => value)

List<Account> accountsToUpdate = new List<Account>();
for (AccountAdress addr : [SELECT id, startdate, enddate, account__c, addres__c 
        FROM AcountAdress 
        WHERE today>startdate  today<startdate
        ]) {
    Account acc = new Account(
        Id = addr.account__c,
        Field1 = Value,
        Field2 = Value
    );
    accountsToUpdate.add(acc);
}
update(accountsToUpdate);
0

Your first Approach does not contain the startDate so it does not match your criteria. Anyway the second approach seems preferable. Since it seems a periodic task (dialy?) I would use a Scheduleable Job to run it earch day after midnight...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .