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
Angela SchloederAngela Schloeder 

Trigger -

I cannot figure out what Im doing wrong. I need it to have Priority of Low if no Direct Dial and/or Mobile on Contact and Medium if there is Direct Dial AND/OR Mobile. Medium works but not Low.

Any ideas? Help is very much appreciated.

trigger createTask on Contact (after insert, after update) {
    List<Task> listOfTaskToBeInserted = new List<Task>();
    Task newtask;
    for(Contact con : trigger.new){
        if((trigger.isInsert && (con.Direct_Dial__c == '' || con.MobilePhone == '')) || (trigger.isUpdate && trigger.oldMap.get(con.id).Exec_Type__c != con.Exec_Type__c && (con.Exec_Type__c == 'Key Exec' || con.Exec_Type__c == 'Approved Exec'))){
            newtask = new Task();
            newtask.WhatId = con.AccountId;
            newtask.Subject = 'Target';
            newtask.whoId = con.id;
            newtask.ActivityDate = system.today();
            newTask.Status = 'Open';
            newTask.Attendee__c = con.Task_Attendee__c;
            if(con.Direct_Dial__c == ''){
               newTask.Priority = 'Low';
            }
            else{
                newTask.Priority = 'Medium';
            }
           listOfTaskToBeInserted.add(newTask);
        }
    }
    if(listOfTaskToBeInserted.size() > 0){
        insert listOfTaskToBeInserted;
    }
}
Nish321Nish321
Hi Angela,

Try the below and let me know if you face any issues :
 
trigger createTask on Contact (after insert, after update) {
    List<Task> listOfTaskToBeInserted = new List<Task>();
    Task newtask;
    for(Contact con : trigger.new){
        if((trigger.isInsert && (String.isBlank(con.Direct_Dial__c) || String.isBlank(con.Direct_Dial__c))) || (trigger.isUpdate && trigger.oldMap.get(con.id).Exec_Type__c != con.Exec_Type__c && (con.Exec_Type__c == 'Key Exec' || con.Exec_Type__c == 'Approved Exec'))){
            newtask = new Task();
            newtask.WhatId = con.AccountId;
            newtask.Subject = 'Target';
            newtask.whoId = con.id;
            newtask.ActivityDate = system.today();
            newTask.Status = 'Open';
            system.debug('con.Direct_Dial__c is ..' +con.Direct_Dial__c);
        //    newTask.Attendee__c = con.Task_Attendee__c;
            if(String.isBlank(con.Direct_Dial__c)){
           system.debug('con.Direct_Dial__c is' +con.Direct_Dial__c);
               newTask.Priority = 'Low';
            }
            else{
                newTask.Priority = 'Medium';
            }
           listOfTaskToBeInserted.add(newTask);
        }
    //    insert listOfTaskToBeInserted;
        }
    
    system.debug('listOfTaskToBeInserted.size() is ..' +listOfTaskToBeInserted.size());
 if(listOfTaskToBeInserted.size() > 0){
      insert listOfTaskToBeInserted;  
    }
}