Drug class information is contained in the GPI code. You can look up categories on the web. For a given patient medication, in table MEDICATE, the gpi field is simply GPI. In MEL using Meds_After("delimited") it is field "4". You will need to parse out the initial characters of the field for the category. The following code snippet retrieves the opioids, initial string "65", and returns the list to a document variable. If you have questions, feel free to contact me.
//retrieve opioids and start dates
local i=0
local strRet=""
local arrayOp
local strOp
strOp=Meds_After("delimited")
if strOp<>"" then
document.M414_Rx6="no"
arrayOp=getfield(strOp,"|","")
for i=1,i<=size(arrayOp),i=i+1 do
arrayOp[i]=getfield(arrayOp[i],"^","")
if arrayOp[i][4]<>"" then
if sub(arrayOp[i][4],1,2)="65" then
strRet=strRet+(if strREt<>"",HRET,"")+arrayOp[i][2]+", start date--"+arrayOp[i][9]
if val(DurationDays(arrayOp[i][9],str(._Todaysdate)))>=42 then
document.M414_Rx6="yes"
endif
endif
endif
endfor
endif
DOCUMENT.OPIOID_LIST=strRet