A police sergeant went online and tried to arrange sexually abusing a 10-year-old girl while on his shift.
Disgraced Police Scotland officer Paul Bucknall, who went by the name "purv" online, asked a mum if she would be willing to "share" her daughter.
The 52-year-old also admitted he was sexually attracted to children in a series of messages, some of which were sent from Cathcart police office.
He said in conversations, which took place on website Chat-Avenue, that his name was Paul and gave out his mobile phone number.
But he was actually talking to undercover police officers based in England who reported his offending to officers in Scotland.
An investigation revealed some of the messages had been sent while he was within the Glasgow station and his home in Cumbernauld, Lanarkshire.
Bucknall, who has since resigned from the force and moved to Rotherham, previously pleaded guilty to two charges under the Communications Act 2003.
He admitted repeatedly sending sexual messages that were "grossly offensive or of an indecent, obscene or menacing character".
The offences were committed between September 5 and December 11, 2019.
Don't miss the latest news from around Scotland and beyond - Sign up to our daily newsletter here.
When he returned to the dock at Glasgow Sheriff Court today to be sentenced, defence solicitor Pamela Rodgers said he had already suffered as a result of the offences.
The lawyer said: "He has already lost his wife, three children, a grandchild, and had to resign from Police Scotland.
"He has lost £60,000 in a pension. He certainly has suffered for his actions."
Sheriff Allan McKay said the first offender was a danger to children.
But he said vital background reports prepared ahead of sentencing said it would be better for society for him to be left in the community rather than jailed.
He explained: "Clearly, in terms of an alternative to custody, he needs to be treated, he needs to be helped.
"The offences are of some age now and there's been a passage of time.
"The offences are serious, and they are repeated, and they are over a period of time.
"The location from which they were committed are also of concern to me.
"You are a high risk of causing serious harm to children - we have to address that.
"Finally, the report says that should not be addressed in prison.
"The priority in my mind is to protect society from you."
Sheriff McKay placed Bucknall on a Community Payback Order (CPO) as a direct alternative to custody.
The CPO will see him supervised by social workers for two years and having to complete 225 hours’ unpaid work in 12 months, reduced from 300 hours as he admitted his guilt.
As part of the CPO, he is banned from having unsupervised contact with children under the age of 16.
He was ordered to attend the Moving Forward Making Changes Programme which tries to help sex offenders change their ways.
Bucknall also has to surrender his electronic devices for monitoring and is banned from wiping his internet history.
He was also placed on the Sex Offenders’ Register for two years and told to return to the dock in the new year for a review of his progress.
Police Scotland has been contacted for comment.