You need to sign in to do that
Don't have an account?
SaiVineeth Maddula
Need help writing test class for the Trigger
Hi, I have written a trigger for the requirement which is after inserting a record in the Campaign Account(Custom Object lookup with Account) it should check the Campaign Account Name with opportunities related to the Account. I have wrote a test class for it but it was not running.
//Trigger public class CreateOppAfterCAInsertionHelper { public Boolean stopRecursive = TRUE; public void insertOpp(List<Campaign_Account__c> campaignAccList) { Map<Id,Map<String,List<Opportunity>>> existingOpportunities = new Map<Id,Map<String,List<Opportunity>>>(); List<Opportunity> oppsToUpdate = new List<Opportunity>(); Set<Id> accIds = new Set<Id>(); Set<String> campaignNames = new Set<String>(); for(Campaign_Account__c campAcc : campaignAccList) { if(campAcc.Account__c != NULL) { accIds.add(campAcc.Account__c); campaignNames.add(campAcc.Name); existingOpportunities.put(campAcc.Account__c,new Map<String,List<Opportunity>>()); } } Map<String,List<Opportunity>> oppMap = new Map<String,List<Opportunity>>(); for(Opportunity record : [SELECT Name, AccountId, Count__c FROM Opportunity WHERE AccountId IN: accIds and Name IN: campaignNames]) { if(oppMap.containsKey(record.Name)) { oppMap.get(record.Name).add(record); } else { oppMap.put(record.Name, new List<Opportunity>{record}); } existingOpportunities.put(record.AccountId, oppMap); } for(Campaign_Account__c campAcc : campaignAccList) { if(campAcc.Account__c == NULL) { continue; } for(Integer i=0; i<existingOpportunities.get(campAcc.Account__c).get(campAcc.Name).size(); i++) { Opportunity existingOpp = existingOpportunities.get(campAcc.Account__c).get(campAcc.Name)[i]; if(existingOpp.Name != campAcc.Name && existingOpp == NULL) { Opportunity newOpp = new Opportunity(Name=campAcc.Name, AccountId=campAcc.Account__c, StageName='Closed Won', CloseDate=date.today(), Count__c=0); insert newOpp; } else { if(existingOpp.Count__c == NULL) { existingOpp.Count__c = 1; } else { existingOpp.Count__c = existingOpp.Count__c + 1; } oppsToUpdate.add(existingOpp); } } } Map<Id,Opportunity> newMap = new Map<Id,Opportunity>(); newMap.putAll(oppsToUpdate); if(newMap.size() > 0) { update newMap.values(); } } }
//Test Class @isTest public class CreateOppAfterCAInsertionHelperTest { @testSetup static void recordsInsertion() { List<Opportunity> newOppsList = new List<Opportunity>(); List<Campaign_Account__c> newCampaignAccs = new List<Campaign_Account__c>(); Account newAcc = new Account(Name='Test Acc'); insert newAcc; newOppsList.add(new Opportunity(Name='TestOpp', StageName='Closed Won', CloseDate=date.today(), AccountId=newAcc.Id)); newOppsList.add(new Opportunity(Name='CampaignOpp', StageName='Prospecting', CloseDate=date.today(), AccountId=newAcc.Id)); insert newOppsList; newCampaignAccs.add(new Campaign_Account__c(Name='Test Record', Account__c=newAcc.Id)); newCampaignAccs.add(new Campaign_Account__c(Name='CampaignOpp', Account__c=newAcc.Id)); insert newCampaignAccs; } @isTest static void method1() { Test.startTest(); CreateOppAfterCAInsertionHelper campAccHelper = new CreateOppAfterCAInsertionHelper(); Test.stopTest(); for(Opportunity opp : [SELECT Name, Count__c FROM Opportunity WHERE Name = 'CampaignOpp']) { system.assertEquals(1, opp.Count__c); } } }Test class is throwing error at the highlighted line. Can someone help me with how to resolve it...
you are building a map using the record.Name of the Opportunity.
by the use of variable names, i suspect at this stage you are expecting to use the Campaign Name However, in your test data there is a disconnect between the Name in the Opportunity and the Name in the Campaign, therefore i suspect you are getting somthing like a null value error.
I would
1. Update my test code so that the Campaign Name for 'Test Record' equals 'TestOpp' or vice-versa.
2. In the executing code, put a test for the map to check that it has values. Perhaps
HTH
Andrew