we want to know how many days it takes from visiting the customer until the opportunity is closed. We use events only for these visits, so every account only has one event. What I need to do is to copy the date of this event either to an opportunity or account to add another custom field to calculate the days between start date (I already cloned this field in the event to use it in formulas) and close date.
trigger EventDateOnAcc on Event (after insert, after update) {
Set <ID> acctIDs = new Set <ID> ();
List<Event> usableEventList = new List<Event>();
for (Event e: Trigger.new) {
if (e.AccountId != null && e.z_Thema__c != null & e.z_Thema__c.indexOf('Demotermin')==0) {
acctIDs.add(e.accountID);
usableEventList.add(e);
}
}
if (acctIds.size() > 0) {
Map<ID, Account> acctMap = new Map<ID, Account> ([Select ID, Termin_am__c from Account where ID in :acctIDs]);
List<Account> changedAccounts = new List<Account>();
for (Event e : UsableEventList) {
Account a = acctMap.get(e.AccountId);
If (a.Termin_am__c == null || a.Termin_am__c < e.endDateTime) {
a.Termin_am__c = e.endDatetime;
changedAccounts.add(a);
}
}
No comments:
Post a Comment