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
D srinivas 8734D srinivas 8734 

while importing products from other system i am able to create pricebookentries for standard pricebook

while importing products from other system i am able to create pricebookentries for standard pricebook,i need to create pricebookentries for custom pricebooks also ?
here is my code

QB_PriceBook__c pb_custset=QB_PriceBook__c.getOrgdefaults();
          system.debug('pb name====='+pb_custset.global_123__pricebook_Name__c);
         list<pricebook2> QB_pricebooklst=[select id from pricebook2 where Name=:pb_custset.pricebook_Name__c AND isActive=true];
        
        // map of all price book entry
        
        Map<ID,PricebookEntry> priceBkEntryMap= new Map<Id, PricebookEntry>();
        
        for ( PriceBookEntry pbEntry : [select Product2Id, UnitPrice from PriceBookEntry where Product2Id in :prodMap.keySet()]){
            priceBkEntryMap.put(pBEntry.Product2Id, pbEntry);
        }   
        system.debug('pricebookentrymap'+priceBkEntryMap);
        List <PriceBookEntry> pbEntryToUpdate = new List<PriceBookEntry>(); 
        
        for(product2 prd:prolst ){
            // check if pricebookentry already exists
            system.debug('product id:'+prd.id);
            PricebookEntry pbEntry = priceBkEntryMap.get(prd.Id);
            system.debug('Found Pricebook entry:' + pbEntry);
            if (pbEntry != null) {
                System.debug('Updating Pricebook Entry');
                if (pbEntry.UnitPrice != decimal.valueOf(prd.price__c) ){
                    System.debug('Pricebook updating price only:'+  decimal.valueOf(prd.price__c) );
                    pbEntry.UnitPrice= decimal.valueOf(prd.price__c);
                    pbEntryToUpdate.add(pbEntry); 
                }    
            }
            else {
                System.debug('Creating new Pricebook Entry');
                PricebookEntry pbe = new PricebookEntry();
                pbe.Pricebook2Id=QB_pricebooklst[0].id; //pricebook ID
                pbe.Product2Id=prd.id;
                pbe.IsActive=true;
                //pbe.UseStandardPrice=false;
                pbe.UnitPrice=decimal.valueOf(prd.price__c);
                pbEntryToUpdate.add(pbe);
            }

        }
        system.debug('my pricebookentry: '+pbEntryToUpdate);
        if(pbEntryToUpdate.size()>0){
            upsert pbEntryToUpdate;
        }