External script not changing properties

Discussion in 'Call Flow Designer' started by Trestill, Sep 27, 2017.

Thread Status:
Not open for further replies.
  1. Trestill

    Joined:
    Jan 22, 2015
    Messages:
    43
    Likes Received:
    1
    Hello,

    I'm experimenting with changing Ext parameters with CFD. If I'm not mistaken I need to use external script, right?
    So what I got right now:
    Code:
    using TCX.Configuration;
    
    namespace mynamespace
    {
        public class myclass
        {
            public string mymethod(string ext, string CID)
            {
                Extension ext2change = (Extension) PhoneSystem.Root.GetDNByNumber(ext);
                string oldCID = ext2change.OutboundCallerID;
                
                //ext2change.OutboundCallerID = CID;
                ext2change.SetProperty("OutboundCallerID", CID);
                
    
                return oldCID;
            }
    
        }
    }
    
    In CFD I send session.ani as ext and printing return value to the text file.
    As you can see I tried to set OutboundCallerID in two different ways. Both failed.
    I read CID correctly but can't change the value. What am I missing?

    Best Regards
     
  2. edossantos

    edossantos Support Team
    Staff Member 3CX Support

    Joined:
    Jun 27, 2007
    Messages:
    1,260
    Likes Received:
    97
    Hi @Trestill

    The second approach is not valid:
    ext2change.SetProperty("OutboundCallerID", CID);

    That will not work, as there is no property for that DN with that name. The OutboundCallerID is a C# property of the Extension object, but not a property in the properties table... So your first approach is the way to go.

    Also, please note that you need to invoke:
    ext2change.Save()

    after doing any change, in order to apply them. That's pretty sure what you're missing.

    Kind regards.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. Trestill

    Joined:
    Jan 22, 2015
    Messages:
    43
    Likes Received:
    1
    Yes, saving changes helps.

    Thank you very much
     
  4. Trestill

    Joined:
    Jan 22, 2015
    Messages:
    43
    Likes Received:
    1
    Hi @edossantos
    Next step in my little project and new questions. My script looks liks this:
    Code:
    using TCX.Configuration;
    using System.Data.SqlClient;
    using System;
    
    namespace mynamespace
    {
        public class myclass
        {
            public void mymethod(string ext)
            {
                Extension ext2change = (Extension) PhoneSystem.Root.GetDNByNumber(ext);
                int i = 0;              
                string connetionString = "Data Source=xx.xx;Initial Catalog=xxxx_0000004;User ID=xxx_0000004;Password=xxx";
               
                using (SqlConnection cnn = new SqlConnection(connetionString))
                {
                    SqlDataReader rdr = null;
                    Random rnd = new Random();
               
                    string query = "SELECT cid FROM CIDs WHERE ext=" + ext + " AND last=0" ;
                   
                    SqlCommand cmd  = new SqlCommand(query, cnn);
                    cnn.Open();
                    rdr = cmd.ExecuteReader();
                    string[] result = new string[20];
                    while (rdr.Read())
                    {
                        result[i++] = rdr[0].ToString();
                    }
                    rdr.Close();              
               
                    string newCID = result[rnd.Next(0, i)];
                    string update = "UPDATE CIDs SET last=0 WHERE ext=" + ext + " AND last=1;UPDATE CIDs SET last=1 WHERE ext=" + ext + " AND cid=" + newCID;
                    SqlCommand cmd2 = new SqlCommand(update, cnn);
                    cmd2.ExecuteNonQuery();
                   
                    ext2change.OutboundCallerID = newCID;
                    ext2change.Save();
                }
               
                return ;
            }
    
        }
    }
    
    but calling from ext to queue results in:

    Code:
    17.09.29 15:04:33.307|100030| Err|10|0014|: PlugIn[random_cid - Callflow - MainFlow - CallID GDBJXIECPQXW] ERROR: Error executing last component: System.Reflection.TargetInvocationException: Exception has been thrown by t$
       at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, Boolean redirectedUserInstance, SqlConnectionString userConnectionO$
       at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionO$
       at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
       at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
       at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
       at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbCon$
       at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
       at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& c$
       at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
       at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
       at System.Data.SqlClient.SqlConnection.Open()
       at mynamespace.myclass.mymethod(String ext)
       --- End of inner exception stack trace ---
       at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
       at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
       at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
       at random_cid.ExternalCodeExecutionComponent.executeStart(QMExtendAPI iface, String id)
       at random_cid.ExternalCodeExecutionComponent.Start(QMExtendAPI iface, CallQueue callQueue, QueueCall queueCall, ActiveConnection activeConnection, TimerManager timerManager, Dictionary`2 variableMap, TempWavFileManager$
       at random_cid.Callflow.ProcessStart()
    
    Now the weird part: the same script run as console app works (prints chosen CID and change DB). If I remove cmd2.ExecuteNonQuery(); CFD works too.

    Is this CFD limitation? Do I have to split this in two scripts?

    Regards,
    Trestill
     
    #4 Trestill, Sep 29, 2017
    Last edited: Sep 29, 2017
  5. edossantos

    edossantos Support Team
    Staff Member 3CX Support

    Joined:
    Jun 27, 2007
    Messages:
    1,260
    Likes Received:
    97
    Hi @Trestill,

    Maybe your console app is using .NET Framework, while 3CX uses .NET Core. Even if the interfaces are the same and you can use both with the same code, internally the implementation is different, so that might explain why you're getting the error when running the code from the CFD and not from the console app.

    However, from the stack trace, I can see that the problem seems to be related to the connection being opened. Maybe with the code you've created, it tries to open the connection twice... Maybe you can try changing:
    SqlCommand cmd2 = new SqlCommand(update, cnn);

    With this:
    SqlCommand cmd2 = cnn.CreateCommand();
    cmd2.CommandText = update;
    cmd2.CommandType = CommandType.Text;

    Please give it a try and let me know. Alse, ensure that your console app is a .NET Core 1.0 app, so you test exactly the same.

    Regards.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
Thread Status:
Not open for further replies.