Passing account number to CDR?

With Freepbx the new distribution, in the CDR I see fields “userfield” and “account”

What I am trying to do is have it so when I dial a number

1402XXXXXXX that I can also pass an account number and have it show up in the CDR - by either dialing more digits or some other method

Is it possible to do this? Each record we work has an account number and I would like to use this to tie back to CDR to the account we are on.


Anyone have any ideas on how to pass a number through to the CDR? We need to tie the calls to an account number.


There are two user fields in the CDR DB that you could use for this purpose.

Thanks- but how would you pass information to them?

You would have to write a custom context that you route the call through. In that custom context you would load the user field with the desired data and then pass control back to FreePBX.

You would also have to either modify the standard reporting module to display the user fields or use an external reporting tool.

While Scott is correct, within the FreePBX GUI, you can more easily set the accountcode in the individual extension setup page, which are reported in current FreePBii

Try this until someone kindly confirms that it’s kosher (I’m not into this sort of thing but I was intrigued because it’s a very useful feature - am sure any legal firm would use it extensively):

Assuming a “77” prefix in not inconsistent with your dialplans, group codes or such and assuming your account codes are 4 digits long - neither are carved in stone, just my example.

Further assuming you can grab hold of an easy way to edit a file (I use WINSCP on a windows terminal but whatever makes you comfortable).

Edit /etc/asterisk/extensions-custom.conf
to insert the following lines:

exten => _77XXXXXX.!,1,Noop(Modifying account code on the fly)
exten => _77XXXXXX.!,n,Set(CDR(accountcode)=${EXTEN:2:4})
exten => _77XXXXXX.!,n,Goto(outbound-allroutes,${EXTEN:6:15},1)

