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
thekid12345thekid12345 

How do I go about writing an APEX class to create an opportunity and assign a follow up task unless the opportunity is already created?

Need some insight on ways to approach this
SandhyaSandhya (Salesforce Developers) 
Hi,

Below is the sample code for creating tasks on the opportunity you can 
​modify according to your requirements.
trigger ClosedOpportunityTrigger on Opportunity (after insert,after update) {
List<task> carry=New List<task>();

  for(opportunity opp:trigger.new){
   if(opp.stagename=='Closed Own'){
    task t=new task(
        whatid=opp.id, 
        Status = 'Active',
        Subject = 'Follow Up Test Task',
        ActivityDate = system.today()
     );
    carry.add(t); 
    }
   }
     insert carry;

 }

Hope this helps you!

If this helps you, please mark it as solved.

Thanks and Regards
Sandhya