我想更新记录,如果记录存在或插入一个新的,如果不存在。检查存在或发现异常?
什么是最好的方法?
做一个Select Count()
如果返回零然后插入,如果有人然后查询记录,修改和更新,或者我应该只是试图查询记录和捕获任何system.queryexception?
这一切都在Apex中完成,而不是从REST或JS API完成。
我想更新记录,如果记录存在或插入一个新的,如果不存在。检查存在或发现异常?
什么是最好的方法?
做一个Select Count()
如果返回零然后插入,如果有人然后查询记录,修改和更新,或者我应该只是试图查询记录和捕获任何system.queryexception?
这一切都在Apex中完成,而不是从REST或JS API完成。
加入已经在这里说过的内容,您想在这些情况下使用FOR UPDATE来避免superfell指的是什么。所以,
Account theAccount;
Account[] accounts = [SELECT Id FROM Account WHERE Name = 'TEST' LIMIT 1 FOR UPDATE];
if(accounts.size() == 1)
theAccount = accounts[0];
else
theAccount = new Account();
// Make modifications to theAccount, which is either:
// 1. A record-locked account that was selected OR
// 2. A new account that was just created with new Account()
upsert theAccount;
我会用一个列表和isEmpty()
功能试试吧:
List<Account> a = [select id from account where name = 'blaahhhh' Limit 1];
if(a.isEmpty()){
System.debug('#### do insert');
}
else{
System.debug('#### do update');
}
好主意。这种方法的任何理由? –
您应该使用upsert呼叫如果在所有可能的选择,然后插入/更新的方法是有问题的,一旦你进入的同时境界除非你正确地锁定父行作为选择呼叫的一部分。
upsert有什么问题? –