1

I am using Gmail, Salesforce integration app, using which I can save an interested mail into Salesforce Activity History of a Contact. Now, as soon as I receive the activity history, I want to save this into a custom field, named EmailBody__c.

I have tried with the below trigger but have no luck

trigger EmailBodyTrigger on Contact(before update) {
    Contact[] contacts = Trigger.new;
    for(Contact c : contacts) {
        try {
            ActivityHistory ah = c.ActivityHistories;
            if(ah != null) {
                contact.EmailBody__c = 'Hello Ram, How are you';
            }
        } catch(System.QueryException e) {
          // no trigger call   
        }
    }
}
2

Updating/creating an activity history doesn't affect on the contact DML operations so you need to use a trigger on Task instead of trigger on Contact then add required restrictions into the trigger in order to do your actions on the EmailBody__c field.

Below is just a simple:

trigger TestTrigger on Task (before update) {

    final Task tsk = Trigger.new[0];
    final ID whoId = tsk.WhoId;

    if(String.isNotEmpty(whoId)){
        try {
            final Schema.sObjectType entityType = whoId.getSObjectType();
            if(entityType == Contact.sObjectType){
                final Contact cnt = [Select Id, EmailBody__c from Contact where id=:whoId];
                cnt.EmailBody__c = 'Hello Ram, How are you';
                update cnt;
            }
        } catch(Exception e) {
            //Ignore...
        }
    }
}

Hope this will help you.

  • Thanks. As I am pretty new to SF apex, can you please, if possible, share the exact code, instead of this sample. – Ram Bavireddi Aug 3 '15 at 11:33
  • I edited my answer, take a look – Ruslan Vekua Aug 3 '15 at 12:02

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.