Skip to main content
1 of 3
Hengky Djapar
  • 1.8k
  • 2
  • 8
  • 18

Regarding your errors,

For error 1: You are calling the constructor without any parameter, but you defined your constructor to accept two parameters.

For error 2: co.asset__c is a lookup field (of Id data type), but you are comparing it against an SObject.

public static void ChangeOrderDuplicateChecker(List<Change_Order__c> cos)
{   ChangeOrderWrapper cow = new ChangeOrderWrapper();

        for(Change_Order__c co : trigger.new)
        {
            if(co.asset__c == cow.ast && co.opportunity__c == cow.opp)
            {
                co.addError('Testing');
            }
        }
}

To your question, I think you are complicating things a little bit. You can build a composite keys based on asset and opportunity lookup field and check if it already presents:

Set<Id> assetIds = new Set<Id>();
Set<Id> oppIds = new Set<Id>();
for(Change_Order__c co : trigger.new) {
    assetIds.add(co.asset__c);
    assetIds.add(co.opportunity__c);
}
Set<Id> compositeKeys = new Set<Id>();
for (Change_Order__c co : [SELECT asset__c, opportunity__c FROM Change_Order__c WHERE asset__c = :assetIds OR opportunit__c = :oppIds]) {
    compositeKeys.add(co.asset__c + co.opportunity__c);
}

for(Change_Order__c co : trigger.new) {
    if (compositeKeys.contains(co.asset__c + co.opportunity__c)) {
        co.addError('Duplicate found!');
    }
}

Or, you might be able to use no-code solution: Duplicate Rules

Hengky Djapar
  • 1.8k
  • 2
  • 8
  • 18