• IT Admin 35
  • NEWBIE
  • 0 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 0
    Replies
I would like to write a trigger on product that will insert/update the pricebook when a product is inserted or updated.
The following code does not seem to work. Sorry new to Apex coding

trigger TrgPricebookEntry on Product2 (after insert, after update)
{
    Set<ID> ids = Trigger.newMap.keySet();
    sObject s = [select ID from Pricebook2 where IsStandard = TRUE and IsActive= TRUE];

    list<PricebookEntry> entries=new list<PricebookEntry>();

    if(trigger.isinsert)
    {
        for (Product2 p : Trigger.new)
        {
            entries.add( new PricebookEntry( Pricebook2Id=s.ID, Product2Id=p.ID, UnitPrice=p.Price__c, IsActive=p.IsActive, UseStandardPrice=FALSE) );
        }
        insert entries;
    }

    list<PricebookEntry> pbe = [SELECT Id FROM PricebookEntry WHERE Product2ID in : ids];
            
    if(trigger.isupdate)
    {
        for (Product2 p : Trigger.new)
        {
            if (pbe != null)
            {
                pbe.UnitPrice=p.Price__c;
            }
        }
    }
}

 
I get an error trying deploying Apex classes to production since code coverage is 54%. I have made the changes to the Sandbox and brought the code coverage to over 80%. I am trying to deploy the changes to production but the deployment still fails with the 54% error message. How can I deploy the changes to my Apex classes from Sandbox to Production?