John Krasinski saved “The Office” characters Jim Halpert and Pam Beesly’s relationship after the show’s writers wanted them to break up in season 9.

In the book “The Office: The Untold Story of the Greatest Sitcom of the 2000s,” author Andy Greene said the show creator, Greg Daniels, wanted to break up Halpert and Beesly in season 9 with a cheating scandal, Collider reported. The characters went public with their relationship in season 4.

One of “The Office” writers ,Owen Ellickson, revealed the secret alternate ending in the book.

JOHN KRASINSKI AND 'THE OFFICE' CAST RECREATE SHOW'S WEDDING DANCE FOR NEWLYWEDS

Actors John Krasinski (L) and Jenna Fischer interact as they receive gift bags from the Screen Actors Guild Awards Committee including a certificate for their nomination for an outstanding performance by an ensemble in a comedy series in

Jenna Fischer starred alongside John Krasinski in NBC's 'The Office.'

Halpert and Beesly’s relationship was on the rocks when he accepted a job in Philadelphia in season 9. At the time, a new character Brian was introduced and was a comfort to Beesly while Halpert was gone.

Ellickson, 41, said he planned to have Beesly and Brian “hooking up a little bit.” He added, “Ultimately, I don't think it was about actually going there. They never did anything. It was just to introduce worry in the audience.”

They didn’t move forward with the plan because of Krasinski, now 40, who played Halpert.

JOHN KRASINSKI EXPLAINS DECISION TO SELL 'SOME GOOD NEWS' AFTER BEING CALLED A 'SELLOUT' BY FANS

John Krasinski as Jim Halpert and Steve Carell as Michael Scott. (Getty)

“We had to pull the ripcord on it because it was so painful to the fans of the show. John Krasinski said to me, 'Brent, this final season is for the ultra fans of the show. They're the only ones really still left watching, right? This is for them. Jim and Pam splitting up is too painful for them to sustain all the way to the reunion. We have to get them back together immediately,’” show writer Brent Forrester claimed in the book.

He added: “I was like, 'Wow, we can't allow this beautiful couple to really be like on the verge of divorce. It's too awful for them.'”

JOHN KRASINSKI INVITES 'OFFICE' CO-STAR STEVE CARELL AS FIRST GUEST FOR YOUTUBE SHOW CALLED 'SOME GOOD NEWS'

Rainn Wilson as Dwight Schrute, Angela Kinsey as Angela Martin, John Krasinski as Jim Halpert and Jenna Fischer as Pam Beesly Halpert. (Getty)

During season 6 Halpert and Beesley got married after Beesley found out she was pregnant. Jenna Fischer, who played Beesley, was pregnant in real life during season 8 and her pregnancy was written into the script.

CLICK HERE TO GET THE FOX NEWS APP

When “The Office” ended after 9 seasons, Krasinki and Fischer’s characters were happily married with two kids.