You need to sign in to do that
Don't have an account?
Mike DeMille
trigger help!
I'm trying to write a trigger on task that when a new task is created, two fields on the opportunity are updated (opportunity 'Last Sales Activity' = task.createdDate, and opportunity 'Last Sales Activity Type = task.Type
How can I point it to the opportunity fields? Do I have to query for parent data first? This is what I have:
trigger updateOpportunityActivityFieldsFromTask on Task (before insert) {
for(task t:trigger.new) {
if(t.whatid != null && t.whatid.getsobjecttype() == opportunity.sobjecttype) {
t.WhatId.Last_Sales_Activity__c=t.CreatedDate;
t.whatid.Last_Sales_Activity_Type__c=t.Type;
}
}
}
How can I point it to the opportunity fields? Do I have to query for parent data first? This is what I have:
trigger updateOpportunityActivityFieldsFromTask on Task (before insert) {
for(task t:trigger.new) {
if(t.whatid != null && t.whatid.getsobjecttype() == opportunity.sobjecttype) {
t.WhatId.Last_Sales_Activity__c=t.CreatedDate;
t.whatid.Last_Sales_Activity_Type__c=t.Type;
}
}
}
All Answers
Thank you so much for the help! I'm very grateful.
I need to convert t.CreatedDate from datetime to date. My 'Last_Activity_Date__c field is a date field. Do you know the best way to do this?
I made a couple of slight tweaks and it accepts it. I will test and get back.
trigger updateOpportunityActivityFieldsFromTask on Task (before insert) {
Map<Id,Opportunity> opportunities = new Map<Id,Opportunity>();
for(task t:trigger.new) {
if(t.whatid != null && t.whatid.getsobjecttype() == opportunity.sobjecttype) {
opportunities.put(t.WhatId,
new Opportunity(
Id=t.whatId
,Last_Sales_Activity__c = t.CreatedDate.date()
,Last_Sales_Activity_Type__c=t.Type));
}
}
update opportunities.values();
}
Here is my trigger
trigger updateOpportunityActivityFieldsFromTask on Task (after insert) {
Map<Id,Opportunity> opportunities = new Map<Id,Opportunity>();
for(task t:trigger.new) {
if(t.Owner.UserRole.Name == 'ADM' && t.whatid != null && t.whatid.getsobjecttype() == opportunity.sobjecttype) {
opportunities.put(t.WhatId,
new Opportunity(
Id=t.whatId
,Last_Sales_Activity__c = t.CreatedDate.date()
,Last_Sales_Activity_Type__c=t.Type));
}
}
update opportunities.values();
}
And here is my test class:
@isTest
private class updateOpportunityActivityTest {
@isTest static void updateOpportunityActivityFieldsFromTask() {
User Polson;
for(User usr : [Select Id
From User
WHERE LastName = 'Polson'
limit 1]){
Polson = usr;
}
System.runAs(Polson){
Account acc = new Account();
acc.Name = 'Tester 18';
insert acc;
Opportunity opp = new Opportunity();
opp.Name = 'Test Oppty';
opp.Amount = 5;
opp.CloseDate = system.now().date();
opp.AccountId = acc.Id;
opp.StageName = 'Stage 0: Scheduled';
insert opp;
Task tsk = new Task();
tsk.WhatId = opp.Id;
tsk.Subject = 'Test 18';
tsk.Type = 'Call';
insert tsk;
}
}
}
I'm only getting 44% coverage, but if I take out the t.Owner.UserRole.Name == 'ADM' part then it gets 100%.
Can you help me reword this to work?
I just saw this, apologize for the late-ish response.
In your unit test, you need to create a user with a UserRole set to "ADM", and use the System.runAs method to run your unit test as the user creating the task record.
I also added in some "best practices" of unit testing in Apex, such as using Test.startTest / Test.stopTest and assertions for your unit test. The code above is untested, so you might encounter compiler and other errors, but it should set you down the right path of getting your unit test working.
Cheers,
- James
Thanks so much for the help. I keep getting the following error:
Error: Compile Error: Illegal assignment from List<UserRole> to Id at line 6 column 14
Nevermind. I got it figured out. I had to change the trigger to reference formula fields on TASK that referenced the t.owner.role.name
THANKS