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.

How can this be Achieved?

  • 1
    Write a trigger on event, add check for status and create record for your test object. – Rohit Mourya Jun 22 '17 at 8:15

You would have to do a trigger on Event, so for each insertion, given that its WhatId is an account, then execute some logic.

Your solution should look something like this:

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

// Bring all the test records related to your accounts.
Map<Id, Test__c> testsById = new Map<Id, Test__c>([SELECT Id, Account__c FROM Test__c WHERE Account__c IN: eventsByAccount.keys()]);
List<Event> testEventsToInsert = new List<Event>();
for(Test__c test : testsById.values()) {
    // Get the event of the test's account.
    Event testEvent = eventsByAccount.get(test.Account__c);

    // Associate it to your Test__c record.
    testEvent.WhatId = test.Id;

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

    // Add it to a list for later insertion.

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

Take into account that I have not compiled the code, so you might have to tune it for it to work. It is explained line by line. You would have to encapsulate this in a method, include it in a trigger and verify that the event comes from an account, etc.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.