• ondrejb
  • NEWBIE
  • 0 Points
  • Member since 2012

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies

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);
          
       
        }
        */
       
       
    }
}

 

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);
          
       
        }
        */
       
       
    }
}