Prepend digits to outbound DID

I have an issue where I have to prepend 6 digits to my extensions for a certain SIP. So if I’m calling from 2579 I need 700417 to be added in front of that to be sent out the trunk so the other side sees my DID as 7004172579. How can this be accomplished? All I see are adding numbers to dialed numbers not the number you’re dialing from. Any help would be appreciated. Thank you in Advance.

There are three places where you can set the caller ID. Have you thought about setting the outbound caller ID in the extension and then honoring the extension caller ID by not overriding in the outbound route and trunk?

By the way, DID stands for “Direct Inbound Dial” or “… Digits”. It’s your inbound number, so there no such thing as an outbound DID.

1 Like

Thanks for the response. what I need probably will have to be done through a custom context then I guess. The trunk I have setup is set to intracompany so that it only gets the 4 digit extension that the call is coming from and I was hoping there was a way to add 6 digits in front of that extension number.

Set the caller ID on the extension with all 10 digits and see if that works.

It should.

I can’t do that. I already have a CID set on the extension. I need it to be something different to go out a certain trunk. I need all calls coming from any extension going out a specific SIP trunk to have 6 digits added in front of the extension.

in the outbound route there is a caller id field that you can use. so for example, if the caller id is 2579 you can prepend the other 6 digits before sending it on to the trunk

so if I put the six digits in the caller ID field on the outbound route it will add those digits to the extension number? I tried that and it didn’t work that way. Is there a specific way to enter the digits in the field? Like in brackets or something?

no - you put the extension number in the caller id field and put the 6 digits in the prepend field. when ever that caller id is used to make an outbound call, the 6 digits get prepended.

we use this all the time for customers that have multiple offices that have different numbers.

remember i am referring to the outbound route, dial patterns section.

Right but that only prepends the dialed number not the number you’re dialing from.

i guess the only way to do what you want is to have an outbound route per extension and set the cid in the outbound route. you would still use the dial pattern in the outbound route to ensure that only that extension uses that route. pretty messy if you have a lot of extensions. you probably also have to have the user dial an extra digit - like 9 or 8 so that the outbound route only picks up and modifies the cid on calls that need to have the 6 digits prepended since i think from what you say some trunks will use the cid from the extension setting.
if you have a ton of extensions or cannot or will not have the user dial an extra digit, you will need to build something custom

1 Like

would that be a custom context in the extension.conf file?

Thank you both for responding and trying to help, I really appreciate it.

Hi LTAdmin.
Did you ever found a solution to this?
I also need to prepend a digit to my extensions caller ID when it goes out a trunk to another office that uses the same extension numbering plan (1XX). So when I call them I want to appear as 41XX instead of 1XX.

I know this is an old thread, but i can help you with the CLID modification.
Is it just one office? or do you have similar changes to do with multiple offices

Hi Dickson,
It’s actually two offices but if you can help with at least one that will be highly appreciated.


Are your interoffice extensions set to (presumably) “intra-company” in the outbound route settings?
Also, is it a certain range of extensions that need to be applied?

IE, 4000-4500 = prepend 1234
4501-4800 = prepend 5678
3000-3999 = prepend 99909

Or are there extensions that are all over the map?
4001 & 4005 = 1234
4002 - 4004 = 4322

Both of our offices use 1XX extensions. I would like when I call them to send 41XX instead of 1XX as my extension.
When they receive the call they should see that it is comming from extension 41XX.

After discussion with Igaetz about the location and syntax of file, here is the updated instructions. Original post is below the # # # symbols below using his recommendations

Open up /etc/asterisk/extensions_custom.conf

Insert this code below (presumably this context doesn’t exist in some for already)

exten => s,1,Noop(Entering user defined context [macro-dialout-trunk-predial-hook] in extensions_custom.conf)
exten => s,n,ExecIf($[${LEN(${DIAL_NUMBER})}=3]?Set(CALLERID(all)=${CALLERID(Name)} <4${AMPUSER}>))
exten => s,n,MacroExitexten => s,n,MacroExit

Save the changes, reload your config through GUI or CLI.
TEST! :slight_smile:

When you make a call, the system should (based on testing I’ve done) look numbers dialed that are 3 digits in length, then add a 4 in front of them.

This line can be modified to suit your environment. Change the “=3” to the number of digits you want to match.
Change the “4” to be any number(s) of digits you want to precede your caller ID.
exten => s,n,ExecIf($[${LEN(${DIAL_NUMBER})}=3]?Set(CALLERID(all)=${CALLERID(Name)} <4${AMPUSER}>))

If you run the code and you are having a problem, just put a “;” (semicolon) in front of the above line and save/reload your config and it will bypass this code.

I’ve tested it, seems to work just fine!

# # # # # # # OLD CODE # # # # #

Modify outbound CallerID for extensions that are 3 digit in length

Tested in FreePBX distro 10.13.66-64bit

Copy the entire [macro-outbound-callerid] context from /etc/asterisk/extensions_additional.conf

and paste it into /etc/asterisk/extensions_override_freepbx.conf

_(someone can write some code to shoe horn this into custom context if they want, override is here for just this sort of thing)

Now scroll down about 10 lines and you’ll see these two lines of code:

exten => s,n(normcid),Set(USEROUTCID=${DB(AMPUSER/${AMPUSER}/outboundcid)})
exten => s,n(bypass),Set(EMERGENCY=${DB(DEVICE/${REALCALLERID}/emergency_cid)})

Insert this line between them:
exten => s,n,Gosub(CIDcheck,s,1)

Go to the bottom of the code just pasted, paste the code between the ===== symbols at the very bottom of the file.

