web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Csharp update ax table

(1) ShareShare
ReportReport
Posted on by 1,243
Hi All,
 
I had created a dll with C# managed code. The dll managed to add into AOS reference and I'm writing a simple job to call the C# method. So far no issue executing.
 
However, in my C# managed code I was trying to update the ax table (this is newly created custom table) after getting result from external application, but it hit error /Cannot edit record, Update must be performed inside a transaction/. I tried to change the custom table name to CustTable and hardcoded some value, it can update successfully. I did a comparison between my custom table and CustTable, there is no much differences.
 
What could be the root cause? My AX version is AX 2012 R3 CU13.
 
Below is the sample code. myToken object is declared in another class
 
public async Task<string> LoginAsTaxPayer(string _dataAreaId){                        MYParameter mYParameter = new MYParameter();                        mYParameter = MYParameter.findByComp(_dataAreaId,true);                                    if (mYParameter.Found)            {                                                using (var client = new HttpClient())                {                    client.BaseAddress = new Uri(https:/// + mYParameter.BaseURL + //connect/token/);                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(/application/x-www-form-urlencoded/));                    var form = new Dictionary<string, string>                    {                        {/client_id/, mYParameter.ClientID},                        {/client_secret/, mYParameter.ClientSecret},                        {/grant_type/, /client_credentials/},                        {/scope/,// }                    };                                        var response = await client.PostAsync(client.BaseAddress, new FormUrlEncodedContent(form));                    if (response.IsSuccessStatusCode)                    {                        string respBody=response.Content.ReadAsStringAsync().Result;                        Token mytoken = JsonConvert.DeserializeObject<Token>(respBody);                        mYParameter.AccessToken = mytoken.AccessToken;                                                                        mYParameter.Update();                                                                        return response.StatusCode.ToString();                    }                    else                    {                        string respBody = response.Content.ReadAsStringAsync().Result;                        ErrResponse errResp = JsonConvert.DeserializeObject<ErrResponse>(respBody);                        return errResp.Error_Descr;                    }                }                                            }            else            {                return /No Parameter found/;            }        }
Any advice is much appreciated. 
 
Thanks.
 
Regards,
Teh
I have the same question (0)
  • Martin Dráb Profile Picture
    240,002 Most Valuable Professional on at
    Can you please post your code once more in a reply? This site has a bug that distorts code formatting in the original post, but it works in replies.
  • Venedict Profile Picture
    1,243 on at
    Hi Martin,
     
    here you go.
     
    public  string LoginAsTaxPayerSync(string _dataAreaId)
            {            
                
                MYParameter myParameter = new MYParameter();            
    
                myParameter = MYParameter.findByComp(_dataAreaId);
                myParameter.ForUpdate(true)
                
                if (ads_EInvcParameter.Found)
                {
                    using (var client = new HttpClient())
                    {
             
                        var request = new HttpRequestMessage(HttpMethod.Post, "https://" + myParameter.BaseURL + "/connect/token");
    
                        var form = new Dictionary<string, string>
                        {
                            {"client_id", myParameter.ClientID},
                            {"client_secret", myParameter.ClientSecret},
                            {"grant_type", "client_credentials"},
                            {"scope","InvoicingAPI" }
                        };                    
    
                        var content = new FormUrlEncodedContent(form);
                        request.Content = content;                    
                        var response = client.SendAsync(request).Result;
    
                        response.EnsureSuccessStatusCode();
    
                        if (response.IsSuccessStatusCode)
                        {
                            string respBody = response.Content.ReadAsStringAsync().Result;
                            Token mytoken = JsonConvert.DeserializeObject<Token>(respBody);
    
                           
                            myParameter.AccessToken = mytoken.AccessToken;
                            myParameter.AccessTokenExpiration = mytoken.ExpiresIn;
                            myParameter.TokenExpirationDt = DateTime.UtcNow.AddSeconds(mytoken.ExpiresIn);
    
                            myParameter.Update();
                            
                            return response.StatusCode.ToString();
                        }
                        else
                        {
                            string respBody = response.Content.ReadAsStringAsync().Result;
                            ErrResponse errResp = JsonConvert.DeserializeObject<ErrResponse>(respBody);
                            return errResp.Error_Descr;                        
                        }
                    }
                }
                else
                {
                    return ("No Parameter found");
                }
            }
    I had changed the code a little bit. Originally I was thinking to update the myparameter table from C#.
  • Martin Dráb Profile Picture
    240,002 Most Valuable Professional on at
    I'm not sure, but the first thing I would try is utilizing methods myParameter.ttsbegin() and myParameter.ttscommit().
  • Venedict Profile Picture
    1,243 on at
    Hi Martin,
     
    I thought the ttsbegin and ttscommit method is auto appear after I added myParameter into C# project. But there is no ttsbegin and ttscommit method available on my custom table.
     
    I tried added CustTable in C# project, also cannot find ttsbegin and ttscommit method. But, no issue to use CustTable.Update() method. Puzzling me.
     
     
     
  • Martin Dráb Profile Picture
    240,002 Most Valuable Professional on at
    All tables have these methods; they are inherited from the parent class. But maybe the proxies don't expose them. Did you try using them? Maybe you don't see them in IntelliSense but the code will compile.
     
    Do you have update() method overridden?
  • Venedict Profile Picture
    1,243 on at
    Hi Martin,
     
    I just tested to put myParameter.ttsbegin() and myParameter.ttscommit(), but build failed. Error saying that no definition for ttsbegin and ttscommit.
     
    No, there is no overriden of update() method.
  • Verified answer
    Martin Dráb Profile Picture
    240,002 Most Valuable Professional on at
    Then the best approach may be doing the update in an X++ method and calling this method from C#, rather then doing it in C# directly.
  • Verified answer
    Mohamed Amine Mahmoudi Profile Picture
    26,809 Super User 2026 Season 1 on at
    Hi @Ya,
     
    i think the best way is to retern the values from C# method to Ax and then do the update through the x++ code.
    e.g.
    YourClass  yourClass;
    YourTable  yourTable;
    ;
    
    ..
    ttsBegin;
    yourTable.field = yourClass.field;
    yourTable.Update();
    ttsCommit;
    Best regards,
    Mohamed Amine MAHMOUDI
  • Venedict Profile Picture
    1,243 on at
    Hi Martin, MAHMOUDI,
     
    Ya, seem like I got no choices to have the result return to X++ and update from X++ method.
     
    Thanks for help. Will try that direction.

    Regards,
    Teh

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the June Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 433 Super User 2026 Season 1

#2
Subra Profile Picture

Subra 382

#3
Martin Dráb Profile Picture

Martin Dráb 279 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans