You need to sign in to do that
Don't have an account?
vivek@shack.co.in
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,
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,
Sorry my bad
Please apply the semicolon end of the 13th line
All Answers
Write the test class as follows,
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.
@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!
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.
This is giving error on saving this.
Error: Compile Error: unexpected token: 'update' at line 14 column 8
Test execution failed :
External entry point
Sorry my bad
Please apply the semicolon end of the 13th line
This trigger is to get Full name for OwnerID. on Account record.
Owner_2 is a lookup field from account object to user object.
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;
}}
Thanks a ton!