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
vivek@shack.co.invivek@shack.co.in 

Test Method for Trigger


Hi,

I am still in process of learning APEX coding. However I need to do a Trigger urgently.

I wrote trigger with the help of a blog and it works great. Now, I need Test Method to deploy it in production.

Trigger:

trigger ownerCopy on Account(Before Insert, Before Update) {

    // handle arbitrary number of opps
    for(Account x : Trigger.New){

        // check that owner is a user (not a queue)
        if( ((String)x.OwnerId).substring(0,3) == '005' ){
            x.Owner_2__c = x.OwnerId;
        }
        else{
            // in case of Queue we clear out our copy field
            x.Owner_2__c = null;
        }
    }

}

I am trying myself using this documentation, however any help will be greatly appretiated.

Regards,
Best Answer chosen by Admin (Salesforce Developers) 
Chamil MadusankaChamil Madusanka

Sorry my bad

 

Please apply the semicolon end of the 13th line

 

a2.Name = 'Account One Edited';
update a2;

All Answers

Chamil MadusankaChamil Madusanka

Write the test class as follows,

 

@isTest
private class OwnerCopyTrigger
{
	public static testmethod void testInsertAccount()
	{
		//Write the code for Insert a Account Record
		Account a1 = new Account();
		a1.Name = 'Account One';
		insert a1;
		
		//Then update the record
		Account a2 = [SELECT id, Name FROM Account WHERE id =: a1.Id];
		a2.Name = 'Account One Edited'
		update a2;
	}
}

 If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

alok29novalok29nov

@isTest public With sharing  class TestOwnerCopy

{    

   static testMethod void ideaBatchTest() 

   {   

 

Account acc1 = new Account(Name='Test1', OwnerId='005P0000000cYkP' );       

insert acc1;

Account acc2 = new Account(Name='Test2', OwnerId='005P0000000crxP' );       

insert acc2;

 

Set<id> ids= new set<id>();       

 ids.add(acc1.ownerid);       

 ids.add(acc2.ownerid);       

             List<User> usr= [Select name, id from User where id in:ids];       

 

           acc1.ownerid='005P0000000crxP';       

           update acc1;       

           acc2.ownerid='005P0000000cYkP';       

            update acc2;

}

}

 

After this, Go to Apex Test Execution, and select this class and run it and after that check the test coverage of your trigger.

 

If you still face any issue, let me know.

 

If this resolve your issue, please mark it as a solution.

 

Thanks!

 

Navatar_DbSupNavatar_DbSup

Hi,
Try the below code as reference of test method:


@isTest


private class testTriggerinsert_Contact_Activity_1
{
public static testMethod void unitTestinsert_Contact_Activity()
{
Profile p = [select id from profile where name='System Administrator'];
User u1 = new User(alias = 'standt2', email='standarduser@test12.com',
emailencodingkey='UTF-8', lastname='Testing1', languagelocalekey='en_US',
localesidkey='en_US', profileid = p.Id,firstname='Heather',
timezonesidkey='America/Los_Angeles', username='standarduser@test12.com');
insert u1;
account temp=new account(Name ='ashish testing',Owner_2__c=u1.id);
insert temp;


}
}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

vivek@shack.co.invivek@shack.co.in

This is giving error on saving this.

 


Error: Compile Error: unexpected token: 'update' at line 14 column 8 

vivek@shack.co.invivek@shack.co.in

Test execution failed :

 

Apex Test Result Detail 
Time Started1/11/2012 1:56 AM
ClassTestOwnerCopy
Method NameideaBatchTest
Pass/FailFail
Error MessageSystem.DmlException: Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: [Owner_2__c]
Stack TraceClass.TestOwnerCopy.ideaBatchTest: line 7, column 1
External entry point
Chamil MadusankaChamil Madusanka

Sorry my bad

 

Please apply the semicolon end of the 13th line

 

a2.Name = 'Account One Edited';
update a2;
This was selected as the best answer
vivek@shack.co.invivek@shack.co.in

This trigger is to get Full name for OwnerID. on Account record.

 

Owner_2 is a lookup field from account object to user object.

 

 

alok29novalok29nov

Ok, try this..This should work.

 

@isTest public With sharing  class TestOwnerCopy

{   

   static testMethod void ideaBatchTest()

   { 

   List<Account>  ac1= new List<Account>();

  Set<ID> id2 = new Set<id>();   

  Id1= [Select Id,Name from User Where UserName='Joan' Limit 1] ; //Assign usermane  name of an owner of any account

   for( User u: Id1)        

     {  id2.add(u.id); }

 

for (id4:id2){

Account acc1 = new Account(Name='Test1', OwnerId=id4 );      

Account acc2 = new Account(Name='Test2', OwnerId=id4 ); 

ac1.add(acc1);

ac1.add(acc2);

}

insert ac1;

 

}}

 

 

 

 

 

 

 

 

   

vivek@shack.co.invivek@shack.co.in

Thanks a ton!