exten => s,1,Set(FOOclid=${CALLERID(all)})
exten => s,n,set(FOOname=${CUT(FOOclid,<,1)})
exten => s,n,set(FOOnumber1=${CUT(FOOclid,<,2)})
exten => s,n,set(FOOnumber=${CUT(FOOnumber1,>,1)})
exten => s,n,ExecIf($[${LEN(${FOOnumber})} = 3]?Set(USEROUTCID=${FOOname}<4${FOOnumber}>))
exten => s,n,Return

SAVE your changes and reload the configuration.
You can use “core reload” in the CLI, or do a save/submit in the GUI to force the reload.

Make a test call. It should now reflect the new CLID.

If you need to disable this modification
open the extensions_override_freepbx.conf
and put a ; in front of it [macro-outbound-callerid]


Save the change and reload the configuration.
This will put the context back to normal and you can review.

We copy the current CLID to a temporary one (FOO) to pull it apart and reassemble with new callerid number if required
exten => s,1,Set(FOOclid=${CALLERID(all)})

Get the callerid NAME
exten => s,n,set(FOOname=${CUT(FOOclid,<,1)})

Get the callerid NUMBER (between the <> symbols)
exten => s,n,set(FOOnumber1=${CUT(FOOclid,<,2)})
exten => s,n,set(FOOnumber=${CUT(FOOnumber1,>,1)})

If the extension is 3 digits in length ("= 3") then put a “4” in front of the number and resemble the callerID with the new number. Modify these two values that suit your system.
exten => s,n,ExecIf($[${LEN(${FOOnumber})} = 3]?Set(USEROUTCID=${FOOname}<4{FOOnumber}>))
exten => s,n,Return


include => macro-outbound-callerid-custom
exten => s,1,ExecIf($["${CALLINGNAMEPRES_SV}" != “”]?Set(CALLERPRES(name-pres)=${CALLINGNAMEPRES_SV}))
exten =>
exten => s,n,ExecIf($["${CALLINGNUMPRES_SV}" != “”]?Set(CALLERPRES(num-pres)=${CALLINGNUMPRES_SV}))
exten => s,n,ExecIf($["${REALCALLERIDNUM:1:2}" = “”]?Set(REALCALLERIDNUM=${CALLERID(number)}))
exten => s,n(start),GotoIf($[ $["${REALCALLERIDNUM}" = “”] | $["${KEEPCID}" != “TRUE”] | $["${OUTKEEPCID_${ARG1}}" = “on”] ]?normcid)
exten => s,n,GotoIf($[“foo${DB(AMPUSER/${REALCALLERIDNUM}/device)}” = “foo”]?bypass)
exten => s,n(normcid),Set(USEROUTCID=${DB(AMPUSER/${AMPUSER}/outboundcid)})
exten => s,n,Gosub(CIDcheck,s,1)
exten => s,n(bypass),Set(EMERGENCYCID=${DB(DEVICE/${REALCALLERIDNUM}/emergency_cid)})
exten => s,n,Set(TRUNKOUTCID=${OUTCID_${ARG1}})
exten => s,n,GotoIf($["${EMERGENCYROUTE:1:2}" = “” | “${EMERGENCYCID:1:2}” = “”]?trunkcid)
exten => s,n,Set(CALLERID(all)=${EMERGENCYCID})
exten => s,n,Set(CDR(outbound_cnum)=${CALLERID(num)})
exten => s,n,Set(CDR(outbound_cnam)=${CALLERID(name)})
exten => s,n(exit),MacroExit()
exten => s,n(trunkcid),ExecIf($[${LEN(${TRUNKOUTCID})} != 0]?Set(CALLERID(all)=${TRUNKOUTCID}))
exten => s,n(usercid),ExecIf($[${LEN(${USEROUTCID})} != 0]?Set(CALLERID(all)=${USEROUTCID}))
exten => s,n,ExecIf($[${LEN(${TRUNKCIDOVERRIDE})} != 0 | ${LEN(${FORCEDOUTCID_${ARG1}})} != 0]?Set(CALLERID(all)=${IF($[${LEN(${FORCEDOUTCID_${ARG1}})}=0]?${TRUNKCIDOVERRIDE}:${FORCEDOUTCID_${ARG1}})}))
exten => s,n(hidecid),ExecIf($["${CALLERID(name)}"=“hidden”]?Set(CALLERPRES(name-pres)=prohib_passed_screen))
exten => s,n,ExecIf($["${CALLERID(name)}"=“hidden”]?Set(CALLERPRES(num-pres)=prohib_passed_screen))
exten => s,n,Set(CDR(outbound_cnum)=${CALLERID(num)})
exten => s,n,Set(CDR(outbound_cnam)=${CALLERID(name)})

;–== end of [macro-outbound-callerid] ==–;

exten => s,1,Set(FOOclid=${CALLERID(all)})
exten => s,n,set(FOOname=${CUT(FOOclid,<,1)})
exten => s,n,set(FOOnumber1=${CUT(FOOclid,<,2)})
exten => s,n,set(FOOnumber=${CUT(FOOnumber1,>,1)})
exten => s,n,ExecIf($[${LEN(${FOOnumber})} = 3]?Set(USEROUTCID=${FOOname}<4${FOOnumber}>))
exten => s,n,Return

As always, using the override file is to be avoided if possible. A better solution would be to populate extensions_custom.conf with:

exten => s,1,Noop(Entering user defined context [macro-dialout-trunk-predial-hook] in extensions_custom.conf)
exten => s,n,ExecIf($[${LEN(${CALLERID(number)})}=3]?Set(CALLERID(number)=4${CALLERID(number)}))
exten => s,n,MacroExit
1 Like

Yea, your ExecIF is much more efficient My code is bad and I feel bad. In my defense, its origin was from old code that I knew worked.
I will stop with solutions that use the _override file and code for custom context from now on. You’ve beat it into me finally. :slight_smile:

1 Like