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
MS123456MS123456 

How to write trigger this use case pls any on can help me its very Urgent.......

Hi Team Pls Help me....

Use Case:update below fields from the Most Recent Event corresponding to a contact with type="Call" to the Contact objects in Salesforce 

Below are the fields which we are going to automate :
1.IsPrivate
2.StartDateTime     
3.Subject

Only Using Trigger......
Glyn Anderson 3Glyn Anderson 3
I'm not sure you have given enough information to solve your problem, but here's how you can get the most recent "Call" for each Contact in a trigger:

<pre>
trigger MostRecentCall on Contact ( before update )
{
    Map<Id,Contact> contactsWithCalls = new Map<Id,Contact>
    (   [   SELECT  Id, 
            (   SELECT  Id, CallType    // add other fields here
                FROM    Tasks
                WHERE   Type = 'Call'
                ORDER BY CreatedDate DESC
                LIMIT 1
            )
            FROM    Contact
            WHERE   Id IN :Trigger.new
        ]
    );

    for ( Contact contact : Trigger.new )
    {
        Contact contactWithCall = contactsWithCalls.get( contact.Id );
        if ( contactWithCall.Tasks.isEmpty() ) continue;
        Task mostRecentCall = contactWithCall.Tasks[ 0 ];
        contact.Most_Recent_Call_Type__c = mostRecentCall.CallType;
    }
}
</pre>

Disclaimer:  This code is untested and might contain typos.

Note also, that a Contact record will only be up-to-date immediately after it has been updated.  If a "Call" Task is added to the Contact, but the Contact record itself is not updated, the Contact will not reflect the value(s) from the newer Task record.  You can create a Task trigger (after insert, after update, after delete, after undelete) that updates the related Contact record(s).
MS123456MS123456
Hi Glyn Anderson 3

thnx for the reply...

An event is a child object of contact Suppose one contact on  Number of Events and the type is ='Call'  and output will be the top most event will be updated? 
Saket Sharma 24Saket Sharma 24
Hi Mayur,
Please Check this trigger,
 
trigger TriggerForMostRecentEvent on Contact (after insert) {
    Set<Id> ids=new Set<Id>();
    for(Contact c: trigger.new)
        ids.add(c.Id);
    List<Event> toUpdate=new List<Event>();
    for(Contact c:[select name,id,(select isPrivate,StartDateTime,Subject from events where type='Call' Order By CreatedDate desc) from contact where id in :ids]){
        if(c.Events!=null)
        {
            Event ev=c.Events[0];
            ev.IsPrivate=false;
            ev.StartDateTime=DateTime.now();
            ev.Subject='Email';
            toUpdate.add(ev);
        }
    }
    if(toUpdate.size()>0)
        update toUpdate;
}

If this code helped you then please mark this as best answer.
Glyn Anderson 3Glyn Anderson 3
Did any of these answers solve your problem?  If so, please mark the question as "Solved".  If not, let us know.  If you solved it yourself another way, please post your solution.  Thanks!