You haven’t shown it but if you want to add code to a browse you probably need to say so in your template declaration. eg for a Control template, the same idea applies to an Extension template…
#CONTROL(BrowsePrintButton,'Print record from a Browse Box'),DESCRIPTION('Print a Record from Browse on ' & %Primary),REQ(BrowseBox),FIRST,HLP('~TPLControlBrowsePrintButton.htm')
…the REQ(BrowseBox) links your code to whichever browse box is selected when adding your template to the procedure. This then becomes the %ActiveTemplateInstance, you can’t create that link yourself, so far as I am aware?
Otherwise, maybe some more detail about what you are looking for? It looks like some mixing of previously generated code into your template, which could be limiting.
What I have in place is a template that works fairly well. The template is designed to allow Insert, Change, Delete functionality to a browse (on the same window).
The problem (solved now) relates to, “what happens when the browse has NO records?” The template now has a “No Browse records” procedure, which will clean up any “stuff” that is calculated or displayed on the window.
I am new to this group, and thanks for your concern.