Just wanted to add that sometimes the Court will still ask you because they don't know that they're not supposed to ask you.
I've seen this happen several times. In Ohio, the situation with forensic psychiatry is relatively more advanced than many other areas because we have three of the best forensic psychiatrists in the country (name-brand) here and they've worked extensively with the state and local courts to advance the interface between law and medicine. Several areas have no forensic psychiatrist whatsoever, but in this area I can name several, many of whom did work to advance the court's knowledge of how things are supposed to work.
In other areas, many of the forensic psychiatrists didn't give a damn about the court's lack of knowledge and merely wanted to profiteer by making a private enterprise with no desire to advance the state of the system. So many forensic psychiatrists operate on a notion that it's their job to win the case for their defendant for the $$, not give a real and honest examination. A forensic psychiatrist, is supposed to act as a guide and explain how psychiatry works to the court. Sometimes, unethical psychiatrists will direct courts in a manner that is misleading so they can profiteer. (The hired gun phenomenon.)
Yet even here, there are still so many problems where courts think we doctors can do things we can't. I have seen so many patients diagnosed with antisocial PD and some courts think that's possible grounds for a not guilty by reason of insanity plea because it's a disorder in the DSM.
Bottom line when this happens, just tell the Court you didn't do an evaluation for custody. There will be doctors who do give opinions on custody when they didn't do an evaluation. No, they don't know something you don't. They're just being idiots. I've been in Courts where other doctors, for years, did the wrong thing, so the Court thinks it's alright and the norm and then asked me why I refused to give them an opinion. In cases like this, I just frankly tell the Court, "If Dr. X wants to give an opinion that doesn't meet the ethics or standards of the profession, I can't stop him. I can, however, not do that myself. If you would like to me to show you these guidelines, I will be happy to do so." I then show the Court the guidelines I'm talking about (usually already printed up by the APA or AAPL) if asked.
Of course this is an awkward situation to be in...if I actually happen to like that doctor. If that happens, I try to be more diplomatic or warn the doctor ahead of time to stop the practice because I'm going to have to reveal it. Other times, especially with a-hole quack doctors (I'm being a bit frank), I just let it all out bluntly, especially when I've tried to have a talk with them and they blow me off.
The last time this happened to me was last week. A colleague of mine that I actually like tried to keep a person involuntarily committed that didn't speak English and this doctor never once bothered to use a translator. I told her that if she didn't use one by the time I had to testify on the case, she have egg on her face big-time in front of the court. Thankfully for both of us, she followed my advice, got a translator, and was able to argue that her impression that the patient was truly psychotic was not merely based on a language barrier.
Had she not done this, I would've had to have said on the stand that she was not following the standard of care. Now given that I actually like this doctor, I would've been in a very awkward situation, especially since I know the defense attorney and he would've went after that other doctor big time and I would've had to have assisted in tearing her apart while I was on the stand. On the flip-side, I actually happen to like that when I know the doctor's terrible, but in those cases, the doctor is usually not in court, or doesn't give a damn anyway.