[SalesForce] Fill the WhoId and WhatId fields when create an event

In a test class I need to create an event object, and I want to assign the fields WhoId and WhatId.
For now I wrote that in my test class:

Account c= new Account(FirstName = 'test',
                       LastName = 'test');                               
    insert c;
    Event ev = new Event(StartDateTime = DateTime.now(),
                         EndDateTime = DateTime.now(),
                         Subject = 'Appel',
                         WhoId = c.Id,
                         WhatId = c.Id);
    insert ev;

but when I run the test class, I got the error :

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Name ID: incorrect type ID value;: 0017E00000BQTraQAH: [WhoId]

Best Answer

WhoId for an event has to be leadId or a contactId.

Create a contact record

  Contact cnt = new contact();
  Cnt.LastName = 'Test ';
  Insert cnt ;

Even.whoId = cnt.Id;

WhatId can be account,opportunity or custom object reference