0

I have the following trigger that is working fine. I just want to include this date field Last_Showing_Date__c to be updated with the TODAY. The field is on the contact object.

Can you please help me on how to do that.

trigger showingWorkflowTrigger on Event (after insert, after update) {
    List<Event> events = Trigger.new;
    Map<Id, Event> oldEvents = null;
    if (Trigger.isUpdate) {
        oldEvents = Trigger.oldMap;
    }
    for (Event event : events) {
        RecordType rt = [Select Name FROM RecordType where Id = :event.RecordTypeId];
        if (rt.Name == 'Showing' && event.WhoId != null) {
            //cambiar el stage del contacto a 'Showing'
            Contact co = [Select Stage_fm__c from Contact where Id = :event.WhoId];
            if (co == null) {
                continue; //No es un contacto
            }

            if (Trigger.isUpdate) {
                //también puede ser que sea un update y anteriormente no tuviese contacto
                Event oldEvent = oldEvents.get(event.Id);
                if (oldEvent.WhoId != null) {
                    Contact oldCo = [Select Stage_fm__c from Contact where Id = :oldEvent.WhoId];
                    if (oldCo != null && oldCo.Id == co.Id) {
                        continue; //Es el mismo contacto
                    }
                }
            }
            if (co.Stage_fm__c != 'Offer' &&  co.Stage_fm__c != 'Showing') { //Si ya está en offer, lo djamos igual
                co.Stage_fm__c = 'Showing';
                update co;
            }
        }
    }
}
4
  • uyou can update today date with date class like Last_Showing_Date__c = Date.Today()
    – Pavan tej
    Feb 10, 2017 at 10:42
  • Please explain a bit briefly where you want to update the contact with any criteria.
    – Pavan tej
    Feb 10, 2017 at 10:43
  • I don't know how to include the line of code in to the trigger. So basically what this trigger is doing is when you have a new event Showing is changing the stage of the contact to Showing. What I want to achieve is to track the showing date Feb 10, 2017 at 10:46
  • While I don't have the answer, I would highly suggest moving this code into a class and refactoring the update out of your loop. You're setting yourself up for major issues in the future if you don't. Feb 11, 2017 at 5:38

1 Answer 1

2

You can use workflow rule here.

Rule: AND( PRIORVALUE(Stage_fm__c) = 'Showing' , Stage_fm__c != 'Showing')

Field Update on Last_Showing_Date__c: Today()

2
  • I know I can use a workflow rule, it was just to keep everything in one place and since I have the trigger I wanted to do it there Feb 10, 2017 at 11:15
  • 1
    Clicks not code.
    – Adrian Larson
    Feb 10, 2017 at 11:20

You must log in to answer this question.

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