Activity Forums Salesforce® Discussions Test is not going inside "trigger.new" while testing Salesforce Trigger

  • Test is not going inside "trigger.new" while testing Salesforce Trigger

    Posted by Raghav Chaturvedi on April 23, 2018 at 4:26 am

    In my trigger testing, the test is not going inside trigger.new

    `
    trigger Setpricebookentry1 on Product2 (after insert)
    {
    Set prodIdSet = Trigger.newMap.keySet();
    list listpbe=new list();
    Pricebook2 prbook=[select id from Pricebook2 where isStandard=true and isActive=true];
    system.debug('++++++>'+prbook);
    for(Product2 po:Trigger.New)
    {
    system.debug('=====>'+trigger.new);
    Pricebookentry pbe=new Pricebookentry();
    pbe.UnitPrice=10;
    pbe.Pricebook2Id=prbook.id;
    pbe.Product2Id=po.id;
    listpbe.add(pbe);
    }
    if(listpbe.size()>0)
    {
    insert listpbe;
    system.debug('------>'+listpbe);
    }
    }
    `

    this is my test code

    `
    @istest
    public class TestSetpricebookentry1
    {
    @istest
    public static void mytest()
    {
    Product2 po=new Product2();
    po.Name='Raghav';
    po.IsActive=true;
    insert po;
    Id pricebookId = Test.getStandardPricebookId();
    PricebookEntry standardPrice = new PricebookEntry();
    standardPrice.Pricebook2Id = pricebookId;
    standardPrice.Product2Id =po.Id;
    standardPrice.UnitPrice = 10000;
    standardPrice.IsActive = true;
    insert standardPrice;
    }
    }
    `

    Raghav Chaturvedi replied 4 years, 9 months ago 2 Members · 6 Replies
  • 6 Replies
  • Aman

    Member
    April 23, 2018 at 6:44 am

    Hi Raghav,

    You have not inserted the standard price book in your test class and you are querying the standard pricebook in your trigger. Insert a standard pricebook in your test class and run the test class again, it will work.

     

  • Raghav Chaturvedi

    Member
    April 23, 2018 at 12:45 pm

    I tried with inserting standard pricebook but there is no effect.

    can you reply with code

  • Aman

    Member
    April 23, 2018 at 6:10 pm

    Hi,

    Below is the code for your test class :

    @istest(SeeAlldata=true)
    public class SetPriceDemoTest {
    @istest
    public static void mytest()
    {
    Product2 po=new Product2();
    po.Name='Raghav';
    po.IsActive=true;
    insert po;
    }
    }

  • Raghav Chaturvedi

    Member
    April 24, 2018 at 4:14 am

    I have studied that using (SeeAlldata=true)  is not a best practice

  • Aman

    Member
    April 24, 2018 at 5:57 am

    Hi,

    Below is the code for test class. Below code providing 100% coverage.

    @istest
    public class SetPriceDemoTest {
    @istest
    public static void mytest()
    {
    Id pricebookId = Test.getStandardPricebookId();
    pricebook2 pb = new pricebook2(id=pricebookId,isActive=true);
    update pb;
    Product2 po=new Product2();
    po.Name='Raghav';
    po.IsActive=true;
    insert po;
    }
    }

  • Raghav Chaturvedi

    Member
    April 24, 2018 at 6:20 am

    thanks

Log In to reply.

Popular Salesforce Blogs