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
SFDC_biSFDC_bi 

Insert new record not working

Please help me. I am not sure what's the issue with this class its not updating.
 
public class MyFirstClass {
    public static void method(){
    List<Account> accLst = new List<Account>([select id,name,description from Account where name like 'United%']);
    system.debug('The account lists are:' + accLst);
    List<Account> accupdLst = new List<Account>();
    for(Account acc: accupdLst ){
        acc.Description = 'Updated from Apex Class';
        accupdLst.add(acc);
    }
    if(accupdLst != null && accupdLst.size()>0){
    update accupdLst;
        
    }
    system.debug('The updated account lists are:' + accupdLst);
}
}

Please help me!<

 
Best Answer chosen by SFDC_bi
Ajay K DubediAjay K Dubedi
Hi Mohamed,

I want to tell you the thing you are doing wrong in your code.
You are trying to iterate accupdLst list which you have initialized but I can see that the list is empty and there are no records inside it.
All you have to do is replace accupdLst with accLst and now your code will surely execute because there you have made a query for this list
and so it contains records.

public class MyFirstClass {
    public static void method(){
    List<Account> accLst = new List<Account>([select id,name,description from Account where name like 'United%']);
    system.debug('The account lists are:' + accLst);
    List<Account> accupdLst = new List<Account>();
    for(Account acc: accLst ){
        acc.Description = 'Updated from Apex Class';
        accupdLst.add(acc);
    }
    if(accupdLst != null && accupdLst.size()>0){
    update accupdLst;
        
    }
    system.debug('The updated account lists are:' + accupdLst);
}
}

If you find this explanation helpful please mark it as best answer so others get help from this.
Thank you.
Ajay Dubedi

All Answers

Steven NsubugaSteven Nsubuga
You used the wrong list in the for loop!! Try this
public class MyFirstClass {
    public static void method(){
    List<Account> accLst = new List<Account>([select id,name,description from Account where name like 'United%']);
    system.debug('The account lists are:' + accLst);
    List<Account> accupdLst = new List<Account>();
    for(Account acc: accLst ){
        acc.Description = 'Updated from Apex Class';
        accupdLst.add(acc);
    }
    if(accupdLst != null && accupdLst.size()>0){
    update accupdLst;
        
    }
    system.debug('The updated account lists are:' + accupdLst);
}
}

 
Raj VakatiRaj Vakati
try this
public class MyFirstClass {
    public static void method(){
    List<Account> accLst = new List<Account>([select id,name,description from Account where name like 'United%']);
    system.debug('The account lists are:' + accLst);
    List<Account> accupdLst = new List<Account>();
    for(Account acc: accLst ){
        acc.Description = 'Updated from Apex Class';
	    accupdLst.add(acc);
    }
    if(accupdLst != null && accupdLst.size()>0){
    update accupdLst;
        
    }
    system.debug('The updated account lists are:' + accupdLst);
}
}

 
Ajay K DubediAjay K Dubedi
Hi Mohamed,

I want to tell you the thing you are doing wrong in your code.
You are trying to iterate accupdLst list which you have initialized but I can see that the list is empty and there are no records inside it.
All you have to do is replace accupdLst with accLst and now your code will surely execute because there you have made a query for this list
and so it contains records.

public class MyFirstClass {
    public static void method(){
    List<Account> accLst = new List<Account>([select id,name,description from Account where name like 'United%']);
    system.debug('The account lists are:' + accLst);
    List<Account> accupdLst = new List<Account>();
    for(Account acc: accLst ){
        acc.Description = 'Updated from Apex Class';
        accupdLst.add(acc);
    }
    if(accupdLst != null && accupdLst.size()>0){
    update accupdLst;
        
    }
    system.debug('The updated account lists are:' + accupdLst);
}
}

If you find this explanation helpful please mark it as best answer so others get help from this.
Thank you.
Ajay Dubedi
This was selected as the best answer
SFDC_biSFDC_bi
Thanks for all your help!!! Can you please share me some scenarios to understand salesforce more.