2

I have crated a RESTful endpoint on my sandbox. When it is called with the proper input it creates a new contact and then decorates a custom response class I created for that purpose and at the end it returns the result in JSON.
No problem so far. But my problem is after I create a contact I have to create a user for that contact in Salesforce and as you know standard objects such as User can not be inserted with other objects such as Contact in a same context (you get Mixed_DML error)
So the solution is after contact creation ,do the user creation in a @future method since it has a different context.

But now my question is how and when to return the response? The response should include the created contact and the user

  • 2
    You can hit the Salesforce REST API to create the user, which will return the record Id, and then in a subsequent call hit your custom endpoint passing the user Id ready for the contact. – Boris Bachovski May 12 '16 at 2:32
  • 1
    can't you use System.runAs() method to insert the user? – Novarg May 12 '16 at 6:38
1

You can try to send first API call to create contact and second to create user, so devide these logics. But this approach will appropriate only if you going to have not so much calls.

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.