0

I am running a trigger without sharing mode and I am getting this error

Field is not writeable: CaseMilestone.CompletionDate

I used this code in before trigger

cM.CompletionDate = System.now();

Can anybody help me out ?

3
  • You need to make sure you update the CaseMilestone before the case saves as closed. The milestone must already be closed when the case is closed. Use a before update trigger on your case, and make sure that you update your completion date prior to closing the case. Commented Oct 1, 2016 at 5:00
  • 1
    I am not closing Case Commented Oct 1, 2016 at 5:05
  • Been a while since I used them but isn't it just a case of marking it as completed and SF sets the date to now?
    – Girbot
    Commented Oct 1, 2016 at 6:35

2 Answers 2

0

You need to change the status of the milestone and the system will automatically set the completion date/time. You cannot write to this field.

0

Try below code .. works like a charm ...

trigger CaseMileStoneTestJos on Case (after update) {

    Set<Id> caseIds = new Set<Id>();

    for (Case record : Trigger.new){
        caseIds.add(record.Id);
    }

    List<CaseMilestone> caseMilestones = [
        SELECT 
            Id
        FROM CaseMileStone
        WHERE CaseId IN :caseIds
    ];

    for (CaseMileStone cm : caseMilestones) {
        cm.CompletionDate = System.today();
    }

    update caseMilestones;
}
1
  • unless properly commented, please avoid posting code dumps as a solution. include reference links if possible alongside the essentials . Take some time to read How to Answer and take the tour in order to get familiar with the forum. Thanks, and welcome to SFSE!
    – glls
    Commented Oct 25, 2017 at 13:29

You must log in to answer this question.

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