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
DX-NoobDX-Noob 

Populating Line Description Using Trigger

I've been using the trigger below to automatically add the Product Description to the Line Description in the Opportunity Product. However, if a user manually adds a Line Description when adding a product, that manual line description is overwritten. Any thoughts of what can be added to this to prevent that from happening.  Here's the code:

 

trigger LineDescriptionFromProduct on OpportunityLineItem (before insert) 

{
    // Add pricebookentry ids to an array for future query
    ID[] idSet = new ID[trigger.new.size()];
    for(Integer i = 0; i < trigger.new.size(); i++)
    {
        idSet[i] = trigger.new[i].PricebookEntryId;
        System.Debug(trigger.new[i].PricebookEntryId);
    }
    
    // Create a map for product ids/descriptions
    Map<ID, String> descMap = new Map<ID, String>();
    for(PricebookEntry entry: [select p.Id, p.Product2.Description from PricebookEntry p where p.Id in :idSet])
    {
        System.Debug(entry.Id + ' - ' + entry.Product2.Description);
        descMap.put(entry.Id, entry.Product2.Description);
    }
    
    // Set descriptions
    for(OpportunityLineItem line: trigger.new)
    {
        System.debug(descMap.get(line.PricebookEntryId));
        line.Description = descMap.get(line.PricebookEntryId);
    }
}