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

Help with Code coverage at code coverage is 0%.
Hi all,
I'm new to apex triggers and had posted in the community to get a trigger to create a field that summarizes Opportunities Product Families. I was so excited when a wondeful community member was able to help. The code works create in my sandbox. I attempted to do a change set to move into production but have been unable. I submitted a case to Salesforce support and they recommded to post this forum since we are not on a Premier Sucess plan.
Code:
trigger populateProductFamily on OpportunityLineItem (after Insert,before delete) {
if((trigger.IsAfter && trigger.IsInsert) || (trigger.IsBefore && trigger.IsDelete) ) // run after insert and before delete
{
//map to hold product family for a gven oppty
Map<id,Set<String>> oppprodFamilyMap = new Map<id,Set<String>>();
//get all oppty id
List<id> oppId = new List<Id>();
List<opportunitylineitem> triggerList = new List<opportunitylineitem>();
if(trigger.IsDelete)
{ system.debug('in delete');
triggerList = trigger.old;
}
else
{
triggerList = trigger.new;
}
for (opportunitylineitem ol : triggerList ) {
oppId.add(ol.opportunityid);
}
// query all opportunity for opportunitylineitem inserted
List<opportunity> oppList = [select id ,Opportunity_Product_Family__c from opportunity where id in :oppId];
// query all opportunitylineitem linked with opportunity of current opportunitylineitem
//we need this so that we can get all product family for a a given oppty
List<opportunitylineitem> olList = new List<opportunitylineitem>();
if(trigger.IsDelete) {
olList = [select id,opportunityid,product2.family from opportunitylineitem where opportunityid in :oppList and id not in :triggerList ];
}
else
{
olList = [select id,opportunityid,product2.family from opportunitylineitem where opportunityid in :oppList ];
}
system.debug('---ollist' + olList );
//now we need to separate productfamily by opportunity
for (opportunitylineitem ol : olList ) {
IF(ol.product2.family != null) {
if(oppprodFamilyMap.get(ol.opportunityid) != null) {
Set<String> pFamilySet = oppprodFamilyMap.get(ol.opportunityid);
pFamilySet.add(ol.product2.family);
oppprodFamilyMap.put(ol.opportunityid,pFamilySet);
}
else {
Set<String> pFamilySet = new Set<String> ();
pFamilySet.add(ol.product2.family);
oppprodFamilyMap.put(ol.opportunityid,pFamilySet);
}
}
}
// now we have got all productfamily for a given oppty.
//we need to concat and update field
//opplist to update
List<opportunity> oppListToUpdate = new List<opportunity>();
for(opportunity o : oppList)
{
List<String> pFamilyList = new List<String>(oppprodFamilyMap.get(o.Id));
String pFamily = string.join(pFamilyList ,',');
o.Opportunity_Product_Family__c = pFamily;
oppListToUpdate.add(o);
}
if(oppListToUpdate.size()>0)
update oppListToUpdate;
}
}
Error - Code Coverage Failure
Your code coverage is 0%. You need at least 75% coverage to complete this deployment.
populateProductFamily
I'm sure I have to change the sandbox, then the change set and try to deploy again into but everything I've tried fails. Any help would be greatly appreciated.
I'm new to apex triggers and had posted in the community to get a trigger to create a field that summarizes Opportunities Product Families. I was so excited when a wondeful community member was able to help. The code works create in my sandbox. I attempted to do a change set to move into production but have been unable. I submitted a case to Salesforce support and they recommded to post this forum since we are not on a Premier Sucess plan.
Code:
trigger populateProductFamily on OpportunityLineItem (after Insert,before delete) {
if((trigger.IsAfter && trigger.IsInsert) || (trigger.IsBefore && trigger.IsDelete) ) // run after insert and before delete
{
//map to hold product family for a gven oppty
Map<id,Set<String>> oppprodFamilyMap = new Map<id,Set<String>>();
//get all oppty id
List<id> oppId = new List<Id>();
List<opportunitylineitem> triggerList = new List<opportunitylineitem>();
if(trigger.IsDelete)
{ system.debug('in delete');
triggerList = trigger.old;
}
else
{
triggerList = trigger.new;
}
for (opportunitylineitem ol : triggerList ) {
oppId.add(ol.opportunityid);
}
// query all opportunity for opportunitylineitem inserted
List<opportunity> oppList = [select id ,Opportunity_Product_Family__c from opportunity where id in :oppId];
// query all opportunitylineitem linked with opportunity of current opportunitylineitem
//we need this so that we can get all product family for a a given oppty
List<opportunitylineitem> olList = new List<opportunitylineitem>();
if(trigger.IsDelete) {
olList = [select id,opportunityid,product2.family from opportunitylineitem where opportunityid in :oppList and id not in :triggerList ];
}
else
{
olList = [select id,opportunityid,product2.family from opportunitylineitem where opportunityid in :oppList ];
}
system.debug('---ollist' + olList );
//now we need to separate productfamily by opportunity
for (opportunitylineitem ol : olList ) {
IF(ol.product2.family != null) {
if(oppprodFamilyMap.get(ol.opportunityid) != null) {
Set<String> pFamilySet = oppprodFamilyMap.get(ol.opportunityid);
pFamilySet.add(ol.product2.family);
oppprodFamilyMap.put(ol.opportunityid,pFamilySet);
}
else {
Set<String> pFamilySet = new Set<String> ();
pFamilySet.add(ol.product2.family);
oppprodFamilyMap.put(ol.opportunityid,pFamilySet);
}
}
}
// now we have got all productfamily for a given oppty.
//we need to concat and update field
//opplist to update
List<opportunity> oppListToUpdate = new List<opportunity>();
for(opportunity o : oppList)
{
List<String> pFamilyList = new List<String>(oppprodFamilyMap.get(o.Id));
String pFamily = string.join(pFamilyList ,',');
o.Opportunity_Product_Family__c = pFamily;
oppListToUpdate.add(o);
}
if(oppListToUpdate.size()>0)
update oppListToUpdate;
}
}
Error - Code Coverage Failure
Your code coverage is 0%. You need at least 75% coverage to complete this deployment.
populateProductFamily
I'm sure I have to change the sandbox, then the change set and try to deploy again into but everything I've tried fails. Any help would be greatly appreciated.
You need to write an apex test class.
For your trigger, you could insert an OpportunityLineItem record. Then your trigger will run ("after insert" scenario) .
And you could also delete this record which you created before in your test class, then "before delete" scenario will be cover.
Thanks.
You could check from here. test class (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm?search_text=test%20class)
Can you try running the below test class and see if the code coverage goes above 75%?
I think we are pretty close. When I added the class, there were 10 errors. I was able to fix four with adding a missing ";" to each row. Now I'm struggling with the variables. Seems like the code is not creating them to find them. I'll include a screenshot of the six errors and keep looking for a solution.
Thank you for the help.
I have corrected the code. Can you try below code.
Looks like we were able to get the class to not show errors in the developer console but the class still has zero coverage. I'm not sure what I'm doing wrong so I'll include two screenshots for reference:
Both fail:
Detailed message on failure
The error says list has no rows for assignement to sobject. System is not getting the data from SELECT query. Can you put system.debug() after the SELECT query and see if you are getting any data.
I am getting 100% code coverage.
Sharing trigger code and class for reference.
Trigger: populateProductFamily
Class: populateProductFamilyTest
Sorry, I'm not sure how to do the following: Can you put system.debug() after the SELECT query and see if you are getting any data.
I see system.debug listed in the trigger already. Should this be listed in the class as well?