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
ondrejbondrejb 

Need Help with Apex Test Class

I want to change owner of Custom Object Book according Lookup(Account)
Apex Class and  Triger are working fine in development mode, however stragling with Apex Test. It's comming with error complaining about invalid Id for Account__c which is lookup field on Book object to Account Object.
Tkank you for any advise how to move on.

Trigger

trigger BookDealerTrigger on Book__c (after insert, after update) {
    Book__c[] books = Trigger.new;
    BookDealer.changeOwner(books);
}

 



Apex Class

public class BookDealer {
 public static void changeOwner(Book__c[] books) {
   for (Book__c b :books){
   
       if(b.Account__c != ''){
           Account a = [SELECT Name FROM Account WHERE ID = :b.Account__c LIMIT 1];
     
          if(a.Name == 'Grand Hotels Resorts Ltd'){
              b.OwnerID = '00590000000YeiV'; //user1
          }elseif(a.Name == 'Something else'){
              b.OwnerID = '00590000000xxx'; //user2
          }//etc
       }
   }
}
}

 




Apex Test

@isTest
private class BookDealerTestClass {
   
    static testMethod void validateBookDealer(){
       
        Account a = new Account(Name='Grand Hotels Resorts Ltd');
        insert a;
       
        // Tell Salesforce that we are starting our test now
  test.startTest();
       
        Book__c b = new Book__c(Name='Behind the Cloud', Price__c=100, Account__c=a.Id);
        System.debug('Creating new book: ' + b.Price__c);
       
        //Insert book
        insert b;
       
        test.stopTest();
       
        /*
        if(b.Account__c== '00190000003NVJx'){
            b.OwnerID = '00590000000YeiV'; //User1
        }
       
        if(a.Name=='Grand Hotels & Resorts Ltd'){
       
            //Test that the trigger correctly updated the owner according account lookup
            System.assertEquals('00590000000YeiV', b.OwnerId);
          
       
        }
        */
       
       
    }
}

 

Neeraj_ChauriyaNeeraj_Chauriya

Hi,

 

The possible reason for the error is the use of hardcoded Ids, as you are using hardcoded record Ids, which is not a reccommended practice for Apex.

 

In the test context, system is unable to find the record with the associated hardcoded Ids.

 

If you want to use existing data of the org in test context then you shoud use @isTest(seeAllData = true) annotation.

 

So, avoid using the hardcoded Ids, instead I would suggest you to create required test data before calling the trigger or apex class and use the Ids of test records.

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other's benefit.

ondrejbondrejb

Hi Neeraj, 

 

thank you for reply I'll try to not use hardcoded Ids.

 

This is error message when I run ApexTest.  Would you mind to explain me why Force is complaing about Id, when I've created new account beforehead and inserting Id of that Account?

 

System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, BookDealerTrigger: execution of AfterInsert
caused by: System.StringException: Invalid id: 

Neeraj_ChauriyaNeeraj_Chauriya

Hi ondrejb,

 

The reason why the exception for Invalid Id occurred is, system is unable to locate a record with the specified hardcoded Id. If you want to use org data in you test class then use the annotation @isTest(seeAllData = true), this will allow you to use the existing record from the org.

 

And use of hardcoded Ids in Apex is not a good practice because, By hardcoding the record Id in the test method, the test method will fail in every other org where this code is deployed becuase the record with specified Account Id won't exist there.

 

The simpler solution to your problem is that, create test record in the test method and use Ids of those record wherever you want to use. The data records created in test context are not committed to database.

 

I hope you got some idea!