If you want to retrieve TaskNum from service task have to run this script on service.
RoboTask application and RoboTask service are different instances of RoboTask. RoboTask service is the separate process with own tasks. TaskNumByExternalName method retrieve TaskNum from local set of tasks only.