Thanks, JaideepBrar I have used your example but it still didn't work. I believe your shared example is helpful and the correct one, but I think there is an issue with my environment or the way it is setup. I had to implement the feature outside of AEM.
But thanks for the reply and I hope it helps others as well who have the same problem.
Clik here to view.