Notice, you’re intercepting from ext-group context (because the dial string failed everything else and then sending back to outbound-allroutes in extensions.conf to start parsing again.

Now save the file and restart Asterisk (if you can - otherwise someone else will chip in with a less dramatic way)

The length of the account code is in the ${EXTEN:2:4} (so after the 77 ‘code’ you have accountcode length 4) and then you have your outgoing number. This will insert your accountcode in the CDR - you could also define the dialstring more specifically as in _77XXXX1XXXXXXXXXX if you only expect to be dialling 1 then 10 digit numbers - might make it faster but I preferred to allow for full parsing by your dialplan subsequently.

This was (almost entirely) extracted from where there are pointers to inserting the account code to a billing system by inserting the code into the SIP header - another interesting concept.

The reason I say someone has to verify its veracity is that like I said above: I am NOT an expert in this and don’t know what it might affect - I was just curious and spent a good twenty minutes to setup and try it (then another ten or so to write this spiel) - works for me for any destination but I don’t have a legal office. I’d be interested to find out if this can be inserted somehow in the product because of the ultimate uses it can provide.

Try it and see.

oops… my thirty minutes wasted??

I think my solution just spells it out - would appreciate if you could check it out (lest someone get in trouble over something I thought would help).

Finally, just a note:
You can preserve the account code programmed for the extension (while inserting your customer/account/business code by modifying the (third) line to

exten => _77XXXXXX.!,n,Set(CDR(accountcode)=${EXTEN:2:4}-${CDR(accountcode)})

This broke my “dial plans”.
I have 7777 simulating an incoming call… so basically I had to also create a route that pointed nowhere just so I can capture this pattern.

I ended up creating a route (called it “capture-acctcode” and pointed it to a trunk I never use - you can point it anywhere as long as you know this route is not going to be used.

Again, find what the name of that route is in the extensions-additional.conf file (something like outrt-34 or so) and then go to your extensions-custom.conf file and modify the ext-group-custom to outrt-35-custom.

I will try this out and see if I can get it to work. The solution of having this in the dialstring would work perfectly for my situation

Just to document what I’ve proposed in one message so someone may verify it’s correctness:

Create an outbound route that uses any trunk (doesn’t matter much which because it will never be used) - label it something clear like "dummy-inserting-acctcodes"
In the outbound route, creat the dialplan to reflect what you allow to dial (minimum is 77XXXXXX.). Here you may choose to either define an actual pattern or as an example “77[0-689]XXXXX.” Make sure you have enough minimum Xs to insert a code and then a further 2 Xs and a period. Again, the pattern might be 77[0-689]XXX1XXXXXXXXXX (for four digit account code) but this would restrict you to US numbers. Essentially you need to make sure that whatever you define after the account code can be handled by your dial plan and that the initial part does NOT satisfy any dial patterns you actually use (hence my not allowing 777X).

After you submit Changes and Apply Config you can
First read extensions-additional.conf to find the entry that shows the new route… search for [outrt- and in all probability the last entered outbound route will show first - that’s what you’re looking for… say it is [outrt-17]

edit /etc/asterisk/extensions-custom.conf

exten => _77[0-689]XXXXX.!,1,Noop(insert accnt code on the fly)
exten => _77[0-689]XXXXX.!,n,Set(CDR(accountcode)=${EXTEN:2:4}-${CDR(accountcode)})
exten => _77[0-689]XXXXX.!,n,Goto(outbound-allroutes,${EXTEN:6:15},1)
exten => h,1,Hangup()

Here this would cater for any dial pattern after the 4 digit account code (please note account codes here cannot begin with a 7 - you can obviously allow for a different setup but mind the rest of your dialplan). I have also allowed retaining the phone-configured account code in this string (your judgement if you want this or not)- if you enter 77123401197317543700 (as an example to call me) your account code will show 1234-extension acct code and the number 01197317543700 will be sent back and re-parsed to use your overseas calls provider (which perhaps would process the number further before sending).

Again, please note after processing these lines, you are thrown back at the top of the outbound-allroutes which will re-parse the remaining portion of your dialstring to send you to the correct trunk.

Save the file (you might be prompted to enter your password) and in Asterisk’s CLI type
core restart when convenient
which will kick you out of Asterisk but will restart it.

type Asterisk -r -vvvvvvvvvvv (to reenter Asterisk)
or just enter it from the web browser and

core set verbose 30

Now you can test.

Please also test all dialling situations from various devices, did(s) and disa(s) while watching your CLI - this is to ensure that you can see if it is failing.
Some of your extensions might choke on dialling a number beginning with 77 so you would probably have to solve these problems.

I used what you said and modified it to do what I needed it to do.

include => outrt-1-custom
exten => _1NXXNXXXXXX.,1,Macro(user-callerid,LIMIT,)
exten => _1NXXNXXXXXX.,n,Set(CDR(accountcode)=${EXTEN:11:10}-${CDR(accountcode)})
exten => _1NXXNXXXXXX.,n,Set(CDR(userfield)=${EXTEN:1:10}-${CDR(userfield)})
exten => _1NXXNXXXXXX.,n,Set(MOHCLASS=${IF($["${MOHCLASS}"=""]?default:${MOHCLASS})})
exten => _1NXXNXXXXXX.,n,ExecIf($["${KEEPCID}"!=“TRUE” & ${LEN(${DB(AMPUSER/${AMPUSER}/outboundcid)})}=0 & ${LEN(${TRUNKCIDOVERRIDE})}=0]?Set(TRUNKCIDOVERRIDE=1))
exten => _1NXXNXXXXXX.,n,Set(_NODEST=)
exten => _1NXXNXXXXXX.,n,Gosub(sub-record-check,s,1(out,${EXTEN},))
exten => _1NXXNXXXXXX.,n,Macro(dialout-trunk,2,${EXTEN:1:11},)
exten => _1NXXNXXXXXX.,n,Macro(outisbusy,)

When I dial through xlite 14022555555 for example, with an account code of 12345, I would dial the whole thing- 1402255555512345

I used a . on the end of the string so the account code can be any length

I put the phone number into userfield, and I put the account code into the account field.

Both show up on the CDR, the recording works fine :slight_smile:

Thanks for all of your help and ideas on this!