As per my webservice i am working with bulk records suppose if same account name comes from second time means how can i check in salesforce that if account is exist or not if exist means under that same account we have to add a contact, And how can i check contact is exist or not how can i modify my webservice

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) {

            Account a = new Account();
            a.Name = a1.aName;
            a.Number = a1.aNum;
            Contact c = new Contact();
            c.Name = a1.cName;
            Return r = new Return();
            r.raName = a.Name;

        // Bulk inserts
        insert accounts;
        insert contacts;

        return returns;

1 Answer 1


I'm going to give you a general answer here rather than a code specific one. First, you're going to need to create either lists or maps of all your accounts and your contacts-by-account that you can use to query on existing accounts and contacts in the database. You'll then need to made a query on accounts to see if those accounts already exist.

Once you have a list of accounts returned which coincide with the accounts in your webservice you could run a separate query of contacts-by-account for all the contacts for the accounts that were returned (in theory, you could have run this as one combined query if you had wanted to). You'd then do a comparison between the contacts you have that are related to those accounts after your query is returned and decide whether to update those contacts. If they don't exist, then you'd simply do an insert of the new contacts.

As you can see, this will involve a several step process of querying the existing database and then sorting the results using for loops to compare with your webservice. Creating maps and sets of your webservice data would likely be the easiest route to approach this. The order would greatly depend on what you're most comfortable with.

Maps and sets would be more efficient for at least portions of this since they won't duplicate any accounts when running your queries. However if used for the "key" when there are multiple contacts on the same account, you wouldn't want to use a map since only one contact would be retained per account; thus the reason I say you'll need to figure out the best approach for your use case in creating your queries and comparing your results.

The latter is where something like a map containing account and lists of contacts would be more appropriate. You could convert the contact name to a string and create a list<string>, so you'd have a map<Id,list<string>> or something similar (where Id is the accountId).


Sample code of how and where to query shown below.

Note: Avoid using names like accounts, contacts, (plurals of object names), etc as variables in your code as they can have other meaning in Apex as you'll see in my query below.

List<Account> acctlst = new List<Account>();
List<Contact> ctclst = new List<Contact>();

for(AllObjects a1 : a1List){
        Account a = new Account();
        a.Name = a1.aName;
        a.Number = a1.aNum;
        Contact c = new Contact();
        c.Name = a1.cName;
        // you now have a list of accounts and list of contacts associated with particular accounts.

    map<Id,Account> AcctQry= new map<Id,Account>([select Id, Name, (select Name, Account from Contacts) from Account where Name in acctlst]);

    // now the above map needs to be sorted for contacts that are in the accounts which were returned. Only accounts that were in your webservice will return contacts, however it will return ALL contacts associated with the account. I'm showing you the way to do this with only one query

  set<Id>QryIds = new set<Id>();

  QryIds = AcctQry.keyset; // contains all the Acct Id's that were returned from the query

  list<Account>QryAcct = new list<Account>();

  list<Contact>QryCtcs = new list<Contact>();

  map<String, String>Ctc2Acct = new map<String, String>(); // map of ctcs to acct that are also in web service feed

  for(Id QId: QryIds){

     Account Acct = AcctQry.get(QId);
     // Acct.Name = the name of the acct that was returned from the query should you need it for any reason. You can add it to the list QryAcct if you want to.

     Contact Ctc = Acct.Contacts; // the way to access the sub query on related contacts

     for(Contact c: Ctc){

        if(Ctc.Account == ctclst.Account){

           if(Ctc.Name == ctclst.Name){

              Ctc2Acct.put(ctc.Account, Ctc.Name); // map of New Ctc Name to Acct Name 
              // note with using a map here, if more than one ctc, then only the last contact will be retained in the map. You could miss a duplicate contact and may want to investigate other methods. 

              QryCtcs.add(Ctc); // list of contacts that are also in webservice feed               
              // This is part of your back-up or alternate method. Can always check in reverse, then look to see if both have same account.



     QryAcct.add(Acct); // list of Accounts returned in query that are also in webservice feed can use this approach if problem with maps or as part of cross-check

  if(Ctc2Acct.isEmpty()){ // proceed without doing anything because there are no duplicates in your database

// will now need to remove duplicate records from your webservice feed.
// will also need to finish building your return
// will leave the rest to you to sort out how to do as I hope it's fairly basic 

Disclaimer This code has not been tested or verified. Was written 'on the fly' to use as an example for illustrative purposes only. Use at your own risk.

  • Actually i am confusion where i have to write a query to reterview the records could you please modify my code
    – Sathya
    Commented Dec 10, 2013 at 15:55
  • I'll show you where to insert the queries with some pseudo code. You'll need to write the actual code.
    – crmprogdev
    Commented Dec 10, 2013 at 16:17
  • ok sure thanq for reply.
    – Sathya
    Commented Dec 10, 2013 at 16:22
  • Thanq for reply i used your code but i am getting some errors could you please modify my code.please.
    – Sathya
    Commented Dec 11, 2013 at 10:51
  • From my original reply with the code: Disclaimer This code has not been tested or verified. Was written 'on the fly' to use as an example for illustrative purposes only. Use at your own risk. Without seeing what you've written, I'd have no idea where to help you troubleshoot your problem. If you edit your post with the new code, or create a new post with it, perhaps someone will help you troubleshoot it.
    – crmprogdev
    Commented Dec 11, 2013 at 13:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .