1

I have a problem with the below trigger

trigger ContactTrigger on Contact (before insert, before update) {
if (Trigger.isInsert && Trigger.isBefore) {

    Aggregateresult result = Database.query('SELECT datev__c FROM Contact WHERE datev__c != NULL ORDER BY datev__c DESC LIMIT 1');

    if (result != null && result.get('datev__c') != null) {
      Decimal datev = (Decimal) result.get('datev__c');

        for (Contact c : Trigger.new) {
            datev = datev + 1;
            c.datev__c = datev;
        }
    }
}

if (Trigger.isUpdate && Trigger.isBefore) {
    for(Contact c : Trigger.new) {
        if(c.Bank_IBAN__c != null && !SEPA_Toolkit.SEPAUtilities.ValidateIBAN(c.Bank_IBAN__c)) {
            c.Bank_IBAN__c.addError('Invalid IBAN. Please check for typos');
        }
    }
}}

The trigger saves fine, but when I try and convert a Lead, I get the following error:

Error: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, ContactTrigger: execution of BeforeInsert caused by: System.TypeException: Invalid conversion from runtime type List to AggregateResult Trigger.ContactTrigger: line 8, column 1: [] (System Code)

Could you please help me troubleshoot it? Thanks!

1

1 Answer 1

2

Why do you use an AggregateResult if you are not aggregate the result? Use List instead:

List<Contact> result = Database.query('SELECT datev__c FROM Contact WHERE datev__c != NULL ORDER BY datev__c DESC LIMIT 1');

if (!result.isEmpty() && result.size() == 1) {
  Decimal datev = (Decimal) result[0].datev__c;
...
1
  • Yes, that makes sense! I'm new to Apex, so learning as I go. Thanks for your input.
    – Quicksilvr
    Dec 16, 2015 at 16:02

You must log in to answer this question.

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