Get mobile nos for customer contact (#8674)

diff --git a/erpnext/public/js/sms_manager.js b/erpnext/public/js/sms_manager.js
index a06c43c..d6293ea 100644
--- a/erpnext/public/js/sms_manager.js
+++ b/erpnext/public/js/sms_manager.js
@@ -21,9 +21,9 @@
 		}
 
 		if (in_list(['Quotation', 'Sales Order', 'Delivery Note', 'Sales Invoice'], doc.doctype))
-			this.show(doc.contact_person, 'customer', doc.customer, '', default_msg[doc.doctype]);
+			this.show(doc.contact_person, 'Customer', doc.customer, '', default_msg[doc.doctype]);
 		else if (in_list(['Purchase Order', 'Purchase Receipt'], doc.doctype))
-			this.show(doc.contact_person, 'supplier', doc.supplier, '', default_msg[doc.doctype]);
+			this.show(doc.contact_person, 'Supplier', doc.supplier, '', default_msg[doc.doctype]);
 		else if (doc.doctype == 'Lead')
 			this.show('', '', '', doc.mobile_no, default_msg[doc.doctype]);
 		else if (doc.doctype == 'Opportunity')
@@ -33,13 +33,13 @@
 
 	};
 
-	this.get_contact_number = function(contact, key, value) {
+	this.get_contact_number = function(contact, ref_doctype, ref_name) {
 		frappe.call({
 			method: "erpnext.setup.doctype.sms_settings.sms_settings.get_contact_number",
 			args: {
-				contact_name:contact,
-				value:value,
-				key:key
+				contact_name: contact,
+				ref_doctype: ref_doctype,
+				ref_name: ref_name
 			},
 			callback: function(r) {
 				if(r.exc) { msgprint(r.exc); return; }
@@ -49,13 +49,13 @@
 		});
 	};
 
-	this.show = function(contact, key, value, mobile_nos, message) {
+	this.show = function(contact, ref_doctype, ref_name, mobile_nos, message) {
 		this.message = message;
 		if (mobile_nos) {
 			me.number = mobile_nos;
 			me.show_dialog();
 		} else if (contact){
-			this.get_contact_number(contact, key, value)
+			this.get_contact_number(contact, ref_doctype, ref_name)
 		} else {
 			me.show_dialog();
 		}
diff --git a/erpnext/setup/doctype/sms_settings/sms_settings.py b/erpnext/setup/doctype/sms_settings/sms_settings.py
index 2888942..a8b59be 100644
--- a/erpnext/setup/doctype/sms_settings/sms_settings.py
+++ b/erpnext/setup/doctype/sms_settings/sms_settings.py
@@ -40,10 +40,15 @@
 	return sender_name
 
 @frappe.whitelist()
-def get_contact_number(contact_name, value, key):
+def get_contact_number(contact_name, ref_doctype, ref_name):
 	"returns mobile number of the contact"
-	number = frappe.db.sql("""select mobile_no, phone from tabContact where name=%s and %s=%s""" %
-		('%s', frappe.db.escape(key), '%s'), (contact_name, value))
+	number = frappe.db.sql("""select mobile_no, phone from tabContact 
+		where name=%s 
+			and exists(
+				select name from `tabDynamic Link` where link_doctype=%s and link_name=%s
+			)
+	""", (contact_name, ref_doctype, ref_name))
+	
 	return number and (number[0][0] or number[0][1]) or ''
 
 @frappe.whitelist()