order by docuRef.createddatetime desc
where docuRef.RefTableId == tableNum(SalesTable)
where docuRef.RefRecId == salesTable.RecId
&& docuRef.ActualCompanyId == curext();
exists join docuType
&& where docuType.TypeId == docuRef.TypeId
&& docuType.TypeGroup == DocuTypeGroup::Note;
inner join salestable on SALESTABLE.RECID=DOCUREF.REFRECID
inner join DOCUTYPE on DOCUTYPE.TYPEID=DOCUREF.TYPEID
where DocuType.TypeGroup=0-- like 'Note'
and docuref.ACTUALCOMPANYID like 'companyName'
--where DOCUREF.REFTABLEID=
order by DOCUREF.CREATEDDATETIME desc