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

Notifications

Announcements

No record found.

Community site session details

Community site session details

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

Is this the correct way of creating method FindOrCreate for a table ?

(0) ShareShare
ReportReport
Posted on by 596

Hi folks,

Want to ask some advice here. I have class which will need to search a record in my custom table, and the logic is if cannot find the record then insert, if found, I will update some fields.

If lets I have table A, then I create this method in the table : 

static public AgentData findOrCreateAgentData(tst_AgentId _AgentId, 
                                           TransDate _startDate,    
                                           TransDate _endDate, 
                                           TransDate _checkDate)
{
        TST_AgentData  tstAgentData;
        
    
        select firstonly tstAgentData where tstAgentData.AgentID == _AgentId;
        ttsbegin;
        if (tstAgentData)
        {
            tstAgentData.selectForUpdate(true);
            tstAgentData.FromDate = _startDate;
            tstAgentData.ToDate = _endDate;
            tstAgentData.TransactionDate = _checkDate;
            tstAgentData.doUpdate();
        }
        else
        {
            TST_AgentData  tstAgentDataInsert;
            tstAgentDataInsert.clear();
            tstAgentDataInsert.AgentId = _AgentId;
            tstAgentDataInsert.FromDate = _startDate;
            tstAgentDataInsert.ToDate = _endDate;
            tstAgentDataInsert.TransactionDate = _checkDate;
            tstAgentDataInsert.doInsert();
            tstAgentData = tstAgentDataInsert;
        }
        ttscommit;

        return tstAgentData;
}

I'm wondering whether this is correct, or according to Best practice, especially the way I'm declare new variable for the table just for insert, when I already declare the table variable for searching.

And in the class whereby I will use that table's method, I'm creating this method : 

protected void FindOrCreateAgentHistory(tst_agentId _agentId, TransDate _startDate, TransDate _endDate, TransDate _checkDate)
{
        TST_AgentData tstagentData;
        
        tstagentData = TST_AgentData::findOrCreateagentData(_agentId, _startDate, _endDate, _checkDate);
}

which then in this same class, I will call the method: 

this.FindOrCreateAgentHistory(agentId, fromDate, toDate, transDate); 

The source of the parameter is another process in the class, obviously, but I just wondering the way of doing the FindOrCreate method is correct or not.

Any comment will be appreciated.

Thanks,

I have the same question (0)
  • Suggested answer
    Mohit Rampal Profile Picture
    12,565 Moderator on at

    Hi Lars, You can refer findOrCreate method in InventDim table. The best practice is to reuse standard code or code structure, let us know if any issues.

  • Voltes Profile Picture
    596 on at

    Hi Mohit,

    Yes actually I want to follow that, but it is too complex for me. I saw it is using class after class, using hash, userConnection and all, which I don't know what, why or how to use it or apply it in my own.

    So I'm come up with this one, which what I understand as of now.

    I'm not a pure AX developer, btw, just having a basic skill of this development thing, so that's why I need some opinion about the code and the way I'm doing it.

    Thanks,

  • Verified answer
    Martin Dráb Profile Picture
    237,884 Most Valuable Professional on at

    I would do quite a few changes:

    • Implement and use find() method.
    • Remove code duplication.
    • Remove the extra variable.
    • Select the record for update in a transaction. This is not really needed with optimistic concurrency control, but I prefer supporting the other case too.
    • Don't use doInsert() and doUpdate(). It could skip important business logic.
    • Validate the data before saving. For example, you may want to check whether start date isn't higher than end date.
    • Consider using write() instead of insert() and update().

    For example:

    public static AgentData findOrCreateAgentData(
    	Tst_AgentId _agentId, 
    	TransDate _startDate,    
    	TransDate _endDate, 
    	TransDate _checkDate)
    {
    	ttsbegin;
    
    	TST_AgentData tstAgentData == TST_AgentData::find(_agentId, true);
    	
    	if (!tstAgentData)
    	{
    		tstAgentData.AgentId = _agentId;
    	}
    	
    	tstAgentData.FromDate = _startDate;
    	tstAgentData.ToDate = _endDate;
    	tstAgentData.TransactionDate = _checkDate;
    	
    	if (!tstAgentData.validateWrite())
    	{
    		throw error(...);
    	}
    	
    	tstAgentData.write();
    
    	ttscommit;
    
    	return tstAgentData;
    }

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
Martin Dráb Profile Picture

Martin Dráb 544 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 450 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 250 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans