Send SMS on creation of a lead

Hello,
I have completed my sms settings.
What i want to do is - A sms should be sent when a new lead is created.
I have used “after_save” as a trigger. But because of this the sms is sent everytime save is clicked.
Can anyone suggest what should be the solution to this ?

i am still facing this issue, can someone please help.

Have you tried the “after_insert” trigger? I haven’t tested it recently, but my understanding is that it triggers only on new document creation.

I believe after_insert should as mentioned by @peterg
Just for reference, here are hooks that you could evaluate to best choose for your use case:

@peterg @fountainhead79 Thanks for the reply. Applied the mentioned trigger but this time the message is not delivered even once.

That’s strange. Could you test out on a new doc and include this email example from the docs too just to verify. It sounds like the problem is related to your messaging code.

class Person(Document):
    def validate(self):
        if self.age > 60:
            frappe.throw('Age must be less than 60')

    def after_insert(self):
        frappe.sendmail(recipients=[self.email], message="Thank you for registering!")

Add a custom flag(boolean), say “sms_sent” to your Doctype and set it to 1 once sms is sent. Always check in “after_save” if this value is not equal to 1, only then send the sms. This should work, we have been able to implement this successfully and send out sms on creation of delivery note(s).