You need to sign in to do that
Don't have an account?
Need help with getting test coverage on trigger
Im having trouble getting any test coverage on my trigger. I am getting 85% on the first class thats executed by my trigger, but I just cant seem to get any coverage at all on the actuall trigger.
Im thinking that part of the reason is since I am checking the users accessability to a custom object that I may need to create a user in the test? Not sure..
Im assuming I shouldnt be referencing an exsiting record to my custom object x_advertiser__c as well, instead creating that object and inserting it?
Anyways, any insight would be much appreciated. The help here on the forums have been really great.
Heres the testmethod:
public class testxOpportunityGrab { private static testmethod void xOpportunityGrabTest() { Opportunity opportunity = new Opportunity (name = 'test', campaign_created_in_x__c = false, stagename = 'Renew', closedate = system.today(), x_advertiser__c = 'a00U000000161iQIAQ', campaign_start_date__c = Date.today(), campaign_end_date__c = Date.today() + 30); insert Opportunity; List<Opportunity> listOpportunity = new List<Opportunity>(); listOpportunity.add(Opportunity); xOpportunityGrab.addOpportunityGrab(listOpportunity); } }
Hers the trigger:
trigger xCheckAdvertiserTrigger on Opportunity (before update) { for (Opportunity o:Trigger.new) { // Check to see if user has access to custom object x_advertiser__c Schema.DescribeFieldResult d = Opportunity.x_advertiser__c.getDescribe(); Boolean usr = d.isAccessible(); // If usr does, then continue through the process of executing the trigger if (usr == true) { // Instantiate passVarification vars Boolean passedVarification1; Boolean passedVarification2; Boolean passedVarification3; // Validate fields if (o.campaign_created_in_x__c == true && o.x_Advertiser__c == null) { o.campaign_created_in_x__c = false; o.x_Advertiser__c.addError('You must select an x Advertiser.'); } else { passedVarification1 = true; } // Validate fields if (o.campaign_created_in_x__c == true && o.Campaign_Start_Date__c == null) { o.campaign_created_in_x__c = false; o.Campaign_Start_Date__c.addError('You must select a campaign start date before you can create the campaign in x.'); } else { passedVarification2 = true; } if (o.campaign_created_in_x__c == true && passedVarification1 == true && passedVarification2 == true) { passedVarification3 = true; } else { passedVarification3 = false; } // If all validation passed then run the create campaign process if (passedVarification1 == true && passedVarification2 == true && passedVarification3 == true) { Opportunity[] opps = Trigger.new; //Call main x method xOpportunityGrab.addOpportunityGrab(opps); } //System.debug(' passedVarification1 :::: '+passedVarification1); //System.debug(' passedVarification2 :::: '+passedVarification2); //System.debug(' passedVarification3 :::: '+passedVarification3); } else { } } }
And heres the main class called by the trigger:
public class xOpportunityGrab { public static void addOpportunityGrab(Opportunity[] opps) { for (Opportunity o: opps) { // Set campaign checkbox to true if flase o.campaign_created_in_x__c = true; // Use Opportunity name as campaign name String xCampaignName = string.valueOf(o.name); // Grab saleforce Id to use for query ID sf_xAdvertiserId = o.x_Advertiser__c; // Query to determine the value of Opportunity x advertiser Id string query = 'Select x_advertiserId__c from x_advertiser__c where id =\'' +sf_xAdvertiserId+'\' '; // Create object to hold Id Value sObject advIdObject = Database.query(query); // Extract Id from object String xAdvertiserID = string.valueof(advIdObject.get(x_Advertiser__c.x_AdvertiserId__c)); // Get opportunity owner Full name Opportunity oo = [Select oo.Owner.name, oo.OwnerId From Opportunity oo where oo.Id = :opps]; String ownerName = String.valueOf(oo.Owner.name); // Campaign start date from custom field String xCampaignStartDate; // Check for missing Start date, if true campaign start date is today if (string.valueOf(o.campaign_Start_Date__c) == null) { xCampaignStartDate = String.valueOf(system.today()); } else { xCampaignStartDate = String.valueOf(o.campaign_start_date__c); } // Campaign end date from custom field String xCampaignEndDate; if (string.valueOf(o.campaign_End_Date__c) == null) { xCampaignEndDate = ''; } else { xCampaignEndDate = String.valueOf(o.campaign_end_date__c); } // Callout to xml api process if (!Test.isRunningTest()) { xXmlApiCall.sendRequest(xCampaignName, ownerName, xAdvertiserID, xCampaignStartDate, xCampaignEndDate); } } } }
Okay, So I figured out my biggest issue.
I was calling the class instead of actually triggering the trigger from the test class...
I needed to add update listOpportunity; <--- triggered the trigger.. i think... haha
Im still interested in any suggestions that may be better. Im very new to apex, and coding in general, as Im sure you can all tell ;)
Heres my new test class:
This gives me 76% covered on the trigger for now.
Glad you found out on your own.
Basically when you test triggers, it is enough that you perform the operation for the trigger. (Insert or Update).
This will fire the appropriate trigger (insert operation will cause a Before/After Insert trigger to be fired). The trigger will inturn call the apex class and you need not call it explicitly.
Just make sure that you insert or update the record with the desired values to pass through the trigger.