Hi,
Is there a way to omit + , -, and brackets when sending an SMS? Because the same number is used in Sales Invoices and other documents. It looks weird if I type the mobile number as 11457289470 instead of +1 (145) 728-9470.
The SMS gateway APIs and even WhatsApp API do not entertain these characters. I have spoken to my provider and they are saying it needs to be removed on ERPNext before sending.
Would like to know the solution to this.
Thanks.
Hi,
- Go to the Customize Form page for the document where you want to use the custom phone number field (e.g., Sales Invoice).
- Add a new field of type “Data” and set the label to “Phone Number”.
- In the “Options” section of the field settings, set “Validate” to “Phone”.
- Save the changes to the form.
- Go to the Customize Script page for the same document.
- Add a new script and set the “Script Type” to “Client”.
- In the script, use the following code to remove the special characters from the phone number:
frappe.ui.form.on('Sales Invoice', {
refresh: function(frm) {
// Get the phone number field value
var phone_number = frm.doc.phone_number;
// Remove the special characters
phone_number = phone_number.replace(/[^\d]/g, '');
// Update the phone number field value
frm.set_value('phone_number', phone_number);
}
});
Hope this will help you out.
Thank You.
Hi,
Thanks for responding. I am talking about the standard fields in Contact and Address DocType.
Will it work there? Also, I want it to remove the charecters only when sending SMSs but show them while printing or viewing PDF.
Regards
Hi,
To only remove the characters when sending SMS and show them while printing or viewing PDF, you can modify the code as follows:
frappe.ui.form.on('Contact', { validate: function(frm) { // Get the phone number field value var phone_number = frm.doc.phone;
// Remove the special characters if SMS is being sent
if(frm.doc.__islocal && frm.doc.communication_medium === 'SMS') {
phone_number = phone_number.replace(/[^\d]/g, '');
}
// Update the phone number field value
frm.set_value('phone', phone_number);
},
refresh: function(frm) {
// Get the phone number field value
var phone_number = frm.doc.phone;
// Add the special characters if SMS is not being sent
if(!frm.doc.__islocal || frm.doc.communication_medium !== 'SMS') {
phone_number = format_phone_number(phone_number);
}
// Update the phone number field value
frm.set_value('phone', phone_number);
}
});
Hope this will help you out.
Thank you.
Hi,
I tried to add ±() in the code replace(/[^\d]/g, ‘’); but it is not working. Please let me know how to add these characters.
Thanks.
Hi,
Make this change in you code and try it.
phone_number = phone_number.replace(/[^\d±()\s]/g, '');
Hope this will help you out.
Thank you.
Hi @non-polar,
Add on @VINOTH answer, if the @VINOTH scenario does not work then apply it.
phone_number = phone_number.replace(/\D+/g, "");
Thank You!
Hi,
I tried your line also. But it is not working. Also, you do not have brackets in it.
Thanks for the help.
Hi,
I tried it. Its not working. I am trying to find out what the SMS provider is receiving from ERP Next. Shall let you know when they revert.
Thanks.
Okay.
Hi,
I just spoke with them. They are saying they do not receive anything till the time correct info is sent through the API. Which means if the number is not in the desired format, they will not get to know what they have received.
Also, another thing I want to mention is that I am going on the Sales Invoice> Send SMS and then changing the mobile number to my personal number to check. With your code, we are trying to change the number parameters in the Contact form. Is there a way to specifically target the event of sending the SMS?
Hope you have understood my query.
Thanks.
Hi,
You can try the below code:
frappe.ui.form.on("Sales Invoice", {
refresh: function(frm) {
frm.cscript.send_sms = function() {
// Get the phone number field value
var phone_number = frm.doc.mobile_no;
// Remove the special characters if SMS is being sent
if(frm.doc.communication_medium === 'SMS') {
phone_number = phone_number.replace(/[^\d±()]/g, '');
}
// Update the phone number field value
frm.set_value('mobile_no', phone_number);
// Call the original send_sms function to send the SMS
frm.cscript._super.send_sms.call(cur_frm);
}
}
});
Hope this will help you out.
Thank you.
Hi,
I updated the Sales Invoice script and when I clicked on Send SMS, a blue “Update” button appeared, and I got the following message:
Heading - “Cannot Update After Submit”
Body - " Not allowed to change Mobile No after submission from +xxxxxxxxxx to"
One more thing, the code you have given is only for Sales Invoices. I would like to send SMS on various events like payment reminders, PO creation etc. Can we have this script for all types of SMSs? As I said earlier, can a script target the event of sending the SMS?
Thanks again.
Hi,
The error message you got means that you are trying to change a field (mobile_no
) that has already been submitted in the Sales Invoice document. To avoid this, you can change the script to only update the mobile_no
field if the document has not been submitted yet. You can check the document status by using the docstatus
property of the frm.doc
object.
For your second question, you can use the same method to send SMS on different events like payment reminders, PO creation, etc. You will need to create a separate script for each event and use the appropriate event trigger, such as on_submit
for payment reminders or on_update
for PO creation. To target the event of sending the SMS, you need to use a custom function in your script that gets called when the user clicks the Send SMS button.
For Example for PO:
frappe.ui.form.on("Purchase Order", {
refresh: function(frm) {
frm.add_custom_button(__('Send SMS'), function() {
// Get the phone number field value
var phone_number = frm.doc.mobile_no;
// Remove the special characters if SMS is being sent
if(frm.doc.communication_medium === 'SMS') {
phone_number = phone_number.replace(/[^\d±()]/g, '');
}
// Send the SMS using an API
frappe.call({
method: 'myapp.utils.send_sms',
args: {
phone_number: phone_number,
message: 'Your PO has been created'
},
callback: function(response) {
if (response.message === 'success') {
frappe.show_alert({
message: __('SMS sent successfully'),
indicator: 'green'
});
} else {
frappe.show_alert({
message: __('Failed to send SMS'),
indicator: 'red'
});
}
}
});
});
},
on_submit: function(frm) {
// Get the phone number field value
var phone_number = frm.doc.mobile_no;
// Remove the special characters if SMS is being sent
if(frm.doc.communication_medium === 'SMS') {
phone_number = phone_number.replace(/[^\d±()]/g, '');
}
// Send the SMS using an API
frappe.call({
method: 'myapp.utils.send_sms',
args: {
phone_number: phone_number,
message: 'Your PO has been approved'
},
callback: function(response) {
if (response.message === 'success') {
frappe.show_alert({
message: __('SMS sent successfully'),
indicator: 'green'
});
} else {
frappe.show_alert({
message: __('Failed to send SMS'),
indicator: 'red'
});
}
}
});
}
});
Hope this will help you out.
Thank you.
Hi again!
I understood what the error meant. The problem is that I do not send SMSs before submitting the documents.
Creating a different Client script for every DocType will be cumbersome and will require me to change the code in every script if there are any changes.
I am still looking for a ‘universal’ solution to this problem where all the SMSs go through only one script.
Do let me know if you come across a solution of this kind.
Thanks a lot for the help!