I am trying to create a new case with record type to cover the lines in my apex class but throwing the inbound error. please help me to cover the lines This is the full method
public void onAfterInsert()
{
// don't do anything if not licensed
if (!FMA_isEnabled)
{
return;
}
// reset class variables
carePlanList = new List<Case>();
carePlanIds = new List<Id>();
dmlOpts = new Database.DMLOptions();
// map referral Case to careplan Case
Map<Id,Id> refToPlanMap = new Map<Id, Id>();
for (Case caseObj : newLst)
{
// if record type is CarePlan
if (
rtIdMap.containsKey(caseObj.RecordTypeId)
&&
rtIdMap.get(caseObj.RecordTypeId).getDeveloperName() == 'CarePlan'
&&
Util.getRecordTypeNamespace(caseObj.RecordTypeId) == 'HealthCloudGA'
&&
caseObj.Associated_Referral__c != null
)
{
handleNewCarePlanAssignment(caseObj);
refToPlanMap.put(caseObj.Associated_Referral__c, caseObj.Id);
}
}
// set in handleNewCarePlanAssignment
if (!carePlanIds.isEmpty() &&
Schema.sObjectType.Case.fields.Assign_using_active_assignment_rule__c.isUpdateable()
&&
Schema.sObjectType.Case.fields.ParentId.isUpdateable())
{
for (Case plan : [SELECT Id, Assign_using_active_assignment_rule__c FROM CASE WHERE Id IN :carePlanIds])
{
plan.Assign_using_active_assignment_rule__c = false;
plan.ParentId = null;
plan.setOptions(dmlOpts);
carePlanList.add(plan);
}
}
// update care plans
if(!carePlanList.isEmpty()){
update carePlanList;
}
if (!refToPlanMap.keySet().isEmpty() && Schema.sObjectType.Case.fields.ParentId.isUpdateable()
&&
Schema.sObjectType.Case.fields.ConvertedToCarePlan__c.isUpdateable())
{
// get referral cases to update
for (Case refCase : [SELECT Id, ConvertedToCarePlan__c, ParentId FROM Case WHERE Id IN :refToPlanMap.keySet()])
{
// set parent Careplan Case
refCase.ParentId = refToPlanMap.get(refCase.Id);
refCase.ConvertedToCarePlan__c = true;
// add to list of updates
referralList.add(refCase);
}
}
// update referrals
if(!referralList.isEmpty()){
update referralList;
}
}
Test Class method
@testSetup static void setup() { UserTest.initTestUsers();
TestUtilities.IS_FMA_ENABLED = true;
System.runAs(UserTest.getTestUser(UserTest.UserType.mcm))
{
// TODO :: change to SOQL & filter by namespace?
Map<String, Schema.RecordTypeInfo> caseRTMap = Schema.SObjectType.Case.getRecordTypeInfosByDeveloperName();
// Account
Account testPatient = TestUtilities.createPatientAccount();
// Program
HealthCloudGA__Program__c testProgram = (HealthCloudGA__Program__c) COMM_DataFactory.createSObject(new HealthCloudGA__Program__c(
Name = 'Unit Test Program',
HealthCloudGA__StartDate__c = Date.today(),
HealthCloudGA__EndDate__c = Date.today() + 1
), true);
Case referralCase = (Case) COMM_DataFactory.createSObject(
new Case
(
RecordTypeId = caseRTMap.get('Referral').getRecordTypeId(),
Program_Name__c = testProgram.Id
),
true
);
Case carePlanCase = (Case) COMM_DataFactory.createSObject( new Case ( RecordTypeId = caseRTMap.get('carePlan').getRecordTypeId(), Program_Name__c = testProgram.Id ), true ); } }
@isTest
static void testOnAfterInsert()
{
TestUtilities.IS_FMA_ENABLED = true;
System.runAs(UserTest.getTestUser(UserTest.UserType.mcm))
{
Test.startTest();
Map<String, Schema.RecordTypeInfo> caseRTMap = Schema.SObjectType.Case.getRecordTypeInfosByDeveloperName();
List<Case> referralCase = [
SELECT Id, Program_Name__c, ContactId
FROM Case
WHERE RecordTypeId = :caseRTMap.get('Referral').getRecordTypeId()
];
List<Case> carePlanCase = [
SELECT Id, Program_Name__c, ContactId
FROM Case
WHERE RecordTypeId = :caseRTMap.get('CarePlan').getRecordTypeId()
];
Case carePlan = new Case();
carePlan.RecordTypeId = caseRTMap.get('CarePlan').getRecordTypeId();
carePlan.Program_Name__c = referralCase[0].Program_Name__c;
carePlan.Associated_Referral__c = referralCase[0].Id;
insert carePlan;
Case Referral = new Case();
Referral.RecordTypeId = caseRTMap.get('CarePlan').getRecordTypeId();
Referral.Program_Name__c = carePlanCase[0].Program_Name__c;
Referral.Associated_Referral__c = carePlanCase[0].Id;
system.debug('referral'+ carePlanCase);
insert Referral;
if(!carePlanCase.isEmpty()){
carePlan.ParentId = referralCase[0].ParentId;
}
referralCase = [
SELECT Id, ConvertedToCarePlan__c
FROM Case
WHERE RecordTypeId = :caseRTMap.get('Referral').getRecordTypeId()
];
System.assert(referralCase[0].ConvertedToCarePlan__c == true, 'Referral Case - Converted to Care Plan not updated.');
Test.stopTest();
}
}