• Sarah S
  • NEWBIE
  • 20 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 4
    Replies
Hello
I am getting below error while running the test class.

System.DmlException: Insert failed. First exception on row 0 with id 0010I00002TQaLoQAL; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]


My trigger is-
---------------------

trigger NewAccountTrigger on Account (before insert, before update, before delete, after insert, after update, after delete, after undelete) {
    
    if(Trigger.isBefore){
        if(Trigger.isInsert){
        //call the handler method BEFORE INSERT
        NewAccountTriggerHandler.beforeInsert(Trigger.new);  
        }
        else if(Trigger.isUpdate){
              //call the handler method BEFORE UPDATE   
             NewAccountTriggerHandler.beforeUpdate(Trigger.new);  
            }
        else if(Trigger.isDelete){
                //call the handler method BEFORE DELETE
                NewAccountTriggerHandler.beforeDelete(trigger.old);
    }
        }
    if(Trigger.isAfter){
 }
}

My Trigger Handler Class-
--------------------------------------
public class NewAccountTriggerHandler {
    
    public static void beforeInsert(list<Account> accs){
      for (Account a : accs){
                a.Nick_Name__c = 'FromBeforeinsertHandlerClass';
            }       
    } 
    
    public static void beforeUpdate(list<Account> accs){
   //Do something here      
    } 
    
    public static void beforeDelete(list<Account> accs){
    //do something here
                }    
    } 

My test class is -
-------------------------
@isTest
private class NewTestAccountTriggerHandler {
    @isTest static void TestBeforeInsert() {
        // Test data setup
        list<account>accountlist = new list <account>();
        account acct1 = new account();
        acct1.name = 'Trigger test 1';
        accountlist.add (acct1);
        insert  accountlist ;
        
        // Perform test
        Test.startTest();
        
        Database.SaveResult[] result = Database.insert(accountlist, true);

        Test.stopTest();
        // Verify 
        System.assert(result[0].isSuccess());
        System.assertEquals(accountlist[0].Nick_Name__c, 'FromBeforeinsertHandlerClass');
        
    }
    
}
 
Hello,
Please help me, how do I call below class from the anonymous window. I am not finding a way to pass IDs as parameter.
I want pass record Ids of Account for example this 0010I00001hMhFIQA0. 
___________________________ 
public class AccountClass {
    public static void CountMyContacts(Set<ID> recordIds){
    List<Account> accs = [Select Id, Name from Account Where Id IN :recordIds];
    for (Account a : accs){
    List<Contact> Cons = [Select Id, Name from Contact Where AccountId = :a.Id];
        integer ConCount = Cons.size();
        system.debug( Account.Name + 'account has' + ConCount + 'number of contacts');
       }
    }            
}
Hello
I am getting below error while running the test class.

System.DmlException: Insert failed. First exception on row 0 with id 0010I00002TQaLoQAL; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]


My trigger is-
---------------------

trigger NewAccountTrigger on Account (before insert, before update, before delete, after insert, after update, after delete, after undelete) {
    
    if(Trigger.isBefore){
        if(Trigger.isInsert){
        //call the handler method BEFORE INSERT
        NewAccountTriggerHandler.beforeInsert(Trigger.new);  
        }
        else if(Trigger.isUpdate){
              //call the handler method BEFORE UPDATE   
             NewAccountTriggerHandler.beforeUpdate(Trigger.new);  
            }
        else if(Trigger.isDelete){
                //call the handler method BEFORE DELETE
                NewAccountTriggerHandler.beforeDelete(trigger.old);
    }
        }
    if(Trigger.isAfter){
 }
}

My Trigger Handler Class-
--------------------------------------
public class NewAccountTriggerHandler {
    
    public static void beforeInsert(list<Account> accs){
      for (Account a : accs){
                a.Nick_Name__c = 'FromBeforeinsertHandlerClass';
            }       
    } 
    
    public static void beforeUpdate(list<Account> accs){
   //Do something here      
    } 
    
    public static void beforeDelete(list<Account> accs){
    //do something here
                }    
    } 

My test class is -
-------------------------
@isTest
private class NewTestAccountTriggerHandler {
    @isTest static void TestBeforeInsert() {
        // Test data setup
        list<account>accountlist = new list <account>();
        account acct1 = new account();
        acct1.name = 'Trigger test 1';
        accountlist.add (acct1);
        insert  accountlist ;
        
        // Perform test
        Test.startTest();
        
        Database.SaveResult[] result = Database.insert(accountlist, true);

        Test.stopTest();
        // Verify 
        System.assert(result[0].isSuccess());
        System.assertEquals(accountlist[0].Nick_Name__c, 'FromBeforeinsertHandlerClass');
        
    }
    
}
 
Hello,
Please help me, how do I call below class from the anonymous window. I am not finding a way to pass IDs as parameter.
I want pass record Ids of Account for example this 0010I00001hMhFIQA0. 
___________________________ 
public class AccountClass {
    public static void CountMyContacts(Set<ID> recordIds){
    List<Account> accs = [Select Id, Name from Account Where Id IN :recordIds];
    for (Account a : accs){
    List<Contact> Cons = [Select Id, Name from Contact Where AccountId = :a.Id];
        integer ConCount = Cons.size();
        system.debug( Account.Name + 'account has' + ConCount + 'number of contacts');
       }
    }            
}
Hi guys,
i recently completed 100% of the admin beginner trail, but salesforce still didn't add it up to my trail's count on my profile, what should i do? thank you