1

I have a Custom (Managed) object called Individual Email Result (et4ae5__IndividualEmailResult__c) which contains a Lookup field to a Contact record (et4ae5__Contact__c) and I want to create a schedulable Apex Class to execute daily and update all of the related Contact records (with specific data from the fields of the Individual Email Result record) when it detects that a particular Individual Email Result checkbox (MC_ANY_Sent__c) equals 'true'.

Here is the code that I have so far:

    public class MCANYSent implements Schedulable, Database.Batchable<sObject>{

    public void execute( SchedulableContext context )
    {
        Database.executeBatch( this );
    }

    public Database.QueryLocator start( Database.BatchableContext context )
    {
        return Database.getQueryLocator
        (   'SELECT Id, MC_ANY_Sent__c, et4ae5__DateSent__c, Name, et4ae5__FromName__c, et4ae5__FromAddress__c, et4ae5__Contact__c'
        +   'FROM et4ae5__IndividualEmailResult__c'
        +   'WHERE MC_ANY_Sent__c = true'
        );
    }

    public void execute( Database.BatchableContext context, List<et4ae5__IndividualEmailResult__c> individualemailresults )
    {
        for ( et4ae5__IndividualEmailResult__c ier : individualemailresults )
        {
            ier.et4ae5__Contact__r.MC_Date_of_Last_ANY_Sent__c = ier.et4ae5__DateSent__c;
            ier.et4ae5__Contact__r.MC_Last_Email_Title__c = ier.Name;
            ier.et4ae5__Contact__r.MC_Last_Email_Sender__c = ier.et4ae5__FromName__c + '(' + ier.et4ae5__FromAddress__c + ')';
        }
        update individualemailresults.et4ae5__Contact__c;
    }

    public void finish( Database.BatchableContext context )
    {
        // nothing to do here
    }
}

However, when I try to save this, I am getting the following error; Error: Compile Error: Variable does not exist: et4ae5__Contact__c at line 25 column 39

Line 25 is the 'update individualemailresults.et4ae5__Contact__c;' line.

I don't need to update the Individual Email Result record, as it isn't being changed, so I'm only looking to update the Contact record in the Lookup field (et4ae5__Contact__c), where all of the changes have been made.

Any ideas?

1

You are trying to access et4ae5__Contact__c field of et4ae5__IndividualEmailResult__c object on the list hence you are facing this issue. You cannot update related parent records by updating child list.

Updated code -

    public class MCANYSent implements Schedulable, Database.Batchable<sObject>{

    public void execute( SchedulableContext context )
    {
        Database.executeBatch( this );
    }

    public Database.QueryLocator start( Database.BatchableContext context )
    {
        return Database.getQueryLocator
        (   'SELECT Id, MC_ANY_Sent__c, et4ae5__DateSent__c, Name, et4ae5__FromName__c, et4ae5__FromAddress__c, et4ae5__Contact__c'
        +   ' FROM et4ae5__IndividualEmailResult__c'
        +   ' WHERE MC_ANY_Sent__c = true'
        );
    }

    public void execute( Database.BatchableContext context, List<et4ae5__IndividualEmailResult__c> individualemailresults )
    {
        Set<Id> contactIdSet = new Set<Id>();
        List<Contact> contactsList = new List<Contact>();
        for ( et4ae5__IndividualEmailResult__c ier : individualemailresults )
        {
            if(!contactIdSet.contains(ier.et4ae5__Contact__r.Id)) {
                contactIdSet.add(ier.et4ae5__Contact__r.Id);
                Contact con  = new Contact();
                con.Id = ier.et4ae5__Contact__r.Id;
                con.MC_Date_of_Last_ANY_Sent__c = ier.et4ae5__DateSent__c;
                con.MC_Last_Email_Title__c = ier.Name;
                con.MC_Last_Email_Sender__c = ier.et4ae5__FromName__c + '(' + ier.et4ae5__FromAddress__c + ')';
                contactsList.add(con);
            }

        }
        update contactsList;
    }

    public void finish( Database.BatchableContext context )
    {
        // nothing to do here
    }
}
|improve this answer|||||
  • Thank you Amit. It saved okay (except that I had to add a ';' after contactsList.add(con), however, I scheduled it to run last night, and none of the Contact records were updated, although, I didn't receive any errors either. Any ideas? – Rexamillian Dec 24 '19 at 0:35
  • Sorry... just noticed the following error in the Apex Jobs monitoring. 23/12/2019 2:00 PM Batch Apex Failed First error: unexpected token: MC_ANY_Sent__c – Rexamillian Dec 24 '19 at 1:03
  • Add space before FROM and WHERE in dynamic query in start method. See updated answer. – Amit Dec 24 '19 at 6:36

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.