0

my webservice is working for new account,contact insert if second time same account is request at that time contact2 is not inserting under that existing account, but it preventing to insert same account second time.may i know why it happens my code is.

 webservice static ResponseClass method(RequestClass req){





      for(integer j=0;j<contactsj++)
      {
        con = new Contact();
        con.LastName = clName;
        con.FirstName =cfName; 
        con.Email = email;
        con.MobilePhone = mobile;             
        con.accountId = srList[i].id;
        contactList.add(con);  
      } 
      insert contactList;
   }
 }
 return res;
}

1 Answer 1

0

When this is called the second time, I suspect that the call to Database.insert will return a list of failed results as the account(s) exist already. So srList[i].id will always be null when your web service is called for the second time using the same account.

Change

Database.SaveResult[] srList = Database.insert(accList, false);

to

Database.UpsertResult[] srList = Database.upsert(accList, false);
5
  • May i know what i have to do..to resolve my issuse.
    – Sathya
    Jan 2, 2014 at 14:11
  • Given the existing code, the easiest way is probably to change your Database.insert to a Database.upsert.
    – FrankZ
    Jan 2, 2014 at 14:17
  • @thanq Frankz it's working.
    – Sathya
    Jan 2, 2014 at 14:29
  • May i know how can i check contacts exist or not..? how can i prevent duplicates contacts based upon lastname..??
    – Sathya
    Jan 3, 2014 at 10:21
  • There are multiple ways to do this depending on your business requirements. I suggest you try to implement it and when you struggle with something specific post this as a new question.
    – FrankZ
    Jan 3, 2014 at 12:30

You must log in to answer this question.

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