Handle the IVR with query

Hello guys,
I would like to create an IVR where i get the latest extension number that called a customer, so if the customer didn’t answer to the phone and call back to my company, asterisk forward the
call to the latest extension that have done the latest call to the customer.
I don’t know where to start, can someone help me please?


In your incoming call context,
-Answer the call
-Use Read or Background application to play a message and collect some DTMFs from caller.
-Dial the application

Hello Satish,
can you make me a practical example please?


This is very basic(un-tested) dialplan and doesn’t perform any validation on user’s input. I suggest you study those applications and some other basic Asterisk Dialplan stuff

Assuming your incoming call context is [incoming]

exten => _X.,1,Answer()
same => n,Read(extensionEntered,audioFile)
same => n,Dial(SIP/${extensionEntered})

I interpret the OPs task as “when an agent calls a customer and the customer doesn’t answer, save the agent extension and the customer phone number in a database so if the customer calls back, the call can be routed to the agent who placed the last call to the customer.”

If correct, please show us what you have done so far (extensions.conf) and where it fails (console log) so we can suggest specific solutions.

Maybe you’re right sedwards.

His use of some words like this created the confusion.

Hello sedwards,
yes is correct, for now I’ve only the CDR but I don’t know how can procede…I don’t have much experience in asterisk :frowning:
In my mind I would like to do the following steps:

  1. when a call arrive I want to store in a variable1 the caller number (example 234xxxxxx),
  2. make a query in the CDR
    SELECT * FROM call_log
    WHERE number_dialed LIKE ‘234xxxxxx’
    ORDER BY start_time DESC LIMIT 1
  3. from the result, store the extension in a variable2
  4. connect the caller number to the variable2
  5. if is not reachable forward the call to a specific group.
    Can you help me please?


Well, yes, and no.

We believe in the ‘teach a man to fish…’ approach where ‘teach’ means ‘offer suggestions to help guide you’, but asking for ‘a practical example’ is unlikely to serve you in the long term.

What you see on this list is all volunteering. Digium may have employees who’s job covers monitoring this list, but they’re here to keep the ball rolling, not hand out ‘work product.’

It sounds like you would profit from a bit of reading and experimentation. I suggest reading through relevant chapters of ’ Asterisk: The Future of Telephony’ and trying some of the concepts.

What you are asking for is not difficult. I’d focus on topics like writing dialplan and database access.

My personal bias is that database access belongs in an AGI, but your needs can also be satisfied using the ODBC method. There are examples of both, just a google away.

1 Like

Indeed, we’re not here to do everything for everyone and make complete solutions. Asterisk is a toolkit, we just shed light on things in that regard.

Thanks, I will check for the AGI method because I tried with odbc without success.

Well, this is new information :smiley:

This implies you have a dialplan and a database.

Did you follow a ‘working’ example off the 'net?

Maybe your ODBC issue is as simple as understanding quoting or configuration.

Maybe if you post your dialplan snippet, ODBC configuration, and a console log, someone can offer a useful suggestion.

Hello sedward,
thanks for your answer, I’ve configure the following files:


Driver = ODBC Driver 17 for SQL Server
Trace = Yes
Server =
Port = 1433
database = data
UserName = as
Password = xxxxx

readsql=select top 1 extension from call_log where number_dialed like ‘%${E}%’ order by start_time desc

exten => *700,1,NoOp(find inbound)
exten => *700,n,Set(E={CALLERID(ani)}) exten => *700,n,Set(number={ODBC_sqlserver({E})}) exten => *300,n,Dial(SIP/{number})
exten => *300,n,Hangup

Out of asterisk I tested the ODBC and it works, the error that I receive is Function ODBC_sqlserver not registered.
Any idea?


You named the entry in func_odbc “find”. The dialplan function would therefore be named ODBC_find and not ODBC_sqlserver.

Thank you very much jcolp

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.