You need to sign in to do that
Don't have an account?
Shriya 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.
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.
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
Dont' forget to mark it as best answer
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
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
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 ?