In general, ignorance of the law isn’t a viable defense to criminal charges, even if it results from the erroneous advice of a lawyer. For example, a defendant can’t overcome assault charges by claiming that his lawyer advised him that it was okay to punch the victim.
But on the other hand, a mistaken belief about the legality of certain conduct can constitute a defense if it negates the required state of mind for a conviction. For example, in one case, a husband who was prohibited by a temporary restraining order from entering his and his wife’s home nevertheless entered it through a window, purportedly to collect his business records. A jury convicted him of offenses including trespass, which specifically required that the defendant know that he or she is not allowed to enter the premises in question. The defendant testified that three lawyers had advised him that he was entitled to enter the home when he did.
In upholding the defendant’s conviction, and given the state-of-mind element of trespass, the North Dakota Supreme Court remarked that the defendant was entitled to testify about having relied on the advice of counsel. However, it found that the jury reasonably concluded that, whatever anyone might have told him, he knew that he wasn’t allowed to be in the house. It therefore affirmed the conviction. (State v. Bertram, 708 N.W.2d 913 (2006).)