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
krish@123krish@123 

Please anybody suggest me ..how to write trigger when contact created automaticaly event also create

HI

How to write trigger when contact created from visualforce page automaticaly event also create( on standard contact detail)
Please share a code

Thanks
Best Answer chosen by krish@123
SarfarajSarfaraj
With this requirement it is not possible to do using workflow rule. Use this trigger,
trigger CreateEventFromContact on Contact (after insert) {
    List<Event> eventList = new List<Event>();
    for (Contact c : Trigger.new) {
        Event event = new Event(
            OwnerId = c.CreatedById,
            WhoId = c.Id,
            ActivityDateTime = c.StartDate__c,
            StartDateTime = c.StartDate__c,
            EndDateTime = c.EndDate__c,
            Subject = 'To Do',
            Location = c.Place__c,
            Type = c.Event_Type__c
            );
        eventList.add(event);
    }
    insert eventList;
}
--Akram

All Answers

SarfarajSarfaraj
You can write a trigger to achieve this requirement. However I would suggest to do this using a workflow.
Write the workflow on contact object with trigger condition, every time it is created. Add one action under it to create a task against the associated contact.
krish@123krish@123
Hi akram,

  Thanks for reply 

But i have custom fields on contact object as same as event fields and 

I need to map the firelds  and send email 

Please help me out....
SarfarajSarfaraj
Can you please elaborate about these custom fields, associated data types and the mapping?
krish@123krish@123
These fields
contact custom fields                                                         event fields

Event_Type__c      it is Picklist                                                Type
startDate__c(only date field)                                                    startdate 
EndDate__C      (only date field)                                               Enddate 
Place__C      ( only text field)                                                   location

Please check  the above fields these fields i need to map 

Thanks

 
SarfarajSarfaraj
With this requirement it is not possible to do using workflow rule. Use this trigger,
trigger CreateEventFromContact on Contact (after insert) {
    List<Event> eventList = new List<Event>();
    for (Contact c : Trigger.new) {
        Event event = new Event(
            OwnerId = c.CreatedById,
            WhoId = c.Id,
            ActivityDateTime = c.StartDate__c,
            StartDateTime = c.StartDate__c,
            EndDateTime = c.EndDate__c,
            Subject = 'To Do',
            Location = c.Place__c,
            Type = c.Event_Type__c
            );
        eventList.add(event);
    }
    insert eventList;
}
--Akram
This was selected as the best answer
krish@123krish@123
Thanks for sharing
I got it.

And One more requirement .could you help me



 
krish@123krish@123
I have meeting request button  from event object.I need to send meeting invities to users

Who Accepts mail first for that user need to assign contact .if  user did not respond 10min .that mail should be expire and throw him a message that "Now u cannot accecpt the mail it is expired

Please help me 
.
SarfarajSarfaraj
Can you please elaborate the functionality of the meeting request button and how it works.
krish@123krish@123
HI akram
 i have Standard contact detail page .and  the  realed list is  Event object i have Meeting request button on Event it is standard button 
I am sending  email invitation to the users and contacts . they getting  below formate url the specified emails for there out look. i that  we need to accept o decline optiona are presents. When use accepts the  the emails automatically the users assigned to that contacts

For suppose when user  hasn't response wtih in 10 min the below url shouls expired .or thow a error 

I have checked that we have event realtion object i found the fields status fields this fields hold the accept ,decline  picklist values.



View Response and Schedule Meeting






Or click https://terms-dev-ed.my.salesforce.com/0039000001AD6Ja?sched=%2Fscheduling%2FeditMeeting.apexp%3Fid%3D00U9000000nvhOk
Do not reply to this email.

Please help me for writing trigger or VF page

If you would help me  Iam very much appreciated!!