• Ember Campbell 10
  • NEWBIE
  • 0 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 4
    Replies
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.  
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.