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
lukas.wallisch1.3903815994810598E12lukas.wallisch1.3903815994810598E12 

Number of days between Event and CloseDate

Hi All,

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.
Another problem is, that CloseDate is only Date and not DateTime like my cloned StartDate. Any chance to calculate it anyway?

Thanks very much for your help!

Lukas
Best Answer chosen by lukas.wallisch1.3903815994810598E12
Venkat PolisettiVenkat Polisetti
@Lukas,

I have bulkified your trigger and changed it a bit. I did not test it though. i have included a template test case for your to fill in details. 

<pre>
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);
   }
  }

  if (changedAccount.size() > 0)
   update changedAccounts;
  }
}

/*
     Test class
*/

@IsTest
private class TestEventDateOnAccTrigger {

@isTest
static void test() {

  Account acc = new Account(Name='Test Account');
  insert acc;

  Event evt = new Event(<... set fields here>);
  insert evt;

  evt.endDateTime = DateTime.newInstance(<set new time here>);
  update evt;

}
}

/* end test class */

</pre>

Also look at this for further help on writing a test class on the Event Object.

https://developer.salesforce.com/forums?id=906F000000090OZIAY

Hope this helps.

Thanks,
Venkat

All Answers

Ankit Gupta@ DeveloperAnkit Gupta@ Developer
Hi,

You can use the EndDateTime of event object.

for more detail follow the below link :
http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_event.htm
Venkat PolisettiVenkat Polisetti
@Lukas,

You may have to resort to writing a APEX trigger on the Event object. You cannot use a cross object field update from a WFR based on Event. Through the trigger, you can pull the event date on to Account / Opportunity and then calculate your days.

Thanks,
Venkat Polisetti
lukas.wallisch1.3903815994810598E12lukas.wallisch1.3903815994810598E12
Hi,

thanks for your response. I already expected your answer, unfortunately I have no idea how to write a cross object trigger. Anyway I had a look and found something:

trigger EventDateOnAcc on Event (after insert, after update) {

Set <ID> acctIDs = new Set <ID> ();

for (Event e: Trigger.new){
   acctIDs.add(e.accountID);

   Map<ID, Account> acctMap = new Map<ID, Account> ([Select ID, Termin_am__c from Account where ID in :acctIDs]);

      Account acctRec = acctMap.get(e.accountID);

If ((e.accountID != null) && (e.z_Thema__c.indexOf('Demotermin')==0))
     If (acctMap.get(e.accountID).Termin_am__c < e.endDateTime || acctMap.get(e.accountID).Termin_am__c ==null){
      acctrec.Termin_am__c = e.endDatetime;
          update acctRec;
   }
  }
}


Had no mistakes in the Developer Console so far, but still I need a test class. Any ideas how it could look like?

Thank you, I really appreciate your advices!
Venkat PolisettiVenkat Polisetti
@Lukas,

I have bulkified your trigger and changed it a bit. I did not test it though. i have included a template test case for your to fill in details. 

<pre>
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);
   }
  }

  if (changedAccount.size() > 0)
   update changedAccounts;
  }
}

/*
     Test class
*/

@IsTest
private class TestEventDateOnAccTrigger {

@isTest
static void test() {

  Account acc = new Account(Name='Test Account');
  insert acc;

  Event evt = new Event(<... set fields here>);
  insert evt;

  evt.endDateTime = DateTime.newInstance(<set new time here>);
  update evt;

}
}

/* end test class */

</pre>

Also look at this for further help on writing a test class on the Event Object.

https://developer.salesforce.com/forums?id=906F000000090OZIAY

Hope this helps.

Thanks,
Venkat
This was selected as the best answer
lukas.wallisch1.3903815994810598E12lukas.wallisch1.3903815994810598E12
Hi Venkat,

wow, thanks so much! The test class passed, but the code coverage is only 31%, so it seems I have to add another test.

You really helped me a lot! Thank you!
Becky MillerBecky Miller
Hi

I am just learning how to create Apex code.  Super brand new.  

I need something similar and some guidance would be amazing.  

I need a number of days from Today() - Lastlogindate on the User Object.  The count will be used in Reporting so we do not have to use 2 different reports and we can make much better dashboards for the managers. 

Any guidance would be much appreciated.
Becky