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
Lakshmi SLakshmi S 

How can we attach the event to a new opportunity?

Hi Team,

How can we attach the event to a new opportunity?

Regards
Lakshmi
GauravendraGauravendra
Hi Lakshmi,

Create the opportunity first, then use that opportunity Id to associate the new event with opportunity.
        Date clsDate = Date.newInstance(2018, 07, 07);
        Datetime start = Datetime.newInstance(2018, 10, 1, 12, 30, 0);
        Datetime endtime = Datetime.newInstance(2018, 10, 7, 12, 00, 0);
        Opportunity opp = new Opportunity(Name='Apex Opp',CloseDate=clsDate,StageName='Qualification');
        insert opp;
        Event evt = new Event();
        evt.Subject='Event Subject';
        evt.OwnerId=UserInfo.getUserId();
        evt.WhatId=opp.Id;
        evt.Description='Event Desciption';
        evt.StartDateTime=start;
        evt.EndDateTime = endtime;
        insert evt;

Hope this helps.
Lakshmi SLakshmi S
Hi Guravendra,

Thanks for your reply.
My question is can we create event with out any related object?
 
Sarvesh_OjhaSarvesh_Ojha
Dear Lakshmi,

Yes, we can create event without any related object. 
The only mandatory fields are Assigned To (Owner of the Event), Start Date/Time and End Date/Time.

Thanks,
Sarvesh
GauravendraGauravendra
Hi Lakshmi,

We can create event without any related object.
You can make sure of it by querying the event object.
Create any event by any values. Then query it from query editor.
 
Event evt = new Event();
        evt.Subject='Event Subject';
        evt.OwnerId=UserInfo.getUserId();
        evt.Description='Event Desciption';
        evt.StartDateTime=start;
        evt.EndDateTime = endtime;
        insert evt;
 
Select Id,Subject from Event

Hope this helps.
Lakshmi SLakshmi S
Hi Sarvesh,

Thanks for your reply.
I have one more question, How can we attach existing event to a new opportunity?
Lakshmi SLakshmi S
Hi Guravendra,

Thanks for your reply.
Can we attach existing event to new opportunity?
Scenario : From event layout page we have created a button called 'New Opporunity'. If we create new opportunity from that page the event automatically attach to that opportunity?
How can we achieve that?