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
Øyvind Borgersen 10Øyvind Borgersen 10 

test class for cloned record

Hi,

Can someone help me with a test class for the below code?
It's a list view with a cloned functionality.

public with sharing class cloneCaseApplication {
@AuraEnabled
public static List < Case > cloneCaseApp() {
//Create variables
String currentuser = UserInfo.getUserId();
String emailAddress = UserInfo.getUserEmail();
Id userContactId = [Select contactid from User where id =: Userinfo.getUserid()].contactId;
List < String > caseRecordtype = new List <String> ();
List < Case > casListCommunity = new List <case>();
List < Case > casListInternal = new List <case>();
system.debug('currentuser' + currentuser);
system.debug('userContactId' + userContactId);
// Add recordtype to list
caseRecordtype.add('Produksjonstilskudd');
caseRecordtype.add('Produksjonstilskudd_ny');
// Create list of all accounts which current user is linked to
List < Account > userAccounts = [SELECT Id,
name
FROM Account
WHERE Id IN(SELECT accountId FROM AccountContactRelation WHERE contactId =: userContactId)];
System.debug('userAccounts' + userAccounts);
// List all cases for accounts linked to user
if (userContactId != null) {
List < Case > cas = [SELECT Id,
Casenumber,
FROM Case
WHERE AccountId IN: userAccounts
AND RecordTypeId IN: caseRecordtype];
return cas;
}
else {
List < Case > casempt = [SELECT Id,
CaseNumber,
Account.Name
FROM Case
//WHERE RecordTypeId IN : caseRecordtype
LIMIT 20];
System.debug('casempt' + casempt);
return casempt;
}
}
@AuraEnabled
public static Boolean cloneRecord(String recId) {
system.debug('RecordToClone' + recid);
List < Case > toBeCloned = new List < Case > ();
Case cloneThis = [SELECT Id, CaseCloned__c FROM Case WHERE ID =: recId];
system.debug('clonethis' + cloneThis);
system.debug('this fires');
if (clonethis != null && Clonethis.CaseCloned__c != true) {
Case CloneTest = cloneThis.clone();
cloneThis.CaseCloned__c = true;
insert clonetest;
update clonethis;
system.debug('clontest' + clonetest);
return true;
} else if (Clonethis.CaseCloned__c == true) {}
return false;
}
}
Best Answer chosen by Øyvind Borgersen 10
sfdcMonkey.comsfdcMonkey.com
Hi here is the code for test class,  this will gives you more then 85% code coverge for your class :
 
@isTest
public class cloneCaseApplicationTest {
    public testMethod static void  testOne(){
        account acc = new account();
        acc.Name = 'test' ; 
        // add all required fields here 
        insert acc;
        Case caseObj = new Case(
            
            AccountId = acc.Id,
            Status = 'Working',
            Origin = 'Phone');
        
        insert caseObj;
        
        cloneCaseApplication.cloneCaseApp();
        cloneCaseApplication.cloneRecord(caseObj.Id);
        
        caseObj.CaseCloned__c = true;
        update caseObj;
        cloneCaseApplication.cloneRecord(caseObj.Id);   
    }
}

Thanks, let us know if it helps you 
 

All Answers

sfdcMonkey.comsfdcMonkey.com
Hi here is the code for test class,  this will gives you more then 85% code coverge for your class :
 
@isTest
public class cloneCaseApplicationTest {
    public testMethod static void  testOne(){
        account acc = new account();
        acc.Name = 'test' ; 
        // add all required fields here 
        insert acc;
        Case caseObj = new Case(
            
            AccountId = acc.Id,
            Status = 'Working',
            Origin = 'Phone');
        
        insert caseObj;
        
        cloneCaseApplication.cloneCaseApp();
        cloneCaseApplication.cloneRecord(caseObj.Id);
        
        caseObj.CaseCloned__c = true;
        update caseObj;
        cloneCaseApplication.cloneRecord(caseObj.Id);   
    }
}

Thanks, let us know if it helps you 
 
This was selected as the best answer
Raj VakatiRaj Vakati
Use this
 
@isTest
private class cloneCaseApplicationTest{
    @testSetup
    static void setupTestData(){
        
		Account portalAccount1 = new Account(
Name = 'TestAccount',
OwnerId = portalAccountOwner1.Id
);
Database.insert(portalAccount1);

//Create contact
Contact contact1 = new Contact(
FirstName = 'Test',
Lastname = 'McTesty',
AccountId = portalAccount1.Id,
Email = System.now().millisecond() + 'test@test.com'
);
Database.insert(contact1);

        Profile pf= [Select Id from profile where Name='System Administrator']; 
        
        String orgId=UserInfo.getOrganizationId(); 
        String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','') ;
        Integer RandomId=Integer.valueOf(Math.rint(Math.random()*1000000)); 
        String uniqueName=orgId+dateString+RandomId; 
        User uu=new User(firstname = 'ABC', 
                         lastName = 'XYZ', ContactId = contact1.Id,

                         email = 'test@gmail', 
                         Username = uniqueName + '@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1', 
                         Alias = uniqueName.substring(18, 23), 
                         TimeZoneSidKey = 'America/Los_Angeles', 
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US', 
                         ProfileId = pf.Id
                        ); 
        
        
        insert uu;
        
    }
    static testMethod void test_postive (){
		try {
	User u = [Select Id,IsActive,Email,Username,Profile from User limit 1 ] ; 
	System.runAs(u){
		 Account accRecord=new account();
        accRecord.Name='Test Acc1114';
        accRecord.AccCustId__c='119900';
        insert accRecord;

        contact conRecord=new contact();
        conRecord.AccountId=accRecord.id;
        conrecord.lastname='TestCon1114';
        conrecord.ConCustId__c='32444';
        insert conRecord;

        AccountContactRelation acr = new AccountContactRelation(); 
        acr.Accountid = accRecord.id;
        acr.Contactid = conRecord.id;
        insert acr;
		
		Id caseR = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Produksjonstilskudd').getRecordTypeId();
 account acc = new account();
        acc.Name = 'test' ; 
        // add all required fields here 
        insert acc;
        Case caseObj = new Case(
            
            AccountId = acc.Id,RecordTypeId =caseR,
            Status = 'Working',
            Origin = 'Phone');
        
        insert caseObj;
		
		cloneCaseApplication.cloneCaseApp();

        cloneCaseApplication.cloneRecord(caseObj.Id);

	}

  }
	}
  
}

 
Øyvind Borgersen 10Øyvind Borgersen 10
Hi Raj,

Thanks for the example. It does however has some flaws, but it's a good guide for the test I need.