function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SittamSittam 

trigger on task

hey guys,

since it is still not possible to trigger a workflow from email to task in salesforce, im trying to solve that with an apex trigger. im trying to change the custom field(newTask.Ergebnis_Aktivit_t__c) to my chosen value, if the subject starts with "E-Mail"
Im pretty new to apex and this created from what i found on the internet.

trigger emailergebnis on Task (before Insert) {

    /*Iterate over the newly created Tasks one by one*/
    for(Task newTask : trigger.new){

        /*Check if the Task is being created as result of Email to Task*/
        if(
            !String.isBlank(newTask.Subject) &&
            newTask
                .Subject
                .toLowerCase()
                .startsWith('E-Mail:')
        ){
            newTask.Ergebnis_Aktivit_t__c = 'Email / SMS / Whatsapp';
        }
    }
}
SittamSittam
it does not change anything. would appreciate if somebody can guide me to the error
Raj VakatiRaj Vakati
Hey, 
Looks like its salesforce issues .. i tested your code couple of instances .. Once instance it is not working but other instance it working as expected 

 
trigger TaskT on Task (before insert) {
    
    for(Task tNew : trigger.new){
        
        System.debug('--->'+tNew.Subject.toLowerCase().startsWith('e-mail:'));
        System.debug('--1123'+!String.isBlank(tNew.Subject));
        /*Check if the Task is being created as result of Email to Task*/
        if(!String.isBlank(tNew.Subject) && tNew.Subject.toLowerCase().startsWith('e-mail:')){
            System.debug('.................>>');
             tNew.Ergebnis_Aktivit_t__c = 'Email / SMS / Whatsapp';
            //tNew.Status='Deferred';
            tNew.Subject='Email-----Update';
            // newTask.addError('NOWWWW');
        }
    }
}