+ Start a Discussion
AribaGalaxyAribaGalaxy 

Writing TEST for Trigger

Last week I worked on my first trigger - I got some help here on finishing it but now I am lost on how to test it. So far all I have done is created two accounts with which I think I need to create a partnership and then a task to test, but I am not sure how to go about this. Most of what I have is based on "how-tos" and the structure of other triggers; I'm not a developer by trade but I am trying to learn. I'm not looking for the work to be done (though I wouldn't turn it down) as much as I am looking for a starting point...

 

Any help is greatly appreciated.

Trigger:

trigger JWGetAssociations on Task (after insert, after update) {
if(FutureTriggerController.isFutureUpdate != true){

FutureTriggerController.isFutureUpdate =true;
Map<Id, Id> TaskToAccountMap = new Map<Id, Id>();
Map<Id, Id> AcctToPartnerMap = new Map<Id, Id>();
Map<Id, Account> PartnerResponseMap = new Map<Id, Account>();
List<Task> tListToBeUpdated = new List<Task>();
for(Task jwtask : [SELECT Id, AccountID FROM Task WHERE ID in: trigger.new]){
TaskToAccountMap.put(jwtask.id, jwtask.AccountId);
}

list <Id> partner1 = new list <Id>();
for(Partner partner : [SELECT Id, AccountToId, AccountFromId FROM Partner WHERE AccountFromId in: TaskToAccountMap.values()]){

AcctToPartnerMap.put(partner.AccountToId , partner.AccountToId);
}
for(Account part : [SELECT Id, Name FROM Account WHERE ID in: AcctToPartnerMap.values()]){
PartnerResponseMap.put(part.ID, part);
}

for(Task o : trigger.new){
Id acctid = TaskToAccountMap.get(o.Id);
list <String> partnerAcc = new list <String>();
for( id Accid : AcctToPartnerMap.keyset()){
Account acc = PartnerResponseMap.get(Accid);
partnerAcc.add(acc.Name+'\n');
partnerAcc.add('\n\n');
}
for(integer i=0;i<partnerAcc.size();i++){
}
Task t = new task(Id = o.Id);
if(t.jw_Associated_Distributors__c == null){
t.jw_Associated_Distributors__c = '';
for(integer i=0;i< partnerAcc.size();i++){
t.jw_Associated_Distributors__c += partnerAcc[i];
}
}
tListToBeUpdated.add(t);
}
if(tListToBeUpdated.size()>0){
upsert tListToBeUpdated;
}
}
}

 FutureTriggerController class code: 

public class FutureTriggerController{
public static boolean isFutureUpdate = false;
}

 

Thus far; my test class looks like this. I know it's far from done and I am not sure if I am even starting off right.

@isTest  
private class testClass_Trigger{  
      private static TestMethod void JWGetAssociations(){  
         Account jwacct1 = new Account(name='TestAccount1');
            insert jwacct1;
         Account jwacct2 = new Account(name='TestAccount2');
            insert jwacct2;
    }  
  
  
}

 

Best Answer chosen by Admin (Salesforce Developers) 
souvik9086souvik9086

Write this and let me know how much it is covering.

 

@isTest
private class testClass_Trigger{
private static TestMethod void JWGetAssociations(){
Account jwacct1 = new Account(name='TestAccount1');
insert jwacct1;
Account jwacct2 = new Account(name='TestAccount2');
insert jwacct2;
Task t = new Task();
t.Subject = 'Email';
t.WhatId = jwacct1.Id;
t.Status = 'Not started';
t.Priority = 'Normal';
insert t;
}


}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

All Answers

souvik9086souvik9086

Write this and let me know how much it is covering.

 

@isTest
private class testClass_Trigger{
private static TestMethod void JWGetAssociations(){
Account jwacct1 = new Account(name='TestAccount1');
insert jwacct1;
Account jwacct2 = new Account(name='TestAccount2');
insert jwacct2;
Task t = new Task();
t.Subject = 'Email';
t.WhatId = jwacct1.Id;
t.Status = 'Not started';
t.Priority = 'Normal';
insert t;
}


}

 

If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.

Thanks

This was selected as the best answer
AribaGalaxyAribaGalaxy

Covered 79%


If I am not mistaken, that's good enough, correct? If so, I have to say that looks a lot simpler/smaller than I thought it would have had to been.

 

Thank you souvik!

souvik9086souvik9086

Yes, minimum code coverage you need is 75%. So you are good in this.

 

If the posts helps you please dont forget to throw KUDOS by clicking on the STAR ICON in the left to help other developers get benefitted by this.

 

Thanks

AribaGalaxyAribaGalaxy
Thank you again Souvik. One thing you've just taught me is while a solution may seem to simple to bother trying; try it anyway!