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
Shannon.ax1730Shannon.ax1730 

Can anyone help determine why trigger is not working?

Hello,

 

I have a trigger that is supposed to update the Contact of a case that is closed:

 

 trigger setLastSurveySentDate on Case (after insert) {
    Map<Id,Id> contactToCaseMap = new Map<Id,Id>();
    for(Case A : trigger.new)
        contactToCaseMap.put(A.ContactId,A.Id);
 
    List<Contact> contactsToUpdate = new List<Contact>{};
      
    for (Contact con: [SELECT Id,Last_Survey_Sent_Date__c FROM Contact WHERE Id IN:  contactToCaseMap.keySet()]) {
        Id caId = contactToCaseMap.get(con.Id);
        Case ca = trigger.newMap.get(caId);
        if (ca.Status=='Closed' && con.Last_Survey_Sent_Date__c < date.today()-90 ){
            con.Last_Survey_Sent_Date__c=date.today();
            contactsToUpdate.add(con);
        }
        
        else if (ca.Status=='Closed' && con.Last_Survey_Sent_Date__c == null ){
            con.Last_Survey_Sent_Date__c=date.today();
            contactsToUpdate.add(con);
        }
    }

    if(contactsToUpdate != null && !contactsToUpdate.isEmpty())
        Database.update(contactsToUpdate);
}

 

I have a workflow that sends a survey, updates the case Last Survey Sent Date case field, and records a task that the survey was sent, as long as the Contact has not received a survey within the last 90 days:

 

OR( today() - DATEVALUE(Contact.Last_Survey_Sent_Date__c) >= 90,isBlank(Contact.Last_Survey_Sent_Date__c))

 

The trigger is updating the contact field, however, it is not using the date that has been updated on the case.

 

The date of  survey sent CASE = 09/03/2013 12:15 AM

The date of survey sent CONTACT = 09/02/2013 8:00 PM

 

The date of survey sent on the contact is always the same time and the date changes depending on the real time of day (e.g. tests performed prior to 12 AM showed a date of 09/02/2013 8:00 PM)

 

Can you please help?

 

Thank you very much!

 

Shannon

 

Shannon.ax1730Shannon.ax1730

Just bumping this up. Please need help.

KrishHariKrishHari

I see that your trigger is defined for 'after insert' but from your code and the problem description, it supposed to be fired for 'after update' event. Can you update the event from 'after insert' to 'after update' and try it.

 

Regards,

HK.

 

Shannon.ax1730Shannon.ax1730

Thanks HK! I actually went back to the first trigger I wrote, and it worked! Now I am trying to figure out how to write a trigger that sends an email because I exceeded my workflow limits...ughhh

KrishHariKrishHari

Glad that it worked. Check out my answer that I posted today about sending an email from trigger. The actual blog post can be found here.

 

Regards,

HK.