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
nagarjuna gurajanagarjuna guraja 

Need Apex Trigger

Hello,
Write a trigger such that To create a task on Opportunity,whenever Account Owner is Changed

 
PriyaPriya (Salesforce Developers) 
Hey Nagarjuna,

Kindly refer these examples and try to develop the trigger for your requirement :-

1. https://developer.salesforce.com/forums/?id=9060G000000Xf8WQAS

2. https://salesforce.stackexchange.com/questions/335692/trigger-on-account-owner-change

If it does not work, let me know i will help you with the code.

Thanks,
Priya Ranjan
nagarjuna gurajanagarjuna guraja
Hi, It does not helps me.Could you please provide me the code. Write a trigger such that to create a task on Opportunity whenever Account owner is changed. Best Regards
PriyaPriya (Salesforce Developers) 
Hey Nagarjuna,

I apologise for the delay response. Here is the code :-
trigger taskonoppownchangedaccount on Account (after update) {
    
    List<Opportunity> Opr = new List<Opportunity>();
    List<Task> createTask = new List<Task>();
    
   
    for(Account acc1 : Trigger.New)
    {
        if (acc1.OwnerId != Trigger.oldmap.get(acc1.id).OwnerId)
        {
            Opr = [select id from Opportunity where AccountId =: acc1.id];
            
            if (Opr != null)
   			 {
    		    system.debug('Opportunity is associated with Account');
                 // creating task
                 // 
                 for(Opportunity oppr : Opr)
                 {
                     Task newTask = new Task();
                     newTask.WhatId = Oppr.id;
                     newTask.Description = 'Account owner being change';
                     createTask.add(newTask);
                 }
   			 }
   			 else
   			 {
     		   system.debug('Opportunity is NOT associated with Account');
    		 }
        }
    }
 

Kindly mark it as the best answer if it works for you.

 

Thanks & Regards,

Priya Ranjan