0

I'm trying to modify my code which works for single account, but I need to handle multiple accounts.

split[0] : holds the account name
split[1] : holds the email

This is my code:

list<string> slistAcc = new list<string>{'abc_rt@gm.com','fd_io@ui.com'};
system.debug(slistAcc);

list<account> acct = new list<account>();
list<account> p2 = new list<account>();

string[] b;

for(integer i=0;i<slistAcc.size();i++) 
{


    string a = slistAcc[i];
      b = a.split('_');

    system.debug('b --->' + b); << ---DEBUG|b --->(abc, rt@gm.com)

    if(!b.IsEmpty())
    {
        acct[i].name = b[0];  <<---keeps failing in this assignment. 

     acct[i].email = b[1];
    system.debug('acct' + acct[i]);    
       p2.add(acct[i])  <--------- 
    }


}
insert p2;

The code keeps failing at the assignment to **acct[i].name = b[0]** saying List index out of bounds... . I don't get how can I assign multiple accounts to a variable. or a list to a list [b is also a list and acct is also a list]. What am trying to achieve is from the split list get account name and email assign to new list -- this needs to be looped through several data in that split array. Now capture all these new assigned values and insert in system using p2. Please advise. Also please excuse me as I'm still getting used to Apex.

  • updated my code as below to get rid of list bounded error : but doesnt give me the result as required: |DEBUG|b --->(abc, rt@gm.com) |DEBUG|b --->(fd, io@ui.com) |DEBUG|(Account:{Name=fd}, Account:{Name=fd}) <---this is incorrect-wanted to contain both abc and fd. How shall i take care of the assignment? – user10727 Apr 9 '15 at 13:53
  • Sorted found the issue!! within the for loop has to initialise the account rather than keeping it outside. – user10727 Apr 9 '15 at 14:02
1

Try this:

string [] email= new string[]{'ram-m@gmail.com','mahi-m@gmail.com'};

    list<account> a= new list<account>();

for(integer i=0;i<email.size();i++)
    {
        account acc= new account();

        acc.name=email[i].split('-')[0];
        acc.email__c=email[i];
        a.add(acc);
    }

insert a;
2

Your list instantiation does not appear to be correct, as you need parentheses. The type you are using is also incorrect, as it is Account, not Accounts. Unless you have some custom class called accounts?

List<Account> p = new List<Account>();

As for the index error, you will not get any errors if you iterate through the elements like so:

for (Account acct : accountList)
{
    // do stuff
}

It won't do anything with an empty list, but you need to populate it somehow and that is outside the scope of your question.

Also, there is no sObject method called split so it is not clear what you are trying to accomplish there.

  • Thanks Adrian!! minelogic has to go with the split. Have updated my query explaining why am using split. – user10727 Apr 9 '15 at 10:14
1

It does have several errors in your code. You use the p[i] before the p list still have no any elements, that's why you got the "index out of bounds" error. I did some change for your code, hope to help you:

List<String> emails = {'abc__ab@gm.com','cdf__fg@ui.com'};
List<account> p = new list<account>;

for (i=0;i < emails.size();i++)
{
    Account acct = new Account();
    acct.name = emails[i].split('__')[0]
    acct.email = emails[i];
    p.add(acct);
}

insert p;
  • I believe the array has name__email values. so split[0] is name and split[1] is email. can you update your code accordingly. – Vamsi Krishna Apr 9 '15 at 1:31
  • In the code, we got the array by spliting email. so split[0] is name and split[1] is just domain, not email. – Allen Li Apr 9 '15 at 1:37
  • Thanks Allen, but am looking for the details as Vamsi Mentioned. – user10727 Apr 9 '15 at 9:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.