2

I have included the code below for the insertion of account, contact & opportunity records

As per the code I think I can insert single contact at a time, if another system move multiple accounts to sfdc at that time i want to change any code. to insert multiple accounts

 global class Integration {

     global class AllObjects {

         webservice string aName;
         webservice string aNum;

         webservice string cName
     }

     global class Return {
         webservice string raName;
     }

     webservice static Return Allobjectsmethod(AllObjects a1) {
         Account a = new Account();
         a.Name = a1.aName;
         a.Number = a1.aNum;
         insert a;

         Contact c = new Contact();
         c.Name = a1.cName;
         insert c;

         Return r = new Return();
         r.raName = a.Name;
         return r;

     }
2

I think what your asking is how to change the code to accept a list of AllObject's?

webservice static List<Return> Allobjectsmethod(List<AllObjects> a1)

To implement this method you need to ensure you minimise the number 'insert' statements (known as DML statements) you execute as Apex governors the maximum per request to 100. Fortunately this statement allows you to pass a list of records to insert, this practice btw is known as 'bulkifciation' on the Force.com platform. So you can adapt your code to something like this...

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++)
         contacts[rowIdx].AccountId = accounts[rowIdx].Id;      

     // Bulk insert contacts
     insert contacts;

     return returns;
 }

I noticed in your sample code you don't associate the contacts with the accounts created, i assume this was just an omission to keep your question short. If not, make sure once you have inserted the Accounts you loop back over your Contacts and associate them before inserting the contacts.

Update: I've updated the sample code to show the association between Account and Contact being made.

NOTE: Its also worth noting that Salesforce does provide a number of existing web service and REST API's to plainly insert records into its objects. The benefit however of your approach here is there will be a database transaction boundary around your Apex Web Service. Meaning if any of your contacts fail to insert, it will rollback any of the Accounts as well.

  • May i know how can i test my class for bulk insertion or not. – Sathya Dec 6 '13 at 7:57
  • from my above webservice hoe can map account id to contact.and where can i replace my code for this. – Sathya Dec 10 '13 at 10:52
  • As per my note, you need to insert the accounts then loop back over the contacts assigning the AccountId field on the Contact accordingly. – Andrew Fawcett Dec 10 '13 at 10:58
  • Andrew Fawcett Hi i modigy like this c.AccountId = a.Id; but it is not inserting can you modify my code where i have to asign accountid to contact. – Sathya Dec 10 '13 at 11:06
  • I've added some more code to illustrate this, however you really perhaps should take time to study the Apex Developers guide more on writing tests and general coding, you will find your investment worth while in the future! ;-) – Andrew Fawcett Dec 10 '13 at 11:16

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.