CCP §529(a) requires the court to order an undertaking when it grants a preliminary injunction to protect a defendant against losses that may be incurred if the defendant prevails on the merits. But when the court granted the injunction here, it also ruled on the merits of the underlying claim, and, thus, could not order the petitioner to provide an undertaking. Bardasian v. Superior Court (Santa Clara Partners Mortgage Corporation) (Cal. App. Third Dist.; December 15, 2011) 201 Cal.App.4th 1371.