You need to sign in to do that
Don't have an account?
uma52551.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!
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!
All Answers
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
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!
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.
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!