0

I have refered the following thread to solve my problem. Not sure where I am going wrong. Compare records with same field value from different Objects in apex?

Here is my attempt:

PROBLEM: While inserting a record with 'product_code__c' into the 'Products_To_Order__c' object database, 'Order_Status__c' should show 'Do Not Order', if the same product code is present in the Slow_Moving_Products__c object.

Example:

Record to Insert into Products_To_Order__c Object :
Name='VomoXL 114 Headphones'.
Product code= 'A15S179'.

Record to Compare with in Slow_Moving_Products__c
Name= 'VomoXL 114 Headphones'
Product code= 'A15S179'

EXPECTED OUTPUT (What I am trying to get):
Order_Status__c='Do not order'

Actual output (What I actually got):
Order_Status__c='Eligible to be ordered'

APEX CLASS:

public class SlowingMovingProductCheckClass {

public static void CheckSlowMovingProducts(list<Products_To_Order__c>ProductsToOrderList)
{    
list<Slowing_Moving_Products__c> SlowingMovingProductsList= [SELECT ProductCode__c FROM Slow_Moving_Products__c];
set<string>SlowMovingSet = new set<string>();

    

    for (Slowing_Moving_Products__c s: SlowingMovingProductsList)
    {
        SlowMovingSet.add(s.ProductCode__c) ;
    }
    
    

    for(Products_To_Order__c p : ProductsToOrderList)
    {
          
   if(SlowingMovingListSet.contains(p.ProductCode__c))
   {
       p.Order_Status__c='Do not order';
   }
   else
   {
       p.Order_Status__c='Eligible to be ordered';
   }
    }
        
    
    }
    
}

TRIGGER:

trigger Slow_Moving_Check_Trigger on Products_To_Order__c(before insert) {

  
       SlowingMovingProductCheckClass.CheckSlowMovingProducts(Trigger.new);
    
}

1 Answer 1

2

You should never query the entire database (a SOQL with no filters and no LIMIT). In addition, keep in mind that standard Set<String> is case sensitive, so it is typical that you'd want to normalize the values to the same casing before proceeding. A more appropriate solution would look like this:

public class SlowingMovingProductCheckClass {
    public static void CheckSlowMovingProducts(list<Products_To_Order__c>ProductsToOrderList) {
        Set<String> SlowMovingSet = new set<string>();
        Set<String> productCodes = new Set<String>();
        for(Products_To_Order__c record: ProductsToOrderList) {
            productCodes.add(record.ProductCode__c);
        }
        productCodes.remove(null);
        for(Slowing_Moving_Products__c record: [SELECT ProductCode__c FROM Slowing_Moving_Products__c WHERE ProductCode__c = :productCodes]) {
            SlowMovingSet.add(record.ProductCode__c.toLowerCase());
        }
        for(Products_To_Order__c record: ProductsToOrderList) {
            if(SlowingMovingListSet.contains(p.ProductCode__c?.toLowerCase())) {
                record.Order_Status__c='Do not order';
            } else {
                record.Order_Status__c='Eligible to be ordered';
            }
        }
    }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .