You need to sign in to do that
Don't have an account?

System.ListException: List index out of bounds: 1
global class Integration { global class AllObjects { webservice string aName; webservice string aNum; webservice string cName } global class Return { webservice string raName; } webservice static List<Return> Allobjectsmethod(List<AllObjects> a1List) { List<Account> accounts = new List<Account>(); List<Contact> contacts = new List<Contact>(); List<Return> returns = new List<Return); for(AllObjects a1 : a1List) { Account a = new Account(); a.Name = a1.aName; a.Number = a1.aNum; accounts.add(a); Contact c = new Contact(); c.Name = a1.cName; contacts.add(c); Return r = new Return(); r.raName = a.Name; results.add(r); } // Bulk insert accounts insert accounts; // Assign contacts to accounts **for(Integer rowIdx = 0; rowIdx < accounts.size(); rowIdx++)** // error occur point contacts[rowIdx].AccountId = accounts[rowIdx].Id; // Bulk insert contacts insert contacts; return returns; }
Hi..
Try below code..
global class Integration { global class AllObjects { webservice string aName; webservice string aNum; webservice string cName } global class Return { webservice string raName; } webservice static List<Return> Allobjectsmethod(List<AllObjects> a1List) { List<Account> accounts = new List<Account>(); List<Contact> contacts = new List<Contact>(); List<Return> returns = new List<Return); for(AllObjects a1 : a1List) { Account a = new Account(); a.Name = a1.aName; a.Number = a1.aNum; accounts.add(a); Contact c = new Contact(); c.Name = a1.cName; contacts.add(c); Return r = new Return(); r.raName = a.Name; results.add(r); } // Bulk insert accounts insert accounts; if(!accounts.isEmpty()){ // Assign contacts to accounts for(Integer rowIdx = 0; rowIdx < accounts.size(); rowIdx++) contacts[rowIdx].AccountId = accounts[rowIdx].Id; } // Bulk insert contacts insert contacts; return returns; }
Please accept my answer as a solution if my solution was helpful. This will make it available to others as a proper answer. If you felt that I went above and beyond please give me Kudos by clicking on on the star icon.
Thanks,
Cool Sfdc
Maybe...
@Rockz,@SeAlVa
Again i am getting same error,
suppose if i insert Acc1, againg i am trying to inser Acc1 at that time it showing an error.
@RockZ
I modified my code it's working but here one issuse is there when Acc1--->>con1
if same acc1 comes from third party means we have to check Acc1 is exist or not if exist means under that con2 have to
insert
Issuse-->> when acc1 is in sfdc, if again acc1 is comes means it insert as a new record in sfdc how can i prevent this new record insertion.con2 have to insert under Acc1 .
@Rockz,@SeAlVa
i use this code for checking account name is exist or not in salesforce but showing an error list out of bounds o may i know how can i modify my code.