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
Ken sfdc1Ken sfdc1 

Try Catch block for DML error

Hi Can anyone please add a try catch block for this trigger.


trigger UpdateAttemptsWithCase on Task (after insert, after update, after delete)
{
    Set<String> caseIds = new Set<String>();
    Set<String> taskIds = new Set<String>();
    Set<String> caseTaskIds = new Set<String>();
    
    if(Trigger.isInsert || Trigger.isUpdate)
    {
        for(Task t : Trigger.new)
        {
            if(Trigger.isInsert)
            {
                if(t.WhatId != null && validWhatId(t.WhatId))
                {
                    caseIds.add(t.WhatId);
                    caseTaskIds.add(t.WhatId);
                    taskIds.add(t.Id);
                }
            }
            else
            {
                Task oldTask = (Task)Trigger.oldMap.get(t.Id);
                if(t.WhatId != null && validWhatId(t.WhatId))
                {
                    caseIds.add(t.WhatId);
                }
                if(oldTask.WhatId != null && validWhatId(oldTask.WhatId))
                {
                    caseIds.add(oldTask.WhatId);
                }
                if(t.WhatId != oldTask.WhatId)
                {
                    caseTaskIds.add(t.WhatId);
                    taskIds.add(t.Id);
                }
            }
        }
    }
    else
    {
        for(Task t : Trigger.old)
        {
            if(t.WhatId != null && validWhatId(t.WhatId))
            {
                caseIds.add(t.WhatId);
            }
        }
    }
    List<Case> cases = new List<Case>();
    RecordType rt = [select Id from RecordType where Name = 'Adherence Call' and sObjectType = 'Case'];
    
    for(Case c : [select Attempts__c, (select Id,Subject,status from Tasks where (Subject = 'Call to Patient' or Subject = 'Call from Patient') and status = 'Completed') from Case where Id in :caseIds and recordTypeId = :rt.Id])
    {
        c.Attempts__c = c.Tasks.size();
        cases.add(c);
    }
    
    if(cases.size() > 0)
    {
        update cases;
    }
    
    
    List<Task> tasks = new List<Task>();
    for(Case c : [select ContactId, (select WhoId from Tasks where (Subject='Compass Approval Expiration' or Subject = 'Compass Copay End Date') and Id in :taskIds) from Case where Id in :caseTaskIds])
    {
        for(Task taskInfo : c.Tasks)
        {
            taskInfo.WhoId = c.ContactId;
            tasks.add(taskInfo);
        }
    }
    if(tasks.size() > 0)
    {
        update tasks;
    }
    
    private Boolean validWhatId(String val)
    {
        if(val.substring(0, 3) == '500')
        {
            return true;
        }
        return false;
    }
}
Best Answer chosen by Ken sfdc1
Andy BoettcherAndy Boettcher
You will want to wrap a try/catch block around any DML statements you do - example:
 
if(tasks.size() > 0)
    {
        try {
             update tasks;
        catch(Exception ex) {
             System.Debug(ex);
        }
    }