在Salesforce销售组织中插入记录时导致CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY错误
我无法使用以下代码在Salesforce销售组织中插入记录,原因是另一个Apex类(触发器处理程序类):
public class Demo {
// try to insert only one record and fetch Id of the record using system.debug
public static void InsertDemo() {
Account acc= new account();
acc.name = 'Skanda145';
acc.Active__c= 'Yes';
insert acc ;
System.debug('Account Id = ' +acc.Id);
}
}
我收到这个错误:
第10行,第1列
System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AccountTrigger1: execution of BeforeInsert caused by: line 5, column 36: Method does not exist or incorrect signature: void PopulateRating(List, NULL) from the type AccountTriggerhandler1: []
问题在于已经存在的触发器处理程序类不允许插入(触发器处理程序类的名称是 --> AccountTriggerhandler1,代码在此显示)。
请问有人能告诉我应该怎么做来克服这个问题吗?我无法修复。
public class AccountTriggerhandler1 {
public static void updateAccountRecursion(List<account> accList, map<Id,Account> accoldmap) {
List<Account> actToBeUpdated = new List<Account>();
for (Account acc :accList) {
Account a = new Account();
a.id = acc.id;
a.description = 'Test Recursion';
actToBeUpdated.add(a);
}
if (!actToBeUpdated.isEmpty()) {
update actToBeUpdated;
}
}
public static void preventDeletion(List<Account> accList) {
profile p = [Select Id FROM Profile Where name = 'System Administrator'];
for (account acc:accList) {
// if(acc.Active__c == 'Yes') {
if (userInfo.getprofileId()!= p.Id || acc.Active__c == 'Yes') { // only system admin can delete record in this case
//acc.addError('We cant delete active account records');
// acc.addError(Label.Prevent_duplicate_Account); // name of custom label api is wrong here
}
}
}
public static void updateRelatedcontact(List<Account> accList , Map<id,Account> accoldmap) {
List<Contact> conList = new List<Contact>();
//Map<id,Account> accmap = new map<Id,Account> ();
set<Id> accIds = new set<Id>();
for (Account acc:accList) {
//map will store only thoes accounts whose phone is updated.
if (acc.phone !=accoldMap.get(acc.id).phone) {
// accMap.put(acc.Id,acc);
accIds.add(acc.id);
}
}
/* for (contact con: [Select Id, Homephone, AccountId from contact
where AccountId IN : accMap.keyset()]){
if(accMap.containskey(con.accountid)){
con.Homephone = accMap.get(con.accountid).phone;
conList.add(con);
}
} */
for(Account acc: [SELECT ID,Phone,(Select Homephone From contacts)
FROM ACCOUNT Where Id IN : Accids]) {
if(acc.Contacts!=null){
for(contact con:acc.contacts){
con.HomePhone = acc.phone;
conList.add(con);
}
}
}
if(!conList.isempty()){
update conList;
}
}
Public static void PopulateRating(List<Account> accList , Map<id,Account> accoldmap){
for (Account acc:acCList){
if((accoldmap == null && acc.industry !=null && acc.Industry == 'Media') // --> if new rcord insert happen
|| (acc.industry != accoldmap.get(acc.Id).Industry && acc.Industry == 'Media')) // --> if rcord existed it will update
{
acc.Rating='Hot';
}
}
}
public static void updatePhone(List<Account> acclist, map<id,account> accoldmap){
for(Account acc:acclist) {
if(acc.phone != accoldmap.get(acc.Id).phone){
acc.description = ' phone is updated | old value = ' + accoldmap.get(acc.Id).phone +
' New value = ' + acc.phone;
}
}
}
public static void createOpp(List<Account> accList){
List<Opportunity> OppList = new List<Opportunity>();
for (account acc:accList){
opportunity opp = new opportunity();
Opp.Name = acc.name;
Opp.StageName = 'prospecting';
Opp.Amount = 0005;
Opp.CloseDate = system.today();
opp.AccountId=acc.Id;
OppList.add(opp);
}
if(!OppList.isEmpty()){
insert OppList;
}
}
public static void updatedesc(List<Account> accList){
for(Account acc : accList){
acc.Description = 'Account created in march';
}
}
Public static void PopulateRating(List<Account> accList){
for (Account acc:acCList){
if(acc.industry !=null && acc.Industry == 'Media'){
acc.Rating='Hot';
}
}
}
}
解决方案
我觉得插入语句并不是这里真正的问题。插入操作只是让账户触发器被执行,而触发器在处理程序内部就失败了。
错误的关键部分是:
方法不存在或签名不正确。
PopulateRating(List, NULL)
所以我会检查触发器是如何调用PopulateRating的。如果是在插入之前运行,Trigger.oldMap将为null,因此你可能需要改用只有一个参数的版本:
AccountTriggerhandler1.PopulateRating(Trigger.new);
并且只在更新逻辑中使用两个参数的版本,当Trigger.oldMap存在时:
AccountTriggerhandler1.PopulateRating(Trigger.new, Trigger.oldMap);
另外,确保处理程序类是一个独立的Apex类,而不是放在Demo类里面。
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。