0

I am using below query and getting the error "COMPILE ERROR: Variable does not exist: R1_ACC_TXT_Id_Golden_record__c LINE: 3 COLUMN: 47"

Query:

    List<Account> Ac = [SELECT Id,FirstName,LastName,R1_ACC_TXT_Id_Golden_record__c FROM Account WHERE 
         R2_ACC_TXT_ICAR_Seg_maturity_level__c ='Churned Customer' and  R1_ACC_FOR_Primary_FF_Card_CCPO__c = null
        and R1_ACC_TXT_Primary_FF_Card_CCPO__c =  null and R1_ACC_TXT_Primary_Frecuent_Flyer_Card__c = null and R2_ACC_NUM_ICAR_ind_ibcom__c = 0 limit 1];
        Account_Archive__b Acarchive = new Account_Archive__b();
        Acarchive.R1_ACC_TXT_Id_Golden_record__c = Ac.R1_ACC_TXT_Id_Golden_record__c;
        Acarchive.AccountID__c = Ac.Id;
        Acarchive.First_Name__c = Ac.FirstName;
        Acarchive.Last_Name__c = Ac.LastName;
        database.insertImmediate(Acarchive);

2 Answers 2

0

You are trying to get the value from List< Account> ac in a wrong way. As you are querying only one record.. You can use Account ac and get the value as you are doing.

If you want to use the list.. then you should get the first index of the list as follows:-

ac.get(0).R1_ACC_TXT_Id_Golden_record__c

try this:-

Account Ac= [SELECT Id,FirstName,LastName,R1_ACC_TXT_Id_Golden_record__c FROM Account WHERE 
         R2_ACC_TXT_ICAR_Seg_maturity_level__c ='Churned Customer' and  R1_ACC_FOR_Primary_FF_Card_CCPO__c = null
        and R1_ACC_TXT_Primary_FF_Card_CCPO__c =  null and R1_ACC_TXT_Primary_Frecuent_Flyer_Card__c = null and R2_ACC_NUM_ICAR_ind_ibcom__c = 0 limit 1];
        Account_Archive__b Acarchive = new Account_Archive__b();
        Acarchive.R1_ACC_TXT_Id_Golden_record__c = Ac.R1_ACC_TXT_Id_Golden_record__c;
        Acarchive.AccountID__c = Ac.Id;
        Acarchive.First_Name__c = Ac.FirstName;
        Acarchive.Last_Name__c = Ac.LastName;
        database.insertImmediate(Acarchive);

List method:-

get(index):- Returns the list element stored at the specified index.

Usage:- To reference an element of a one-dimensional list of primitives or sObjects, you can also follow the name of the list with the element's index position in square brackets as shown in the example.

example:-

List<Integer> myList = new List<Integer>();
myList.add(47);
Integer myNumber = myList.get(0);
system.assertEquals(47, myNumber);

You can learn more about List in apex here:- https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_list.htm

Update:- If you have a large number of records, all you need is run a for loop through the list, store the data in another list which you are trying to insert, and insert the list outside for loop like this:-

List< Account_Archive__b Acarchive> listtoinsert = new list< Account_Archive__b Acarchive>();
for(Account ac :acclist){
   Account_Archive__b Acarchive aaba= new  Account_Archive__b Acarchive();
        aaba.R1_ACC_TXT_Id_Golden_record__c = Ac.R1_ACC_TXT_Id_Golden_record__c;
        aaba.AccountID__c = Ac.Id;
        aaba.First_Name__c = Ac.FirstName;
        aaba.Last_Name__c = Ac.LastName;
        listtoinsert.add(aaba);
}
insert listtoinsert;
6
  • Thanks @Sanket kumar .I get it now.But what do i do if i have to query tens of 1000s of records in the list of Account and insert them all in the big object Acarchive?I am a novice and i can fathom that i need to use some kind of for loop here.Please help! Commented Sep 16, 2018 at 14:57
  • Check my answer. I have updated it. Commented Sep 16, 2018 at 15:18
  • Thanks Sanket. I made few changes and it worked like charm. Commented Sep 16, 2018 at 15:46
  • Welcome :) @PseudoDarwinist Commented Sep 16, 2018 at 15:50
  • Your syntax is wrong: ac.[0] is not how you get the first element in a list.
    – Adrian Larson
    Commented Sep 16, 2018 at 15:56
0

Since it's a big object i had to use insertimmediate method. If it helps somebody who is looking for entire answer.

List <Account> acclist = [SELECT Id,FirstName,LastName,R1_ACC_TXT_Id_Golden_record__c FROM Account WHERE 
             R2_ACC_TXT_ICAR_Seg_maturity_level__c ='Churned Customer' and  R1_ACC_FOR_Primary_FF_Card_CCPO__c = null
            and R1_ACC_TXT_Primary_FF_Card_CCPO__c =  null and R1_ACC_TXT_Primary_Frecuent_Flyer_Card__c = null and R2_ACC_NUM_ICAR_ind_ibcom__c = 0 limit 100];

List <Account_Archive__b> listtoinsert = new List <Account_Archive__b> ();
for(Account ac :acclist){ 
   Account_Archive__b Acarchive = new  Account_Archive__b ();
        Acarchive.R1_ACC_TXT_Id_Golden_record__c = Ac.R1_ACC_TXT_Id_Golden_record__c;
        Acarchive.AccountID__c = Ac.Id;
        Acarchive.First_Name__c = Ac.FirstName;
        Acarchive.Last_Name__c = Ac.LastName;
        listtoinsert.add(Acarchive);
}
database.insertImmediate(listtoinsert);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .