+ Start a Discussion
cambart14cambart14 

Apex Text Class

We have the below APEX trigger written in our sandbox, but need to deploy it into production.  The issue is we don't have any code coverage, and I am new to APEX programming.

 

Any help on creating a text class to cover 75% of this code would be much appreciated.

 

	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_Copy__c = x.OwnerId;
        }
        else{
            // in case of Queue we clear out our copy field
            x.Owner_Copy__c = null;
        }
    }

}

 Thanks again for your time and input!

Best Answer chosen by Admin (Salesforce Developers) 
cambart14cambart14

Kiran, thanks for your comments on queue ownership, I was able to update the trigger for that. 

 

I was able to get this test class below to cover 75% of my trigger.

 

@isTest private class Test_account { 

public static testMethod void testPersonAccountCreation() {
        // Find any person account record type
        RecordType recordType = [ select Id, Name, DeveloperName from RecordType where SObjectType = 'Account' limit 1 ];
        system.debug( recordType );

        Account newAccount = new Account( name = 'Mr. Joe Schmoe' );
        insert newAccount;
        Contact contact = new Contact ( Salutation='Mr.', FirstName='Joe', LastName='Schmoe', AccountId=newAccount.id );
        insert contact;

        newAccount.RecordTypeId = recordType.id;
        update newAccount;
    }
    
}

 

All Answers

kiranmutturukiranmutturu

try this.... but here u need to understand some points

 

1. we can't create queues for account object so there is no chance of assiging a queue to account record and also there will be no case where a record in salesforce is not having any owner. SO i dont think else part required there in the code . Any ways if no value assigned to any of the fields in the salesforce it will default to null only. 

 

@isTest
private class TestUtil {


static void createTestAccounts() {

Account objAccount = new Account();
ObjAccount.Name = 'test account';
objAccount.ownerid = userinfo.getuserid();
//Assign all the values which are required
Test.starttest();
insert objAccount;
Test.stoptest();
}

 

}

cambart14cambart14

Kiran, thanks for your comments on queue ownership, I was able to update the trigger for that. 

 

I was able to get this test class below to cover 75% of my trigger.

 

@isTest private class Test_account { 

public static testMethod void testPersonAccountCreation() {
        // Find any person account record type
        RecordType recordType = [ select Id, Name, DeveloperName from RecordType where SObjectType = 'Account' limit 1 ];
        system.debug( recordType );

        Account newAccount = new Account( name = 'Mr. Joe Schmoe' );
        insert newAccount;
        Contact contact = new Contact ( Salutation='Mr.', FirstName='Joe', LastName='Schmoe', AccountId=newAccount.id );
        insert contact;

        newAccount.RecordTypeId = recordType.id;
        update newAccount;
    }
    
}

 

This was selected as the best answer