You need to sign in to do that
Don't have an account?
Evelyn Fayad
'Last Activity Assigned To' custom field on the lead object using an apex trigger
Hi everyone,
I'm trying to create a custom field that populates with the name of whomever had the last activity on the lead. I'm currently trying to do this using a trigger.
This is the Apex Trigger that I have written:
trigger lastactassigned on task(after insert) {
map<id,lead> leads = new map<id,lead>();
for(task record:trigger.new) {
if(record.ownerid.getsobjecttype()==user.sobjecttype) {
if(record.whoid!=null&&record.whoid.getsobjecttype()==lead.sobjecttype) {
leads.put(record.whoid,new lead(id=record.whoid,Last_Act_Assigned_To_Test__c=record.ownerid));
}
}
}
update leads.values();
}
AND this is the Apex Class that I have written to test the trigger:
@isTest
public class lastactassignedTest1
{
static testMethod void testMethod1()
{
User user = new User();
Lead lead = new Lead();
lead.LastName ='Test';
lead.FirstName ='New';
// Add all required field
insert lead;
Task task = new Task();
task.WhoId = user.Id;
task.Subject = 'Other';
task.Status = 'Not Started';
task.Description = 'New Work';
insert task;
}
}
So as of right now, both of them save without any problems; however, when I hit 'run test' on the Apex Class, it fails.
I'm thinking that there may be something wrong with the trigger, but I am not completely sure. I would love some input!
Thank you,
Evelyn
I'm trying to create a custom field that populates with the name of whomever had the last activity on the lead. I'm currently trying to do this using a trigger.
This is the Apex Trigger that I have written:
trigger lastactassigned on task(after insert) {
map<id,lead> leads = new map<id,lead>();
for(task record:trigger.new) {
if(record.ownerid.getsobjecttype()==user.sobjecttype) {
if(record.whoid!=null&&record.whoid.getsobjecttype()==lead.sobjecttype) {
leads.put(record.whoid,new lead(id=record.whoid,Last_Act_Assigned_To_Test__c=record.ownerid));
}
}
}
update leads.values();
}
AND this is the Apex Class that I have written to test the trigger:
@isTest
public class lastactassignedTest1
{
static testMethod void testMethod1()
{
User user = new User();
Lead lead = new Lead();
lead.LastName ='Test';
lead.FirstName ='New';
// Add all required field
insert lead;
Task task = new Task();
task.WhoId = user.Id;
task.Subject = 'Other';
task.Status = 'Not Started';
task.Description = 'New Work';
insert task;
}
}
So as of right now, both of them save without any problems; however, when I hit 'run test' on the Apex Class, it fails.
I'm thinking that there may be something wrong with the trigger, but I am not completely sure. I would love some input!
Thank you,
Evelyn
There are couple of things that doesnt seem correct.
It checks if the ownerId of the task is of type user, then it verifies if the whoid of task is Lead. However in your test class you are assigning the tasks whoid to user record instead of task. As per the trigger the test class should be
Hope this helps.
All Answers
There are couple of things that doesnt seem correct.
It checks if the ownerId of the task is of type user, then it verifies if the whoid of task is Lead. However in your test class you are assigning the tasks whoid to user record instead of task. As per the trigger the test class should be
Hope this helps.
Here is the new class test I have written:
@isTest
public class lastactassigned
{
public static User insertStandardUser(Id standardProfileId){
User standardUser = new User(
Username ='standarduser@testorg.com',
Alias = 'standt',
email = 'standarduser@testorg.com',
emailencodingkey = 'UTF-8',
LanguageLocaleKey = 'English',
LocaleSidKey = 'en_US',
Profile = [SELECT Id FROM Profile WHERE Name='Standard User'],
LastName = 'Testing',
TimeZoneSidKey = 'America/Los_Angeles'
);
insert standardUser;
Lead lead = new Lead();
lead.LastName = 'Test';
lead.FirstName = 'New';
insert lead;
Task task = new Task();
task.WhoId = lead.Id;
task.Subject = 'Other';
task.Status = 'Completed';
task.Description = 'New Work';
task.ownerid = standardUser.id;
insert task;
}
}
Im getting a compile error that reads Error: Compile Error: Non-void method might not return a value or might have statement after a return statement. at line 32 column 9. (which is the 'insert task;' line)
Any ideas on why I'm getting this error?
I really appreciate your help.
Evelyn
The issue is with the test method signature. Please replace with
You might also want to change the username 'standarduser@testorg.com' to something more unqiue.
Thank you so much for all of your help. I was able to package and deploy it. Works wonderfully.