You need to sign in to do that
Don't have an account?

Can you guys help me to write a test class
This is my code to simpy update a field on custom object from the task. I have never written a test class so please help me out to compose it.
trigger SandboxCC on Task (before update,after update) {
if(Trigger.isUpdate){
for(Task t : Trigger.new)
{
if(t.IsClosed == True && t.subject.contains('App Preview Sent to Customer'))
{
for(CC_Card__c c : [Select c.Id, c.stage__c From CC_Card__c c ])
{
c.stage__c = 'Preview & Iterate';
c.a_Preview_Date__c = system.today();
update c;
}
}
}
}
}
Dude, your code is better now but still not fully bulkified... For example, if the trigger fires on more than one task record(more tasks, like in the case of a dataloader operation) you are still doing the select for Cards and the update cardslist for each task. You need to move that for outside of the if:
Also remove one of the time conditions on the trigger, leave just one, either before or after, no need for both...
And yes, the test class. I forgotten about that, sorry :P
You run it (from eclipse) right clicking on the file in the package explorer > Force.com > Run Tests.
Thanks,
Adi
All Answers
Why are you calling it on before as well as after?
I don't see any difference in the logic. So you are actually executing this twice without any need.
Secondly, you are querying inside a FOR loop. So your trigger isn't bulkified.
On every update of Task, do you have to update all the CC_Card__c records in the database? Any relation between Task and CC_Card__c?
yes you are right. For every task, whole database is getting updated, how can I correct that? thanks a lot for your help
Read the comments and adapt the code to your situation. Check this:
Tkxs,
Adrian
Thanks Man, I was also working for similar sort of code, here is my bit.
trigger CCCards on Task (before update,after update) {
//Preparting CCCards to Update
public list<CC_Card__c> CardsToUpdate = new List<CC_Card__c>();
public set<Id> CCCardIDs = new Set<Id>();
if(Trigger.isUpdate){
for(Task t : Trigger.new)
{
if(t.IsClosed == True && t.subject.contains('App Preview Sent to Customer'))
{
CCCardIDs.add(t.WhatId);
for(CC_Card__c c : [Select c.Id, c.stage__c From CC_Card__c c where Id in :CCCardIDs])
{
c.stage__c = 'Preview & Iterate';
c.a_Preview_Date__c = system.today();
CardsToUpdate.add(c);
}
update CardsToUpdate;
}
}
}
But I am struggling to write a test class, can you help me in that..thanks a ton.
Dude, your code is better now but still not fully bulkified... For example, if the trigger fires on more than one task record(more tasks, like in the case of a dataloader operation) you are still doing the select for Cards and the update cardslist for each task. You need to move that for outside of the if:
Also remove one of the time conditions on the trigger, leave just one, either before or after, no need for both...
And yes, the test class. I forgotten about that, sorry :P
You run it (from eclipse) right clicking on the file in the package explorer > Force.com > Run Tests.
Thanks,
Adi
Thanks boss, just a question. I have a required field at CC Card object and that is primary contact. This is a lookup field to contacts. How do I incorporate this in test class. I mean what should I add in here.
CC_Card__c card = new CC_Card__c(Name='test', Primary_Contact__c=??)
For this, create a test contact record in your test method.
Contact c = new Contact (LastName = 'test'); // add any other fields, if required.
insert c;
CC_Card__c card = new CC_Card__c(Name='test', Primary_Contact__c=c.Id);
This should do the job for you!
that is awesome, I have a question just for my knowledge, here I had only one task "App Preview Sent to Customer". But what if there are multiple tasks and I need to update stage's status according to that? How would I make that bulkified.