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,