-1

it appears my if statement with String.isEmpty doesn't seem to be working. What i want it to do is check if a price list entry doesn't exist it creates a new one, and if it does it updates that one. So, the code works for one or the other but it doesn't work when utilising then at the same time in an if statement.

Apex Code:

         Map<String, Product2> productByName = new Map<String, Product2>();
             for (Product2 prod : [SELECT Name FROM Product2 WHERE Name IN :csvNames]) {
                productByName.put(prod.Name, prod);
              }
              Map<String, PricebookEntry> productByEntry = new Map<String, PricebookEntry>();
              for (PricebookEntry prEntry : [SELECT Product2.Name FROM PricebookEntry WHERE Product2.Name IN :csvNames]) 
           {
            productByEntry.put(prEntry.Product2.Name, prEntry);
           }
             for (Integer i = 1; i < csvFileLines.size(); i++) {

                PricebookEntry prEnt = new PricebookEntry();
                PricebookEntry updPrEnt = new PricebookEntry();
                 String[] csvRecordData = csvFileLines[i].split(',');
                 String csvName = csvRecordData[0];
                 String unit_price = csvRecordData[5];
                 ID productID = productByName.get(csvRecordData[0]).Id;
                 ID pricebookID = '01s4J000001yJ9UQAU';
                 String entryId = productByEntry.get(csvRecordData[0]).Id;

                 if(String.isEmpty(entryId) == true){

                  prEnt.Product2Id = productID;
                  prEnt.Pricebook2Id = pricebookID;
                  prEnt.IsActive = True;
                  prEnt.UnitPrice = Integer.valueOf(unit_price);
                  prEnt.UseStandardPrice = False;
                  prEnt.CurrencyIsoCode = 'GBP';
                  priceEntry.add(prEnt);
                 }
                  else
                  {
                  updPrEnt.Id = productByEntry.get(csvRecordData[0]).Id;
                  updPrEnt.IsActive = True;
                  updPrEnt.UnitPrice = Integer.valueOf(unit_price);
                  updatePriceEntry.add(updPrEnt);
                 }
             
             if(priceEntry.size()>0){
              insert priceEntry;
            }
             
             if(updatePriceEntry.size()>0){
                 update updatePriceEntry;
             }
0

I figured out a solution utilise .containsKey to instead of isBlank

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.