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
k2018123k2018123 

creating a new list from the existing list

Hi ,
i have a list called List<test__c> lsttest = Database.query(sQueryForUniqueId);. It has a field name unique code with value "1234;456".
I want to create a new list which will have two records one with unique code 1234 and another with 456. Can anyone help with a sample code snippet?

Thanks
Best Answer chosen by k2018123
Pradeep SinghPradeep Singh
try this one:-

List<test__c> List1 = new List<test__c>();
List<test__c> List2 = new List<test__c>();

For(test__c test : lsttest){
    if(test.testfield__c == test.name){
         test__c test1 = new test__c ();
         test1 = test.clone();
         List1.add(test1);
    }
    else{
        test__c test1 = new test__c ();
        test1 = test.clone();
        List2.add(test1);
       
       test__c test2 = new test__c ();
         test2= test.clone();
         test2.testfield__c = test.name
         List2.add(test2);
    }
}
 

All Answers

Pradeep SinghPradeep Singh
Hi,
Use something like :-

List<string> List1 = new List<string>();
List<string> List2 = new List<string>();
List<string> fieldvalueList = new List<string>();

For(test__c test : lsttest){
    fieldvalueList  = test.field.split(";");
    List1.add(fieldvalueList[0]);
    List2.add(fieldvalueList[1]);
}

Its the basic one and might need some changes according to the requirements/complexity.

If this solves your problem, mark it as solved.
k2018123k2018123
Works fine if i have only one field. But the requirement is something different. My lsttest is having two fields "Name and testfield".
Sample record -
                        name - test
                        testfield - testsecondrecord.

So it should create a new list with two records;
first record will have just name - test and testfield - blank. second will have name - test and testfield - testsecondrecord.

Can you please help?
k2018123k2018123
I think is requirement is confusing now.

So i have a list List<test__c> lsttest = Database.query(sQueryForUniqueId).
testfield__c and name are the two fields in this database.query.
I want to create a new list lsttest1 which will seperate the records on the below mentioned condition,
If testfield__c = name create one record in the list.
if testfield__c != name , create two records in the list. one with testfield__c = name and another with testfield__c != name.

Thanks
Pradeep SinghPradeep Singh
try this one:-

List<test__c> List1 = new List<test__c>();
List<test__c> List2 = new List<test__c>();

For(test__c test : lsttest){
    if(test.testfield__c == test.name){
         test__c test1 = new test__c ();
         test1 = test.clone();
         List1.add(test1);
    }
    else{
        test__c test1 = new test__c ();
        test1 = test.clone();
        List2.add(test1);
       
       test__c test2 = new test__c ();
         test2= test.clone();
         test2.testfield__c = test.name
         List2.add(test2);
    }
}
 
This was selected as the best answer