+ Start a Discussion
avianna lopexavianna lopex 

Need a help in writing SOQL and its test class

Dear Folks,

I need to write a SOQL to copy data from  Case_Type_data_c to new object Exec_Case_Type_c ..I have the query to pull out the data from the this new Exec case type data
select Case_c, Level_1c, Level_2c,Level_3c from Exec_Case_Type_c

Can some one please provide a  SOQL and its relevant test case class

Thanks in Advance
Avianna
 
Best Answer chosen by avianna lopex
SFDC_SaurabhSFDC_Saurabh
List<Exec_Case_Type_c> execs = new List<Exec_Case_Type_c>();
List<Case_Type_data_c> ctData = mew List<Case_Type_data_c>();
execs = [select Case_c, Level_1c, Level_2c,Level_3c from Exec_Case_Type_c];
for(Exec_Case_Type_c ec : execs){
ctData.add(new Case_Type_data_c(filedname1 = ec.correspondingFieldName, FieldName2=ec.CorrespondinfFiled2));
}
insert ctData ;
Try out above as a pseudo code. I assume you wish to copy data from Exec_Case_Type_c to Case_Type_data_c.
 

All Answers

SFDC_SaurabhSFDC_Saurabh
List<Exec_Case_Type_c> execs = new List<Exec_Case_Type_c>();
List<Case_Type_data_c> ctData = mew List<Case_Type_data_c>();
execs = [select Case_c, Level_1c, Level_2c,Level_3c from Exec_Case_Type_c];
for(Exec_Case_Type_c ec : execs){
ctData.add(new Case_Type_data_c(filedname1 = ec.correspondingFieldName, FieldName2=ec.CorrespondinfFiled2));
}
insert ctData ;
Try out above as a pseudo code. I assume you wish to copy data from Exec_Case_Type_c to Case_Type_data_c.
 
This was selected as the best answer
Abhishek BansalAbhishek Bansal
Hi Avianna,

Please find the below class to clone records from one object to other:
public class CopyRecords {
	public void cloneCaseRecords() {
		List<Case_Type_data_c> newCaseTypeDataList = mew List<Case_Type_data_c>();
		Case_Type_data_c newCaseTypeData;
		for(Exec_Case_Type_c exeCaseType : [select Case__c, Level_1__c, Level_2__c,Level_3__c from Exec_Case_Type_c]) {
			newCaseTypeData = new Case_Type_data_c();
			newCaseTypeData.Case__c = exeCaseType.Case__c;
			newCaseTypeData.Level_1__c = exeCaseType.Level_1__c;
			newCaseTypeData.Level_2__c = exeCaseType.Level_2__c;
			newCaseTypeData.Level_3__c = exeCaseType.Level_3__c;
			newCaseTypeDataList.add(newCaseTypeData);
		}

		if(newCaseTypeDataList.size() > 0) {
			insert newCaseTypeDataList;
		}
	}
}

Below is the test class:
@isTest
private class CopyRecordsTest {
	static testMethod void testCloneCaseRecords() {
		Exec_Case_Type_c testExecRecord = new Exec_Case_Type_c();
		testExecRecord.Name = 'Test Name';
		//Add all required fields here
		insert testExecRecord;
		
		Test.startTest();
		CopyRecords testObj = new CopyRecords();
		testObj.cloneCaseRecords;
		Test.stopTest();
	}
}

Please take care of the API name of the fields and let me know if you need any other help on this.

Thanks,
Abhishek Bansal.
Gmail: abhibansal2790@gmail.com
Skype: abhishek.bansal2790
Phone: +917357512102
avianna lopexavianna lopex
Getting error in dev console while running tests,I am not passing ID at all in test class then also error coming? please help,I gave different values as well like a3XJ0000000WuaM but same message
 
System.StringException: Invalid id: Account Maintenance

17:30:04:052 FATAL_ERROR System.StringException: Invalid id: Account Maintenance

Here is code for copy data from Case_Type_data_c. to Exec_Case_Type_c 
public class CopyRecords {
	public void cloneCaseRecords() {
              List<Exec_Case_Type__c> execs = new List<Exec_Case_Type__c>();
              List<Case_Type_data__c> ctData = new List<Case_Type_data__c>();
              ctData = [select Name, Level_1__c, Level_2__c,Level_3__c from Case_Type_data__c];

             for(Case_Type_data__c ct : ctData){
               execs.add(new Exec_Case_Type__c(Case__c = ct.Name, Level_1__c=ct.Level_1__c,Level_2__c=ct.Level_2__c,Level_3__c=ct.Level_3__c));
                                               }
            insert execs ;
	                              }
       }

Test class
 
@isTest
private class CopyRecordsTest {
	static testMethod void testCloneCaseRecords() {
		Case_Type_data__c testExecRecord = new Case_Type_data__c();
        testExecRecord.Name = 'Account Maintenance';
		testExecRecord.Level_1__c = 'Account Maintenance Issues';
		testExecRecord.Level_2__c = 'Cancellation';
		testExecRecord.Level_3__c = 'Misuse of service';
		insert testExecRecord;
		
		Test.startTest();
		CopyRecords testObj = new CopyRecords();
		testObj.cloneCaseRecords();
		Test.stopTest();
	}
}

​​​​​​​