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
sathya82sathya82 

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;
}
RockzRockz

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

 

SeAlVaSeAlVa

Maybe...

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)
		{
			accounts.add(
				new Account(
					Name = a1.aName,
					Number=a1.aNum
				)
			);
			contacts.add(
				new Contact(
					Name = a1.cName
				)
			);
			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++)
		{
			contacts.get(rowIdx).AccountId = accounts.get(rowIdx).Id;
		}
		// Bulk insert contacts
		insert contacts;
		return returns;
	}
}

 

sathya82sathya82

@Rockz,@

 

        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.

sathya82sathya82

@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 .

sathya82sathya82

@Rockz,@

 

   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.

 accounts = [Select Id from Account where Name =: a1.aName];

        if(accounts.size() > 0)
        {
            Account a;
           for(integer i = 0 ; i<accounts.size();i++)
          
           a = accounts[0];
        }