I assume you are referring STO process, if so please find the below logic,
Take VBRK-VBELN, input this in VBFA table in VBELN(follow on document) and take VBELV( Prceeding document), pass this to EKBE table and input this in EKBE-BELNR(material document) and take EKBE-EBELN, pass this field to output the of the program.
thanks,
Srinu.