James Marsden Says a Random Encounter with Julia Roberts Got Him His Hairspray Role in Musical

James Marsden has one very special actress to thank for his role in Hairspray.

The Dead to Me star, 46, recently appeared on The Ellen DeGeneres Show and recounted his experience being cast as Corny Collins in the 2007 film, inspired by the hit Broadway musical.

"I was sitting down with Adam Shankman, the director, and Craig Zadan and Neil Meron at the time at an empty restaurant off Broadway in New York City," Marsden told host Ellen DeGeneres. "And we were just talking about the potential of me playing Corny Collins."

"In the middle of the interview, I had a tap on my shoulder and it was Julia Roberts, she was standing there," he recalled. "She said 'I don't mean to interrupt, but I just wanted to tell you that you're in my favorite movie, The Notebook, and I love what you do in the film, and congratulations on it, and nice to meet you.' "

Phillip Faraone/Getty Images; Lester Cohen/Getty Images

RELATED: Dead to Me Season 2: How Not to Get Away with Murder, According to Christina Applegate

Marsden said that immediately after Roberts, 52, left, his role in Hairpsray became a guarantee.

"Adam Shankman turns to me and goes, 'You didn't have the job already, but I think that sort of seals it.' " he said. "So I have Julia Roberts to thank for that, she was the one who cemented me getting cast in that role."

David James/New Line/Kobal/Shutterstock

Hairspray featured a star-studded cast of Marsden, John Travolta, Michelle Pfeiffer, Zac Efron, Brittany Snow, Amanda Bynes, Queen Latifah, Allison Janey, Christopher Walken, and Nikki Blonsky.

The film debuted in theaters on July 20, 2007 and was a critical and financial success, holding the record for biggest sales at opening weekend for a movie musical until Mamma Mia! the following year.