0

I have a method that searches an object with a unique key and then takes that ID and populates a field on the Product object. This seems to work as I can pass the value from a trigger handler to a helper class, then the logic finds the record and updates the product.

The next step that if there is no record found I want to pass the product's information to a map so in a different method I can create a new object record. I think I need to put an if a record is returned do the update else put the info into a map to be handed off. However, I cannot figure out how to do it with the correct syntax

Is there a good way to do this?

public static void existingBundleSearch(map<String, List<Product2>> mapProductIdentifier)
{
    // Query on product bundle object and update the all the product 'Product_Bundle__c' lookup with Product_Bundle__c id
    Map<Id, Product2> prods = new Map<Id, Product2>();
    List<Product2> prodUpdate = new List<Product2>(); // list of products to Update
    for(Product_Bundle__c prodBundle: [ SELECT  Id,
                                                Product_Identifier__c
                                        FROM Product_Bundle__c 
                                        WHERE Product_Identifier__c IN: mapProductIdentifier.keySet()])
    {
        for(Product2 prod : mapProductIdentifier.get(prodBundle.Product_Identifier__c))
        {
            //if(!prodBundle.size() = 0)
            {
                prodUpdate.add(new Product2(
                    Id = prod.Id, 
                    Product_Bundle__c = prodBundle.Id
                ));
            //} else{
            //  prods.putAll(prod);
            }
        }
    }
        update prodUpdate;
        createBundle(prods);
}
1

If I understood the question:-

for(Product2 prod : mapProductIdentifier.get(prodBundle.Product_Identifier__c))
{
    if(prod != null && ! mapProductIdentifier.get(prodBundle.Product_Identifier__c).size()==0)
    {
        prodUpdate.add(new Product2(
            Id = prod.Id, 
            Product_Bundle__c = prodBundle.Id
        ));
    //} else{
    //  prods.putAll(prod);
    }
    else
    {
        prods.put(prod.id,prod)
    }
}

prod.id is going to be unique so, it would not create issues.

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.