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
Ratheven SivarajahRatheven Sivarajah 

How do you auto complete a miilestone

So I have created my milestone called First Response and tried to auto complete it when I send an email to a contact associated with the account. I found a code online but does not seem to work
https://help.salesforce.com/s/articleView?id=sf.entitlements_milestones_trigger.htm&type=5
Can you help me I have copied the apex class and trigger and still nothing works
Best Answer chosen by Ratheven Sivarajah
Shri RajShri Raj
Here's a step-by-step guide for auto-completing a milestone:
Create a custom field on the Milestone object called "Auto-complete on Email" of type checkbox.
Create a custom field on the Contact object called "Auto-complete Milestone" of type text.
Create a trigger on the EmailMessage object to populate the "Auto-complete Milestone" field on the Contact when an email is sent to a contact associated with an account. The trigger should look something like this:
trigger AutoCompleteMilestone on EmailMessage (after insert) {
    List<Id> contactIds = new List<Id>();
    for (EmailMessage email : Trigger.new) {
        if (email.ParentId.getSObjectType() == Account.SObjectType) {
            contactIds.add(email.ParentId);
        }
    }
    List<Contact> contacts = [SELECT Id, Auto_complete_Milestone__c FROM Contact WHERE Id IN :contactIds];
    for (Contact contact : contacts) {
        contact.Auto_complete_Milestone__c = 'First Response';
    }
    update contacts;
}

 

All Answers

VinayVinay (Salesforce Developers) 
Hi Ratheven,

Do you see any errors? Also check below similar example.

https://sfdcian.com/auto-complete-case-milestones-using-flow-and-trigger/

Thanks,
Ratheven SivarajahRatheven Sivarajah
Hey Vinay I see no errors in my code. It wont auto complete for some reasone. The link you provided is for resolution time - That wont work. Thank you
Shri RajShri Raj
Here's a step-by-step guide for auto-completing a milestone:
Create a custom field on the Milestone object called "Auto-complete on Email" of type checkbox.
Create a custom field on the Contact object called "Auto-complete Milestone" of type text.
Create a trigger on the EmailMessage object to populate the "Auto-complete Milestone" field on the Contact when an email is sent to a contact associated with an account. The trigger should look something like this:
trigger AutoCompleteMilestone on EmailMessage (after insert) {
    List<Id> contactIds = new List<Id>();
    for (EmailMessage email : Trigger.new) {
        if (email.ParentId.getSObjectType() == Account.SObjectType) {
            contactIds.add(email.ParentId);
        }
    }
    List<Contact> contacts = [SELECT Id, Auto_complete_Milestone__c FROM Contact WHERE Id IN :contactIds];
    for (Contact contact : contacts) {
        contact.Auto_complete_Milestone__c = 'First Response';
    }
    update contacts;
}

 
This was selected as the best answer