+ Start a Discussion
Swapnil PatneSwapnil Patne 

How to write a trigger?

Hi,

This is what I want to achieve, can you help?

Aim-   To create a new Event record after a Task record is created against a contact.

We have a webiste where contacts register for physical events- once a contact registers it creates an  Task against the contact in Salesforce with following fields updated- (All std fields)

Subject: Registered for xx event
Status: Completed
Priority: Low
Comments: Paid xx amount

Now, once this record is created in SF I would like to have a trigger set up to create an Event record assigned to the contact owner and associated to the Contact/Lead.

Fields to update:

Std fields-

Assigned To
 
  
Subject
 
Related To
 
Start
  
Name 
End
 
Phone
 
Location
 
Email
 


Custom Fields-

Event Record Type
Event Registration
Attendee Type
Pick list
Event Speaker
Check box
Registered For Event
Pick list
Presentation Topic
Text
Event Year
Pick list
Replaced By
Text
Registration Date
Date
  
Event Registration Status
Pick list
  
Event Attended
Pick list
  


How do i write a  trigger for this?

Please help me..

Regards,
Swapnil

Ritesh AswaneyRitesh Aswaney

trigger TaskAfter on Task(After Insert){

 

String contactPrefix = Contact.sObjectType.getDescribe().getKeyPrefix();

List<Event> events = new List<Event>{};

 

for(Task t : trigger.new){

if(t.WhoId != null && t.WhoId.startsWith(contactPrefix) && t.status = 'Completed' && t.Subject.IndexOf('Registered' > 0)

events.add(new Event(StartDate = Date.today(), EndDate = Date.today(), RelatedTo = t.WhoId .......... // set all fields   ));

 

}

 

if(!events.isEmpty())

Database.insert(events);

 

}