Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Answered

Csharp update ax table

Posted on by 1,217
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
  • YA TEH Profile Picture
    YA TEH 1,217 on at
    Csharp update ax table
    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
  • Verified answer
    Mohamed Amine Mahmoudi Profile Picture
    Mohamed Amine Mahmoudi 9,559 Super User 2024 Season 2 on at
    Csharp update ax table
    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
  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 230,214 Most Valuable Professional on at
    Csharp update ax table
    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.
  • YA TEH Profile Picture
    YA TEH 1,217 on at
    Csharp update ax table
    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.
  • Martin Dráb Profile Picture
    Martin Dráb 230,214 Most Valuable Professional on at
    Csharp update ax table
    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?
  • YA TEH Profile Picture
    YA TEH 1,217 on at
    Csharp update ax table
    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
    Martin Dráb 230,214 Most Valuable Professional on at
    Csharp update ax table
    I'm not sure, but the first thing I would try is utilizing methods myParameter.ttsbegin() and myParameter.ttscommit().
  • YA TEH Profile Picture
    YA TEH 1,217 on at
    Csharp update ax table
    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
    Martin Dráb 230,214 Most Valuable Professional on at
    Csharp update ax table
    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.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans