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
Shriya BhalchandranShriya Bhalchandran 

please help me in resolving this error

Req: one custom object: Test...which has lookup relationship with Account object.
So now as we know in Account related list:> open Activities> we can create new event.
so on creating new event, wherein if status is Closed...that same record should automatically create in test object.

Solution:
trigger records on Event (before insert) {

Map<Id, Event> eventsByAccount = new Map<Id, Event>();   // Map all the events to their account.
    for(Event ev : (Event[]) Trigger.new) {
        eventsByAccount.put(ev.WhatId, ev);
}

Map<Id,Employee__c> EmpsById = new Map<Id,Employee__c>([SELECT Id,EmpAcc__c FROM Employee__c WHERE EmpAcc__c IN: eventsByAccount.keyset()]);  // Bring all the employee records related to your accounts.

List<Event> testEventsToInsert = new List<Event>();
  for(Employee__c emp : EmpsById.values()) {                           // Get the event of the test's account.
      Event testEvent = eventsByAccount.get(emp.EmpAcc__c);

      testEvent.WhatId = emp.Id;                                                 // Associate it to Employee__c record.

      testEvent.Id = null;                                                              // Clean its Id so you can insert the new event.

      testEventsToInsert.add(emp);                                              // Add it to a list for later insertion.
}

     insert testEventsToInsert;                                                    // Insert all the new events related to test records.
    



I have created this trigger but 1 error is coming on line 23 as Method does not exist or incorrect signature: void add(Employee__c) from the type List<Event>. Please help me in resolving this. 
Ahmad J. KoubeissyAhmad J. Koubeissy
That's because you are assigning employee__c to a list of Events. you should replace  testEventsToInsert.add(emp);   ​with testEventsToInsert.add(testEvent);   ​
 
trigger records on Event (before insert) {

Map<Id, Event> eventsByAccount = new Map<Id, Event>();   // Map all the events to their account.
    for(Event ev : (Event[]) Trigger.new) {
        eventsByAccount.put(ev.WhatId, ev);
	}

	Map<Id,Employee__c> EmpsById = new Map<Id,Employee__c>([SELECT Id,EmpAcc__c FROM Employee__c WHERE EmpAcc__c IN: eventsByAccount.keyset()]);  // Bring all the employee records related to your accounts.

	List<Event> testEventsToInsert = new List<Event>();
	for(Employee__c emp : EmpsById.values()) {                           // Get the event of the test's account.
		Event testEvent = eventsByAccount.get(emp.EmpAcc__c);
		testEvent.WhatId = emp.Id;    
		testEvent.id=null;
		testEventsToInsert.add(testEvent);                                              // Add it to a list for later insertion.
	}
    insert testEventsToInsert;                                                    // Insert all the new events related to test records.
    
}

 
Shriya BhalchandranShriya Bhalchandran
hi Ahmd, thank you for your help.

Can you ell me whhere will i include (Event field)status is Closed in this trigger..that should only create record in employee object when status is closed
 
Ahmad J. KoubeissyAhmad J. Koubeissy
So if you have a field called status__c, you can include it like this way:
trigger records on Event (before insert) {

Map<Id, Event> eventsByAccount = new Map<Id, Event>();   // Map all the events to their account.
    for(Event ev : (Event[]) Trigger.new) {
      if(ev.status__c=='Closed'){        
           eventsByAccount.put(ev.WhatId, ev);
       }
  }

	Map<Id,Employee__c> EmpsById = new Map<Id,Employee__c>([SELECT Id,EmpAcc__c FROM Employee__c WHERE EmpAcc__c IN: eventsByAccount.keyset()]);  // Bring all the employee records related to your accounts.

	List<Event> testEventsToInsert = new List<Event>();
	for(Employee__c emp : EmpsById.values()) {                           // Get the event of the test's account.
		Event testEvent = eventsByAccount.get(emp.EmpAcc__c);
		testEvent.WhatId = emp.Id;    
		testEvent.id=null;
		testEventsToInsert.add(testEvent);                                              // Add it to a list for later insertion.
	}
    insert testEventsToInsert;                                                    // Insert all the new events related to test records.
    
}

Dont' forget to mark it as best answer
Shriya BhalchandranShriya Bhalchandran
Hi ahmed, dont know why but its not working. 
Ahmad J. KoubeissyAhmad J. Koubeissy
can you attach the error or the code that you wrote?
Shriya BhalchandranShriya Bhalchandran
No error, but its not giving expected result.. on creating event it should create same record in employee object..but it s not creating.
 
Ahmad J. KoubeissyAhmad J. Koubeissy
trigger records on Event (after insert) {

Map<Id, Event> eventsByAccount = new Map<Id, Event>();   // Map all the events to their account.
    for(Event ev : Trigger.new) {
       if(ev.status__c=='Closed'){        
           eventsByAccount.put(ev.WhatId, ev);
       }
  }

    Map<Id,employee__c> EmpsById = new Map<Id,opportunity>([SELECT Id,empAcc__c FROM employee__c WHERE empAcc__c  IN: eventsByAccount.keyset()]);  // Bring all the employee records related to your accounts.
    List<Event> testEventsToInsert = new List<Event>();
    for(employee__c emp : EmpsById.values()) {                           // Get the event of the test's account.
        Event testEvent = eventsByAccount.get(emp.empAcc__c ).clone(false, true, false, false);
        testEvent.whatid= emp.id;
        testEventsToInsert.add(testEvent);                                              // Add it to a list for later insertion.
    }
    insert testEventsToInsert;                                                    // Insert all the new events related to test records.
    
}

this code should work, but i dont know exactly how u define a closed event. im defining it based on status__c field. so adjust it
Shriya BhalchandranShriya Bhalchandran
Now its showing an error.
 
Ahmad J. KoubeissyAhmad J. Koubeissy
Replace the opportunity with employee__c. Sorry my bad
Shriya BhalchandranShriya Bhalchandran

Yes i changed, but its throwng error now. 

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger CreateRecordsonEmployee caused an unexpected exception, contact your administrator: CreateRecordsonEmployee: execution of AfterInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Related To ID: id value of incorrect type: a166F0000058w43QAA: [WhatId]: Trigger.CreateRecordsonEmployee: line 17, column 1

Ahmad J. KoubeissyAhmad J. Koubeissy
You should enable the activities on employee object.
Ahmad J. KoubeissyAhmad J. Koubeissy
kindly mark my previous answer as best answer if this is solved
Shriya BhalchandranShriya Bhalchandran
Hi Ahmad, it dint work for me yet :(
 
Ahmad J. KoubeissyAhmad J. Koubeissy
go to employee__c object and click on edit, scroll down to Optional Features section and enable Allow ActivitiesUser-added image
Shriya BhalchandranShriya Bhalchandran
i did it too.. but still no expected output.
Ahmad J. KoubeissyAhmad J. Koubeissy
What is the error message ?
Are you sure that you're adding a closed event?
and are you sure there is a related employee for the account where you are creating the event ?
Shriya BhalchandranShriya Bhalchandran
yes please look at this. i created employee object then created lookup field to account.  i took one accoubt record, then in the related list of Account> i willl get new event, i created new event...that even dosent display in employee object. 
Ahmad J. KoubeissyAhmad J. Koubeissy
trigger records on Event (after insert) {

Map<Id, Event> eventsByAccount = new Map<Id, Event>();   // Map all the events to their account.
    for(Event ev : Trigger.new) {
       if(ev.status__c=='Closed'){        
           eventsByAccount.put(ev.WhatId, ev);
       }
     }
      system.debug('eventsByAccount '+eventsByAccount);

    Map<Id,employee__c> EmpsById = new Map<Id,employee__c>([SELECT Id,empAcc__c FROM employee__c WHERE empAcc__c  IN: eventsByAccount.keyset()]);  // Bring all the employee records related to your accounts.
system.debug('EmpsById '+EmpsById );    
List<Event> testEventsToInsert = new List<Event>();
    for(employee__c emp : EmpsById.values()) {                           // Get the event of the test's account.
        Event testEvent = eventsByAccount.get(emp.empAcc__c ).clone(false, true, false, false);
        testEvent.whatid= emp.id;
        testEventsToInsert.add(testEvent);                                              // Add it to a list for later insertion.
    }
    insert testEventsToInsert;                                                    // Insert all the new events related to test records.
    
}
can you copy this code. i added some debugs. one you try it send me the debugs please
 
Ahmad J. KoubeissyAhmad J. Koubeissy
As you can see in the below image, you are inserting a event with Open status, so its not taken into consideration. you should add it as Closed status.
User-added image
Ahmad J. KoubeissyAhmad J. Koubeissy
im sorry but this debug is not clear, maybe u didnt choose the right one