I need to compare contract[0] from the same Account with the contract[1], if they have same values as in the specific fields that I'm using in SOQL I don't need to add it on my map.So if contract[1] is equal contract[0], contract[1] is not added to map, so the contract[0] is now compared with contract[2] and etc. That is my code so far.

public class ContractValidation {

public static void ClassifyContract(List<Contract> newContracts){
    Map<Id, Contract[]> oldContracts = new Map<Id, Contract[]>();
    for(Contract record: newContracts) {
        oldContracts.put(record.AccountId, new Contract[0]);

    for(Contract record: [SELECT AccountId, EndDate, Code__c, ProductNumber__c FROM Contract WHERE AccountId in :oldContracts.keySet()]) {         

    for(Contract record: newContracts){      
        for(Contract oldRecord: oldContracts.get(record.AccountId)){                           
            // ...                                           
  • I didn't understand your requirement completely because If an account having multiple contracts with different values it will consider only one. And why multiple loops what you're expecting – Pavan tej Jul 22 '20 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.