How do I get the name of the product in an opportunity using Groovy?

ChildRevenue is the collection on the Opportunity object that tells you what products have been added to the respective opportunity. By taking the first record from that collection, you get either the Description or the Product Group Name, depending on the product type: product or product group.

Here's a sample script:

def a = ChildRevenue

if (a.hasNext()){

def b = a.first()

setAttribute('Name', Name + nvl(b.Description, b.ProdGroupName))

}