0

I have a task trigger

public static void CustomObject_Email_rec_date_Upd(list<Task> lst_task){

    list<Custom_Object_c> lst_ELE_Agg_Var = new list<Custom_Object_c>();

          for(Task taskvar:lst_task) {                  
                  if(taskvar.Whatid !=null)
                  {                     
                   if(taskvar.Whatid.getSobjectType() == Schema.Custom_Object_c.SObjectType && taskvar.subject.contains('RE:')){

                       DateTime dT = System.now();
                       Date myDate = date.newinstance(dT.year(), dT.month(), dT.day());

                       Custom_Object_c ELE_Agg_Var = new Custom_Object_c();
                           ELE_Agg_Var.id = taskvar.Whatid;
                          // ELE_Agg_Var.Email_Received_Date__c = taskvar.createddate;
                          ELE_Agg_Var.Email_Received_Date__c = myDate;
                          ELE_Agg_Var.Read_Email__c = taskvar.WCT_Is_Read__c;
                          system.debug('+++++++++++=========='+taskvar.WCT_Is_Read__c);
                           lst_ELE_Agg_Var.add(ELE_Agg_Var); 
                   }
                  }

                Database.update(lst_ELE_Agg_Var,false);    
} 
}

Trigger:

 if(trigger.isAfter && trigger.isUpdate){

WCT_Task_Trigger_Handler.Ele_Agrements_Email_rec_date_Upd(Trigger.new);   

}

 if(trigger.isAfter && trigger.isInsert){
        WCT_Task_Trigger_Handler.Ele_Agrements_Email_rec_date_Upd(Trigger.new);   

    }

I am trying update Email receive date field and Read Email field in custom Object Whenever a task is created or updated.

I have received an email in Activity history and then Email receive date should update and when I open that Read Email field in Custom Object should be updated.

when we open email from Activity history Task trigger will not run? if not when will execute Task trigger to update fields?

Thanks in Advance

This is how we actually update task using this page:

<apex:page standardController="Task" >

 <script src="/soap/ajax/29.0/connection.js"></script>
 <script type="text/javascript"> 
 if('{!Task.WCT_Is_Read__c}' == 'false'){

    var tsk = new sforce.SObject("Task");
    tsk.Id = '{!Task.Id}';
    tsk.WCT_Is_Read__c = 'true';
    var mobId = '{!Task.What.Id}';
    var result = sforce.connection.update([tsk]);

    var tskRecord =  sforce.connection.query("Select Id,WCT_Is_Read__c from Task where what.id = '{!Task.What.id}' AND recordTypeId = '0121W000000gzDH' AND subject LIKE 'RE:%'");
    //alert('tskRecord'+tskRecord);
    var isMobUpdate;
    var tskRecords = tskRecord.getArray('records');
    var count = 0;
    for(var i = 0; i < tskRecords.length; i++){
        var tskRec = tskRecords[i];


        if(tskRec.WCT_Is_Read__c == 'true'){

            count++;

        }


    }

       if(tskRecords.length == count){
           // alert('Count matched');
            isMobUpdate = 'update';

        }

    if(isMobUpdate == 'update'){

        if("{!Task.WCT_Task_for_object__c}" == 'Pension_Request__c'){

            var mob = new sforce.SObject("Pension_Request__c");

            mob.Id = '{!Task.What.Id}';
            mob.PR_Unread_Email_on_this_Request__c = 'false';

            var mobresult = sforce.connection.update([mob]);
            // alert('result'+mobresult);
        }

    }
    // window.top.location='/'+'{!Task.Id}'; 
   }

</script>

Can we update any custom object fields using this page? if yes, Can you please tell me how?

1 Answer 1

1

You're correct, no triggers are fired when you "open" a record.

Triggers are only fired on update, insert and delete events. You'll have to actually update the record in order to fire your trigger to update "read" field on your related object.

If you're sending/receiving emails on case you should enable Email-To-Case as this creates an object called "Email Message". The Email Message has a built in "status" field that will automatically update when an email is read. But this only works for cases.

The only way I can think to achieve what you want using activities is to code a Visualforce page. This would allow you to, onload of the page, flag a "read" value on what ever object you want. Then you could override the "view" action on task/event objects with your page.

References:

Update

To access other objects when using the standard controller you'll want to code an apex extension and then query for the objects you want to update.

Lightning

I usually think in SF Classic. Thanks to Pranay for pointing out that you could have a bodyless lightning component that handles the init event perform an update on the object. Then simply add the component to your record detail page.

4
  • Hi gNerb Thanks for the response. i have updated my question can you please suggest me?
    – Frank
    Commented May 16, 2018 at 16:04
  • 1
    How about a lightning component with no body and having init event that makes record as opened? Commented May 16, 2018 at 16:05
  • @Frank I added another reference that will explain how to build a controller extension which will give you access to apex so you can query for and update records related to your task.
    – gNerb
    Commented May 16, 2018 at 16:08
  • @PranayJaiswal Updated my answer to include a lightning section with references as well. Thanks
    – gNerb
    Commented May 16, 2018 at 16:12

You must log in to answer this question.

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