function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RRRizeRRRize 

Writing test class problem - CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, - please help

I am trying to create a test class for a trigger in my org called UpdateAccount and failing miserably.  I'm totally new to Apex (and coding in general) so any help would be appreciated.

Let me start with the error I am getting:

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, UpdateAccount: execution of AfterInsert caused by: System.DmlException: Update failed. First exception on row 0; first error: MISSING_ARGUMENT, Id not specified in an update call: [] Trigger.UpdateAccount: line 8, column 9: []


Here is the test class I wrote:

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@isTest
private with sharing class TestUpdateAccount {

    static testMethod void testAccountChange(){
    
        
        Opportunity oppty = new Opportunity(Sales_Representative__c = '00570000001FDA3',
        Name = 'iPad Mania', Brand__c = 'a0OS0000002TTo6', Account=[select Name from Account where Id = '001S000000QxQpJ'], Budget_High__c = 2000,
        Amount = 200, StageName = '10% - Proposed', CloseDate = date.today(), Phase__c = 'Proposal/RFP',
        Platforms_Included__c = 'Digital', Description = 'Digital opportunity', Estimated_Campaign_Start_Date__c = date.today(), Estimated_Campaign_End_Date__c = date.today());
       
       
        Test.startTest();
        
      insert oppty;
                
       List<Account> accounts = [select Id, dclk__Advertiser_ID__c from Account where Id = '001S000000QxQpJ'];
       if(accounts != null){
           for(Account account:accounts){
               System.assert(account.dclk__Advertiser_ID__c=='7777777');
         }
       }
    
        Test.stopTest();
        
        delete oppty;
           
    
    }

}

 

Here is the trigger (which works fine by the way):

 

1
2
3
4
5
6
7
8
9
10
trigger UpdateAccount on Opportunity (after insert) {
    Map<ID, Account> updateAccountMap = new Map<ID, Account>();
    for (Opportunity oppty : [select Id, AccountId, DART_Advertiser_ID__c from Opportunity where Id in :Trigger.new order by CreatedDate]) {
        updateAccountMap.put(oppty.AccountId, new Account(Id = oppty.AccountId, dclk__Advertiser_ID__c = oppty.DART_Advertiser_ID__c));
    }

    if (!updateAccountMap.isEmpty()) {
        update updateAccountMap.values();
    }
}

 

Can anyone tell me what I'm doing wrong in the test class? Apologies for the sloppyness of it.  Thank you for any help you might bring my way! : )

Ritesh AswaneyRitesh Aswaney

try AccountId rather than Account

 

 Opportunity oppty = new Opportunity(Sales_Representative__c = '00570000001FDA3',
        Name = 'iPad Mania', Brand__c = 'a0OS0000002TTo6', AccountID=[select Name from Account where Id = '001S000000QxQpJ']. Id, Budget_High__c = 2000,

 

Also, if I were you, I'd b creating a new Account, rather than relying on existing data, coz potentially the data present will vary between environments.

RRRizeRRRize

I actually tried that at one point, but I get the following error:

 

Error: Compile Error: Invalid initial expression type for field Opportunity.AccountId, expecting: Id at line 8 column 70

 


Ritesh AswaneyRitesh Aswaney
Reckon you didn't use the .id on the soql query as I've indicated in my post AccountID=[select id, Name from Account where Id = '001S000000QxQpJ']. Id, Again I'd urge you to create test data rather than relying on an existing Account