You need to sign in to do that
Don't have an account?
Salesforce Admin
Trigger for Task- Opportunity not firing
Hello There,
I'm kinda new in apex triggers and was trying to write a trigger to copy a field from a task to an account/ opportunity. I tried out the below codes for opportunity but it doesn't seem to copy the task field to opportunity. Any suggestions please? Cheers.
trigger TaskAfterInsertUpdate on Task (after update, after insert)
{
list<opportunity> liOpportunities = new list<opportunity>();
list<id> liIDs = new list<id>();
for(Task sTask : trigger.new)
{
if(sTask.Subject.startsWith('Outbound call') || sTask.Subject.startsWith('Inbound call'))
{
liIDs.add(sTask.WhatId);
}
}
for(Opportunity sOppty : [select Id, Agent__c from Opportunity]) //where Id in : liIDS])
{
sOppty.Agent__c = 'Bla';
sOppty.Date = sTask.LastModifiedDate;
liOpportunities.add(sOppty);
}
update liOpportunities;
}
I'm kinda new in apex triggers and was trying to write a trigger to copy a field from a task to an account/ opportunity. I tried out the below codes for opportunity but it doesn't seem to copy the task field to opportunity. Any suggestions please? Cheers.
trigger TaskAfterInsertUpdate on Task (after update, after insert)
{
list<opportunity> liOpportunities = new list<opportunity>();
list<id> liIDs = new list<id>();
for(Task sTask : trigger.new)
{
if(sTask.Subject.startsWith('Outbound call') || sTask.Subject.startsWith('Inbound call'))
{
liIDs.add(sTask.WhatId);
}
}
for(Opportunity sOppty : [select Id, Agent__c from Opportunity]) //where Id in : liIDS])
{
sOppty.Agent__c = 'Bla';
sOppty.Date = sTask.LastModifiedDate;
liOpportunities.add(sOppty);
}
update liOpportunities;
}
NOTE: This code has not been tested and may contain typographical or logical errors
All Answers
NOTE: This code has not been tested and may contain typographical or logical errors
[Error] Error: Compile Error: Invalid field initializer: opps.Date at line 17 column 13
Also, the "bla" is supposed to be the AssignedTo field in Task.
Please any ideas
Thanks
Thanks for the updated code. The code seems to work but still gives this error:
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger TaskAfterInsertUpdate caused an unexpected exception, contact your administrator: TaskAfterInsertUpdate: execution of BeforeInsert caused by: System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: []: Trigger.TaskAfterInsertUpdate: line 24, column 1
This is my updated code. Please help. Cheers. Plus the date field does not recognize it.(//Date__c = t.LastModifiedDate.date())
That is assuming that Date__c is actually a type of DateTime
Thanks Pcon, I got it working by:
Thanks for your help.
:)
NOTE: This code has not been tested and may contain typographical or logical errors