+ Start a Discussion
uma52551.3972270309784705E12uma52551.3972270309784705E12 

Need Urgent Help to write Apex Trigger On Opportunity

Hi All,

I am trying to write a trigger that will get the subject of the Next Scheduled Activity and place it in a custom field under opportunity object called 'Next_Activity__c'. Can any one help me what was wrong in the below trigger.

trigger OpportunityActivityTrigger on Opportunity (after insert,after update) {
List <RecordType> rtList = [SELECT Id from RecordType where SobjectType='Opportunity' and Name='van_Wagenen' Limit 1];
list<Id> accIds = new list<Id>();           
 Map<Id,Task> TaskOppMap = new Map<Id,Task>();                   
      for(opportunity opp: trigger.new){
            if(opp.Id!=Null)
            accIds.add(opp.Id);
       }              
List<Task> taskList = [select Id, Subject, ActivityDate from Task where Id IN :accIds];                                               
        for(Task t: taskList){ 
                if(t.Id!=Null){
                TaskOppMap.put(t.Id, t);
                }
         }     
        for(opportunity o: trigger.new){
              
            if(o.recordTypeId==rtList[0].Id){
               Task ts = TaskOppMap.get(o.Id);
                o.Next_Activity__c=ts.Subject;                               
            }
         update o;            
        }
    
}


Thanks In advance!
Best Answer chosen by uma52551.3972270309784705E12
BalajiRanganathanBalajiRanganathan
Try the code below. Note : i have not compiled and tested this code.
 
trigger OpportunityActivityTrigger on Task(after insert, after update) {

List<Id> idSet = new List<Id>();

 for (Task att: Trigger.new) {
    if ((att.WhatId + '').startsWith('006')) {
      idSet.add(att.WhatId);
    }
 }

 List<Opportunity> updateList = new List<Opportunity> ();  
 for (opportunity o: [Select Next_Activity__c, (select id, subject from Tasks where isClosed = false order by ActivityDate DESC limit 1) from Opportunity where id in : idSet ]){  
     if (o.tasks.size() > 0 && o.Next_Activity__c != o.tasks[0].subject)  {          
       o.Next_Activity__c = o.tasks[0].subject;
       updateList.add(o);
     }                            
 }    
 update updateList; 
}

 

All Answers

BalajiRanganathanBalajiRanganathan
1) you can write the trigger on before insert and before update instead of after insert and after update
2) Id will not be null. so you dont need to check like if (opp.id!=null)
3) In the SOQL in class you can directly use Trigger.New
4) you have to use WhatId of task to associate with the Opportunity

try the code below
trigger OpportunityActivityTrigger on Opportunity (before insert,before update) {

  List <RecordType> rtList = [SELECT Id from RecordType where    SobjectType='Opportunity' and Name='van_Wagenen' Limit 1];
             
  List<Task> taskList = [select Id, Subject, ActivityDate,WhatId 
    from Task where WhatId IN :Trigger.new]; 
                                              
 for(Task t: taskList){ 
   TaskOppMap.put(t.WhatId , t);
 }     
 
 for(opportunity o: trigger.new){              
   if(o.recordTypeId==rtList[0].Id){
     Task ts = TaskOppMap.get(o.Id);
     o.Next_Activity__c=ts.Subject;                               
   }     
 }
}

 
BalajiRanganathanBalajiRanganathan
add  Map<Id,Task> TaskOppMap = new Map<Id,Task>();  after line 6
Suneel#8Suneel#8
You can achieve the same with below code on before insert.
Map<Id,Opportunity> mapOfOppties=new Map<Id,Opportunity>([select id,name,Approval_Status__c,(select id,subject from tasks order by createddate desc) from opportunity where id in :trigger.newmap.KeySet()]);
for(Opportunity o:trigger.new){
        if(mapOfOppties.get(o.id).tasks.size()>0){
			o.Next_Activity__c=mapOfOppties.get(o.id).tasks[0].subject;
        }    
}

 
uma52551.3972270309784705E12uma52551.3972270309784705E12
Hi BalajiRanganathan & Suneel,

Thank you very much for your help. I am having a small question. How to check whether the Opportunity is open/close any standard field for that? and I have to check that the Activity Date is equal to the Todays Date. Please correct the following SQL by adding the oppty open/close status and Activity Date is equal to today.

 List<Task> taskList = [select Id,RecordTypeId, Subject, ActivityDate,WhatId from Task where WhatId IN :Trigger.new and RecordTypeId ='012E000000022Zf' order by ActivityDate Desc]; 

If I am keeping ActivityDate = Today() it was throwing error. Please advise me.

Thanks!



 
BalajiRanganathanBalajiRanganathan
To check Activity Date is equal to today use ActivityDate = TODAY

Refer the date literals that can be used in SOQL
http://www.salesforce.com/us/developer/docs/officetoolkit/Content/sforce_api_calls_soql_select_dateformats.htm

you can use IsClosed field on Opportunity to know if the Opportunity is closed or not.
 
uma52551.3972270309784705E12uma52551.3972270309784705E12
Hi BalajiRanganathan,

Actually my problem not solved yet.. I have to write a trigger on Task Which will update task subject in the Opportunity Field(Next_Acitivity__c). It should be the first open activity based on due date. Can you please help I am really confused how to write this trigger..Thanks!
BalajiRanganathanBalajiRanganathan
Try the code below. Note : i have not compiled and tested this code.
 
trigger OpportunityActivityTrigger on Task(after insert, after update) {

List<Id> idSet = new List<Id>();

 for (Task att: Trigger.new) {
    if ((att.WhatId + '').startsWith('006')) {
      idSet.add(att.WhatId);
    }
 }

 List<Opportunity> updateList = new List<Opportunity> ();  
 for (opportunity o: [Select Next_Activity__c, (select id, subject from Tasks where isClosed = false order by ActivityDate DESC limit 1) from Opportunity where id in : idSet ]){  
     if (o.tasks.size() > 0 && o.Next_Activity__c != o.tasks[0].subject)  {          
       o.Next_Activity__c = o.tasks[0].subject;
       updateList.add(o);
     }                            
 }    
 update updateList; 
}

 
This was selected as the best answer
uma52551.3972270309784705E12uma52551.3972270309784705E12
Thank you BalajiRanganathan it is working Awesome help thank you so much..