0

I'm writing a trigger that chops up a long-text field on an event activity record and puts it into the appropriate fields on the related contact's record. The problem is, the code seems to be firing twice.

  • 1st fire: when I System.debug() the code, it showed that the first time, the updated text from the event field did not show.

  • 2nd fire: when I System.debug() the code it shows the information from the event field.

CODE BELOW:

 trigger remeetUpdate on Event (after update) {
    Event e = Trigger.new[0];

    List<Contact> c = [
        SELECT Id, Person_s_met_Place_met__c, Current_Responsibilities__c, Personality_Impression__c, Additional_Information__c, Hot_Buttons__c, Companies_Introducing_to__c, Companies_Met_in_the_past__c
        FROM Contact
        Where Contact.Id = :e.WhoId
    ];

    if (e.Write_up_completed__c == True && e.Type__c == 'Remeet') {
        // chop up the comments field
        // stick them in the write-up field

        String comments = e.Comments__c;
        List<String> sections = new List<String>();

        List<String> contactFields = new List<String>();
        contactFields.add('Person_s_met_Place_met__c');
        contactFields.add('Personality_Impression__c');
        contactFields.add('Current_Responsibilities__c');
        contactFields.add('Additional_Information__c');
        contactFields.add('Companies_met_in_the_past__c');
        contactFields.add('Companies_Introducing_to__c');
        contactFields.add('Hot_Buttons__c');

        List<String> commentArray = new List<String>();

        sections = comments.split('IMPRESSION:|CURRENT POSITION/ RESPONSIBILITIES:|ADDITIONAL INFORMATION:|COMPANIES MET RECENTLY:|COMPANIES INTRODUCING TO:|UPDATED HOT BUTTONS:');
        for (Integer i = 0; i < sections.size(); i++) {
            if (i == 0) {
                sections[0] = sections[0].replace('CONSULTANTS MET, DATE, AND PLACE:', 'REMEET ' + e.StartDateTime);
                sections[0] = sections[0] + '*****' + '\n';
                commentArray.add(sections[0]);
            } else {
                commentArray.add('REMEET ' + e.StartDateTime + sections[i] + '*****' + '\n');
            }  
        }

        for (Integer j = 0; j < commentArray.size(); j++) {
            c[0].put(contactFields[j], commentArray[j] + c[0].get(contactFields[j]));
        }

        update c[0];
    }
}

Feels like a bit of a catch 22. I can't use a helper class to tell it to only fire once because the first time the information isn't getting through to the contact record.

1

Solved. This issue was a combination of pointing to the wrong field and multiple after update triggers firing. I pointed to the correct "Description" field on the event rather than a custom formula field that copies the description field. I also put in a helper class to only allow the trigger to update once.

| improve this answer | |

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.