As someone who has played a lot of grappling characters, casters and even grappling casters (favorite character ever!), the answer is actually simpler than it seems.
Short Answer: Yes. While grappling, touch attacks against your opponent automatically succeed.
Assuming you pass the concentration check (in order to cast a spell while grappled). You have a hand charged with a spell (spells always charge one hand unless otherwise specified). When casting a spell that requires a melee touch attack, the "attempt to touch" is always considered to be part of the standard action after the spell is successfully cast. So inside the grapple, touching the creature you are grappling with is free (as part of the spell) and easy (there is no way for a grappling creature to avoid a touch attach from another creature in the same grapple).
Touch spells can only be delivered by a caster's free hand. So if the caster has the "pinned" condition, they cannot cast or deliver a spell. If the caster is using both hands to climb a rope, or wield a sword and shield, or anything else that requires two hands, they cannot cast or deliver the spell.
Spells that are ranged touch attacks are almost always a ray, that emanate from the finger of one hand.
In addition, spells do not "misfire" on anything but the target that the caster intends to target. A long time ago, it was explicitly stated in the rules that the target of the spell is named during the verbal component of the spell. If there is no verbal component, then it is considered to be part of the training of the mage that he/she/it can hold the target firmly in their mind enough to deliver the spell to the intended target. So there is no way to steal a charged spell, cast it on yourself, etc.
From the way the question was worded, I suspect that the grappling portion of the combat might not have been handled correctly, so I'm throwing in a little extra info on how the grappling portion of the combat should play out:
First off, you need to know the grappling status of both the monster and yourself.
In Pathfinder combat rules, pretty much any time two creatures are touching each other it is considered a grapple. The vast majority of the time, whenever there is a grapple, both creatures gain the "grappled" condition.
If the Slug/Tentacle monster was tiny like a leech, then it might have the "Attach" special ability. Which means that the monster gains the "grappled" condition but the player does not. Considering the original question, if the creature was "attaching itself to you" and you did not gain the grapple condition, then touch attacks would not automatically succeed. You would still need to roll against its touch AC. However the creature is grappled and loses its Dex bonus to AC and it cannot move, so it should be fairly easy to hit.
If it was super-gigantic like a Kraken, then it has enough appendages that the player gains the "grappled" condition but the monster does not.
But most likely the monster was normal or small size and attacked you (the player) with the "Grab" special ability, which means that it gets a free grapple attempt each time it performs a normal attack. That free grapple attempt is not an automatic success, it is a combat maneuver, and you get to roll your CMD (combat maneuver defense) against it. If you win, the grapple attempt fails and nobody is attached to anything. But for the sake of argument lets assume you failed your CMD, now both parties gain the "grappled" condition.
One of the things you can do while grappled is perform a standard action that only requires one hand. It explicitly lists "like casting a spell" in the rules. However, you need to make a concentration check against the monsters grappling CMB. If you fail, the spell "fizzles" (it is spent, but nothing happens)