function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Alisa ZhurovaAlisa Zhurova 

Why this code not add records in object PricebookEntry?

trigger UpdatePrice on Product2 (after insert) {
	
    list<PricebookEntry> pblist=new list<PricebookEntry>();
    PricebookEntry pr=new PricebookEntry();  
      
    for(Product2 ps:trigger.new){
        if(ps.isActive==false){
            pr.Product2Id = ps.id;
            pr.Pricebook2Id = '01s5g00000JO516AAD';
            pr.isActive = true;
            
            Integer len = 5;
            final String chars = '123456789';
            String randStr = '';
            
            while(randStr.length()<len){
                Integer idx = Math.mod(Math.abs(Crypto.getRandomInteger()), chars.length());
                randStr += chars.substring(idx,idx+1);
            }
            
            pr.UnitPrice = Integer.valueOf(randStr);
        }
    }         
    
    pblist.add(pr);
    insert pblist;

    system.debug('***'+pr);

}
User-added imageUser-added imageUser-added imageUser-added image