You need to sign in to do that
Don't have an account?
Brittanie
How to write a trigger test on field update from contact to task
I wrote a trigger that works in sandbox but have to now write a trigger test (class) for it and although I am learning through doing, I am officially lost. please help
Trigger TaskBefore on Task(before insert, before update){ Map<Id, List<Task>> whoIdsMap = new Map<Id, List<Task>>(); for(Task t : trigger.new){ if(t.WhoId != null){ if(!whoIdsMap.containsKey(t.WhoId)){ List<Task> temp = new List<Task>(); temp.add(t); whoIdsMap.put(t.WhoId, temp); }else{ whoIdsMap.get(t.WhoId).add(t); } } } for(Contact con : [Select Id, Name,Title from Contact where Id in :whoIdsMap.keySet()]){ for(Task t :whoIdsMap.get(con.Id)){ t.Contacts_Title__c = con.Title; } } }
You have to do something like this
I just created skeleton code for you, you can use the same to create your test class
I'm not sure what you mean by required fields... the only required field on the contact is "Lastname." I am getting the following error:
Error: Compile Error: Comparison arguments must be compatible types: Schema.SObjectField, String at line 17 column
9
Please advise
System.assertEquals(Contact.Title,[SELECT Contacts_Title__c FROM Task WHERE
Id=:taskObj.Id].Contacts_Title__c);
Replace this with
System.assertEquals(Con.Title,[SELECT Contacts_Title__c FROM Task WHERE
Id=:taskObj.Id].Contacts_Title__c);
Well this line is just for checking if your trigger is working as expected.
You can comment this line as well to avoid error.
is current code now, but now recieving the following error: (@ insert taskObj;
Error: Compile Error: unexpected token: insert at line 19 column 8
new Task(
WhoId = con.Id);
Subject = 'CVM';
Status = 'In Progress';
Priority = 'High';
Owner = 'Name';}
This is not correct
it should be
new Task(
WhoId = con.Id,
Subject = 'CVM',
Status = 'In Progress',
Priority = 'High',
Owner = 'Name');
Another one Test.startTest();}
it should be Test.startTest();