There are 2 objects. Object A and object B with same fields. There is no relationship between them (no lookup). When a record is inserted in object A, object B should be checked to see if the same record exists. If it exists then update it else create a new one. How do I do this. Please assist.

Regards, Sfdcdev

  • 1
    Hi sfdcdev. Your question appears to be asking for other people to write code for you. SFSE is a Q&A forum where we look to help each other by asking and answering questions about real problems or errors with our code. Posting requests to write code for you will get closed. If you post what you've written so far and where you're stuck, I'm confident you'll find plenty of people willing to assist you
    – Himanshu
    Jul 27, 2016 at 3:31
  • Hi Himanshu, My intension was not to ask others to write code. But I was looking for some help to get started. I will make sure to add the code that I have written if any going forward and see that my question does not look like asking for code to be written by others.
    – sfdcdev
    Jul 27, 2016 at 6:04
  • yes. you should always add some code and research what you have doe so far to achieve your requirement. if you still have your code edit your question and add your code. :)
    – Himanshu
    Jul 27, 2016 at 6:09

1 Answer 1


You'll probably need a trigger on Object A:

trigger on ObjectA (after insert){

public class TriggerHelper{
  public static void checkExistenceInObjectB(List<ObjectA> l){
   //some code to query objects B based on your objects A criteria
   //some code to treat or create objects B
   //insert or update your list of objects B:
   upsert listOfObjectsB;

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.