Set diversion for outgoing call

I am trying to set a diversion field for an outgoing call.
I am using the following dial plan:

here is the dial command:

exten => 2,n,ExecIf($["${SYSTEM_RESPONSE}" = "OK"]?Dial(PJSIP/0${FORWARDER:3}@Provider_SIP_Trunk,,b(outgoing-call^headerchange^1)))

here is the context to add the diversion:

exten => headerchange,1,Set(PJSIP_HEADER(add,Diversion)=<sip:[email protected]:5060>\;privacy=off\;screen=no\;reason=unconditional)

the log looks OK:

-- Executing [[email protected]:3] ExecIf("PJSIP/Provider_SIP_Trunk-00000007", "1?Dial(PJSIP/[email protected]_SIP_Trunk,,b(outgoing-call^headerchange^1))") in new stack
-- PJSIP/Provider_SIP_Trunk-00000008 Internal Gosub(outgoing-call,headerchange,1) start
-- Executing [[email protected]:1] Set("PJSIP/Provider_SIP_Trunk-00000008", "PJSIP_HEADER(add,Diversion)=<sip:[email protected]:5060>;privacy=off;screen=no;reason=unconditional") in new stack
[2017-05-20 13:56:25] NOTICE[25350][C-00000004]: app_stack.c:1082 gosub_run: PJSIP/Provider_SIP_Trunk-00000008 Abnormal 'Gosub(outgoing-call,headerchange,1)' exit.  Popping routine return locations.
-- Called PJSIP/[email protected]_SIP_Trunk
-- PJSIP/Provider_SIP_Trunk-00000008 is ringing
-- PJSIP/Provider_SIP_Trunk-00000008 answered PJSIP/Provider_SIP_Trunk-00000007
-- Channel PJSIP/Provider_SIP_Trunk-00000008 joined 'simple_bridge' basic-bridge <d6a2f00f-1a47-4068-9c9d-1763d614c36f>
-- Channel PJSIP/Provider_SIP_Trunk-00000007 joined 'simple_bridge' basic-bridge <d6a2f00f-1a47-4068-9c9d-1763d614c36f>
-- Channel PJSIP/Provider_SIP_Trunk-00000007 left 'simple_bridge' basic-bridge <d6a2f00f-1a47-4068-9c9d-1763d614c36f>
-- Channel PJSIP/Provider_SIP_Trunk-00000008 left 'simple_bridge' basic-bridge <d6a2f00f-1a47-4068-9c9d-1763d614c36f>
  == Spawn extension (a-service, 2, 3) exited non-zero on 'PJSIP/Provider_SIP_Trunk-00000007'

the problem is that the Diversion is not added or updated, the trace contains different diversion.
the added diversion is the one from the other channel of the incoming call before the dial command was executed.

can you please assist to create a new diversion for outgoing call?


The “Popping routine return location” message is telling you that your subroutine is missing a Return statement.

Not an expert on PJSIP_HEADER function, but it is possible that that ‘add’ action will not overwrite an existing header, only add a new one. Try using ‘update’ or ‘remove’ then ‘add’.

Thanks for your replay.
Can you enlarge on the subroutine issue? I didn’t understand this error.

When I tried ro update the existing diversion the function return an error that this field wasn’t added before and cannot be updated.

Any other suggestions?


Subroutines should end with a Return statement:

exten => headerchange,1,Set(PJSIP_HEADER(add,Diversion)=<sip:[email protected]:5060>\;privacy=off\;screen=no\;reason=unconditional)
exten => headerchange,n,Return

I added the return function, but still have the same result in the trace.

here is the log with the return:
– Executing [[email protected]:3] ExecIf(“PJSIP/Provider_SIP_Trunk-00000006”, “1?Dial(PJSIP/[email protected]_SIP_Trunk,b(outgoing-call^headerchange^1))”) in new stack
– PJSIP/Provider_SIP_Trunk-00000007 Internal Gosub(outgoing-call,headerchange,1) start
– Executing [[email protected]:1] Set(“PJSIP/Provider_SIP_Trunk-00000007”, “PJSIP_HEADER(add,Diversion)=sip:[email protected]:5060;privacy=off;screen=no;reason=unconditional”) in new stack
– Executing [[email protected]:2] Return(“PJSIP/Provider_SIP_Trunk-00000007”, “”) in new stack
== Spawn extension (from-pstn, 2, 1) exited non-zero on ‘PJSIP/Provider_SIP_Trunk-00000007’
– PJSIP/Provider_SIP_Trunk-00000007 Internal Gosub(outgoing-call,headerchange,1) complete GOSUB_RETVAL=
– Called PJSIP/[email protected]_SIP_Trunk
– PJSIP/Provider_SIP_Trunk-00000007 is ringing
– PJSIP/Provider_SIP_Trunk-00000007 answered PJSIP/Provider_SIP_Trunk-00000006
– Channel PJSIP/Provider_SIP_Trunk-00000007 joined ‘simple_bridge’ basic-bridge <25972989-4ec1-4cf5-b0dd-ab14c5569d41>
– Channel PJSIP/Provider_SIP_Trunk-00000006 joined ‘simple_bridge’ basic-bridge <25972989-4ec1-4cf5-b0dd-ab14c5569d41>
– Channel PJSIP/Provider_SIP_Trunk-00000006 left ‘simple_bridge’ basic-bridge <25972989-4ec1-4cf5-b0dd-ab14c5569d41>
– Channel PJSIP/Provider_SIP_Trunk-00000007 left ‘simple_bridge’ basic-bridge <25972989-4ec1-4cf5-b0dd-ab14c5569d41>
== Spawn extension (a-service, 2, 3) exited non-zero on ‘PJSIP/Provider_SIP_Trunk-00000006’

the log looks OK, maybe the asterisk overwrite the value from the dial plan?


another update - I tried to remove the diversion first and then to add it, but still have the problem, the original diversion from the incoming call is still exists.


another update…
I tried to add a new field - “TestField” with “testvalue” this works, seems that the function is working, but probably something else is overwrite the diversion.


the problem solved using the REDIRECTING function:

exten => 2,n,Set(REDIRECTING(from-num,i)=0531234567)


1 Like