0

I'm having a custom Lookup of Product Field on Asset Object .I want that whenever i fill this Field ,it should populate the same with standard lookup of Product Field .Currently i'm using before triggers to Update the Standard Field and i'm even getting the ID in Deug log ..but it's not poulating on record . Here is Code:

private static void updateCustomModelField(list<Asset> lsttriggernew,map<Id,Asset> triggeroldmap)
{
    if(lsttriggernew != null && !lsttriggernew.isEmpty())
    {
        set<id> setProduct = new set<id>();
        list<Asset> lstAsset = new list<Asset>();

        for(Asset objAsset : lsttriggernew)
        {
            if(objAsset.VGA_Model__c != null && (triggeroldmap == null || 
                (triggeroldmap != null && triggeroldmap.get(objAsset.id).VGA_Model__c != objAsset.VGA_Model__c)))
            {
                setProduct.add(objAsset.VGA_Model__c);  
            }  
        }
        if(setProduct != null && !setProduct.isEmpty())
        {
            list<Product2> lstProduct = [select id from Product2 where id in:setProduct];

            if(lstProduct != null && !lstProduct.isEmpty())
            {
                map<id,Product2> mapofProduct = new map<id,Product2>();
                for(Product2 objProduct : lstProduct)
                {
                    if(!mapofProduct.containskey(objProduct.id))
                        mapofProduct.put(objProduct.id,objProduct);
                }
                system.debug('@@@mapofProduct'+mapofProduct);
                if(mapofProduct != null && !mapofProduct.isEmpty())
                {
                    for(Asset objAsset : lsttriggernew)
                    {
                        if(objAsset.VGA_Model__c != null && mapofProduct.containskey(objAsset.VGA_Model__c) 
                            && mapofProduct.get(objAsset.VGA_Model__c) != null)
                        {
                            objAsset.product2 = mapofProduct.get(objAsset.VGA_Model__c);
                        }
                    }
                }
            }
        }
    }
}
1

I think you can achieve what you need in simpler way

private static void updateCustomModelField(list<Asset> lsttriggernew,map<Id,Asset> triggeroldmap)
{
    if ( lsttriggernew == null || lsttriggernew.isEmpty() ) 
        return; // It is better to use early exit.

    for(Asset objAsset : lsttriggernew)  {
            if( triggeroldmap == null || triggeroldmap.get(objAsset.id).VGA_Model__c != objAsset.VGA_Model__c )
            {
                objAsset.product2Id = objAsset.VGA_Model__c;
            }  
        }

    }
}

Also make sure that you call updateCustomModelField in before trigger

  • (triggeroldmap == null || triggeroldmap.get(objAsset.id).VGA_Model__c != objAsset.VGA_Model__c) will suffice. – sfdcfox Dec 14 '17 at 20:49
  • yes, I do agree – Patlatus Dec 14 '17 at 22:24

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.