Skip to main content

Notifications

Announcements

No record found.

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

CustTable insert() CoC get RecId

(3) ShareShare
ReportReport
Posted on by 88
Hi Community,
 
I'm working on implementing a Power App to replace a HTML/JavaScript control, see https://community.dynamics.com/forums/thread/details/?threadid=22a8446e-91bc-ef11-8ee9-6045bdd80cdd.
 
In doing so, we've decided to store a unique ID of a customer in a new custom field in CustTable so this can be passed on to the Power App. The ID is a concatenation of the RecId and some other information about the customer that the Power App needs. So every time a customer is inserted or updated, we should write to this custom field.
 
A simplified copy of my class is below. The update is fine, but the insert is not. I need to call "next insert()" before writing to "myCustomField" because I need the RecId to exist, but I think that since the CustTable "insert()" method ends with "ttscommit", my code isn't working. I've did the same on SalesTable, and it works. 
 
Could this be the reason, and does anyone have some pointers for me to solve this? 
 
[ExtensionOf(tableStr(CustTable))]
final class AB_CustTable_Table_Extension
{
    void insert(DirPartyType _partyType, Name _name, boolean _updateCRM)
    {
        next insert(_partyType, _name, _updateCRM);

	this.myCustomField = any2str(this.RecId) + "_" + this.someField + "_" + this.anotherField;
    }

    void update(boolean _updateSmmBusRelTable, boolean _updateParty)
    {
	this.myCustomField = any2str(this.RecId) + "_" + this.someField + "_" + this.anotherField;

        next update(_updateSmmBusRelTable, _updateParty);
    }

}
Thanks!
 
BR
Categories:
  • hca Profile Picture
    hca 88 on at
    CustTable insert() CoC get RecId
    Ok, thank you for clarifying that. I can confirm that it works.
  • Martin Dráb Profile Picture
    Martin Dráb 230,646 Most Valuable Professional on at
    CustTable insert() CoC get RecId
    In my opinion, setting the value directly is equally supported scenario as using a binding, therefore I don't see why you should be rejecting one of these solutions and promiting the other.
  • hca Profile Picture
    hca 88 on at
    CustTable insert() CoC get RecId
    Yes, I've tried experimenting. As I wrote in the other thread, it threw me down a rabbit whole with hours of amazing Finance errors :)
     
    As of now, since you warned us against the additional table field, we plan to set EntityId directly from a form event handler instead. The concern is that Finance might do something with that field after we write to it. We don't know what they might do in the future also.
     
    Thanks!
  • Martin Dráb Profile Picture
    Martin Dráb 230,646 Most Valuable Professional on at
    CustTable insert() CoC get RecId
    If I was you, I would simply try it and see if it works, I get an error (which tells me what I'm doing wrong) or so. For example, try a table method method accepting no parameters and returning a string. 
     
    Not everything is well-document, or behaves as documented. Experimenting is a integral part of my work as a developer. :-)
     
    Instead of binding to a data source, you could also set entityId directly from code.
  • hca Profile Picture
    hca 88 on at
    CustTable insert() CoC get RecId
    That's correct, I should be able to use that field, but I don't understand how to use it. I am not sure about the syntax, and when I try to point to a method, Finance doesn't find it. Probably because my syntax is wrong :) It would seem like it's the best option to point to a method that returns the entity ID, but I've yet to find any examples online on how to use it correctly.
  • Martin Dráb Profile Picture
    Martin Dráb 230,646 Most Valuable Professional on at
    CustTable insert() CoC get RecId
    The process I'm talking about is Embed canvas apps from Power Apps.
     
    In your case, you should be able to use Entity ID Method property. You mentioned you could't figure out how to use it, but didn't provide any details.
  • hca Profile Picture
    hca 88 on at
    CustTable insert() CoC get RecId
    This is getting a bit beyond the scope of this thread, but it says "Control name" when you click the drop down in your screenshot. I was under the impression that the available fields are coming from the OData exposed entity, so for Customer it would be the CustCustomerV3Entity. I could be wrong. But writing to the Power App that gets added via personalization via code seems even more complex to me. At the moment I'm working with the "PowerAppsHost" control, which exposes table fields directly. Hopefully we could support both, but it's not a must right now.
     
  • Martin Dráb Profile Picture
    Martin Dráb 230,646 Most Valuable Professional on at
    CustTable insert() CoC get RecId
    When you're adding a Power App to F&O, you're selecting a form control where the context will be taked from (the yellow box):
     
    Note that's a form control, not a table field. It's up to you how to set the value of the control.
  • hca Profile Picture
    hca 88 on at
    CustTable insert() CoC get RecId
    Thank you @Martin Dráb. I was not sure how smart it was to do another save inside the insert method. Something tells me that's bad.
     
    I've also had a feeling that the way we planned to do it has a drawback of the resource cost. The official documentation indicates that it should be possible to bind the control to a method, but I couldn't figure it out. I am able to write to the field directly in code, but I'm concerned that the control is not designed for it and it might cause an issue.
  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 230,646 Most Valuable Professional on at
    CustTable insert() CoC get RecId
    The best approach will be not using such a field at all. It wastes storage and decreases performance. What you provide as a context of a the app is a form control, but it - I'm assuming - doesn't have to be bound to a table field. You could use an bound control and set the value in code, or maybe bind the control to a display method.
     
    Just for completeness, the bug in your insert() method is that you set the value of myCustomField() after the record was already created and you don't save the record. Therefore your code has no effect. If you didn't depend on RecId, you could set the value before saving, i.e. above next insert(). But to access RecId, you'd need to save (update) the record once more (impacting performance even more).

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

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Share Your Event with the Community

Jump start your event engagement! 📢

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,646 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans