After Treaty of Ganja, Nadir Shah appointed a certain Galem from Gilan as a sultan of Baku, also awarded Ashur khan Afshar with lands in Absheron peninsula, including Sabunchu, Keshla and Zabrat.
[9] Khanates of Karabakh, Shaki, Baku, Salyan and Sultanate of Gabala trying to keep their independence, invaded Shamakhi in alliance, forcing Hajji Muhammad to swear that he won't submit to Afsharids.
In order to escape newly crowned Shahrukh Afshar's invasion, he sent his ambassador Haji Fehim to Astrakhan, to ask for Russian protection.
[10] Next year, another threat from Persia - Muhammad Hasan khan landed in what is today Azerbaijan.
As a further precaution, he employed a naval company under the leadership of John Elton, now having 3 warships and 3 merchant ships at his disposal.
He soon acknowledged suzerainty of Fatali khan of Guba and started to join his military campaigns, such as capturing of Muji castle.
He cemented this alliance by marrying his son to Fatali khan's sister, after when he was practically absent from politics.