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
Suresh RaghuramSuresh Raghuram 

help for writing test class

situation is when i create patient record on contact some of its fields are to be populated in the panthom  object

code of trigger and class are as follows

trigger CreateAffiliation1 on Contact (after Insert, after Update) {

createPhantom pnt1 = new createPhantom();
Phantom__c p = new Phantom__C();
for(Contact con : Trigger.new){
if(con.RecordTypeId == '012U00000009ALt'){

Pnt1.Phantom();
}
}
}}

public class createphantom {

Panthom__c p = new Panthom__c();

List<contact> cnt =new List<Contact>([select Name,id,ReportsToId, AccountId
from Contact order by CreatedDate desc limit 1 ]);
// Create an empty list to keep DML out of the loop!
List<phantom__c> lstPhantomNew = new List<phantom__c>();

public void phantom() {

for(Contact thisP : cnt) {

phantom__c pnt = new phantom__c();

pnt.Name = thisP.Name;
pnt.External_Key__c = thisP.Id;
pnt.Last_Name__c = thisP.Name;
pnt.Reports_To__c = thisP.ReportsToId;
pnt.Account_Name__c = thisP.AccountId;

lstPhantomNew.add(pnt);
}
if(lstPhantomNew.size() > 0) {


insert lstPhantomNew; }
}
}

 

The test class which i wrote is as follows it is working but the code coverage is 0%

@isTest
private class PhantomTest{

static testMethod void PanthomMethod(){

Contact ct = new contact (RecordTypeId = '012U00000009ALt');
ct.LastName = 'jhon';
ct.ReportsToId = 'hasan Parthy';
ct.AccountId = 'Cognizent';

Insert ct;

CreatePhantom cp = new CreatePhantom();


cp.Phantom();

}
}

 

LoserKidLoserKid

In the @istest this code is not doing anything. 

 

CreatePhantom cp = new CreatePhantom();

cp.Phantom();

 

The trigger goes when you insert ct. The fact that you are inserting ct and fire code should create a new phantom. Make sure that you conditional statements gonna be true. when you do a test class you want to push in data that will test all conditional statments. There may be a problem with your record type. part of best practices is to never hard code anything. if you move this from Sand to Prod you will have issue. best thing ot do is get the record typo base off the name and comper it to thats.

 

RecordType rt = [select id from RecordType where name = '{record type name here}' limit 1]; 

 

if(con.RecordTypeId == rt.id)

{

...