You need to sign in to do that
Don't have an account?
Trigger and Test Class help
I've created my first Trigger (yay me) to automatically update a picklist field on a custom Policy object to "Pending" when a task has been created with something selected from the custom "Type_of_Action" field. I've also written a test class; however I'm getting an error: "System.AssertException: Assertion Failed: Expected: Pending, Actual: Null"
Below is the Trigger and Test Class - any help would be greatly appreciated.
trigger policyStatusUpdate onTask (beforeinsert, beforeupdate) {
String desiredNewPolStatus = 'Pending';
List<Id> polIds=new List<Id>();
for(Task t:trigger.new) {
if (t.IsClosed==FALSE){
if(String.valueOf(t.whatId).startsWith('a06d') && (t.Type_of_Action__c != null)==TRUE) {//check if the task is associated with a policy
polIds.add(t.whatId);
}//if 2
}//if 1
}//for
List<Policy__c> polToUpdate=[SELECT Id, Status__c FROM Policy__c WHERE Id IN :polIds];
For (Policy__c l:polToUpdate){
l.Status__c=desiredNewPolStatus;
}//for
try{
update polToUpdate;
}catch(DMLException e){
system.debug('Policies were not all properly updated. Error: '+e);
}
}
TEST CLASS:
@isTest
private class policyStatusUpdate {
static testMethod void myUpdateTest() {
//CREATE A POLICY RECORD
Policy__c aDS = new Policy__c(name='TestPolicy1', Policy_Holder_Name__c='001d000000AoHIr');
insert aDS;
//CREATE A TASK ASSIGNED TO THE POLICY
Task t1 = new Task(subject='testupdate 1', WhatId = aDS.id, description = 'test description', Type_of_Action__c='Medicals');
insert t1;
//RUN AN ASSERT CALL TO MAKE SURE THAT THE CUSTOM FIELD ON THE POLICY HAS THE VALUE OF PENDING
Policy__c assertToTest = [Select Status__c from Policy__c where id = :aDS.id];
system.AssertEquals('Pending', assertToTest.Status__c);
}
}
Hi David,
Can you check the following statement-
if(String.valueOf(t.whatId).startsWith('a06') && (t.Type_of_Action__c != null)==TRUE) {//check if the task is associated with a policy
You should always compare with keyprefix which is 3 digit only.
Also, I would recon if you need Describe method to compare instead of doing it hard coded. Check Schema describe method here
Most likely this condition is not met (String.valueOf(t.whatId).startsWith('a06d') and status is not updated. Try to remove it and see if this helps. Also minor adjustments I would make - in your try and catch block don't catch it if you only output it in the debug log. If there is an exception, you will never find that out unless you are looking the debug. A nice way would be to use Database.update (list, false) and then go through the save result to see if there are any errors and add that error message to the task.
Hope this helps.
Sorry, answer got posted while I was typing :)
Ok - I've corrected the prefix to 3 characters.
for the system assert comparison can you point me to any examples of how to write a system assert using the describe method? I've found a few examples of queries using describe (and it's been very helpful to improve my understanding of describe) but I'm having trouble making the leap to a system assert comparison using the describe method.
Thanks,
You don't need to make Describe calls within assert methods. What I was telling is that, Instead of hard code 3 digit prefix i.e. "a06" you should use describe method to compare/validate.
For Example:
if((t.whatId != null && String.valueOf(t.WhatId).subString(0, 3) == Policy__c.sObjectType.getDescribe().getKeyPrefix()) .....) {
.
.
In this way, you don't need to hard code the key prefix as It changes when its new and deployed to Other org/Production.
Make sure you put prefix from getDescribe into a string variable outside of loop or it will hit the limit.
Agreed OlegForce !!!
:)
@ crmtech21
Ahhhh - cool. I get it. Thank you for the example - that helps me immensely.