I am updating and inserting some records in salesforce, through a SOAP API service.
The procedure, corresponds to a simple update or insert to the fields of an Account record. It is required to handle the use case, where a Business Account(record type), changes into a Person Account(the standard record type, that gets activated, by salesforce, on your demand), or vice versa.
Due to the special nature of Person Accounts RT, I have designed the class, so it first sets the record type(FIRST DML OP), and then I commit in the database the other info of account records, in a second moment(i.e. Phone, email etc.)(SECOND DML OP).
Before commiting the second DML OP, i perform some data validation, according to the data received from the SOAP Request.
What I need, is a logic, that rolls back, the records modified in the FIRST DML OP, if they fail the custom validation, that I have specified, before the SECOND DML OP.
I've thought about using Database.SavePoint() & Database.Rollback(), but that rolls back all the records validated successfully. I want only to roll back, the update of the record type, for the records not validated.
Q: Why do I set the record type, separately, in a first DML operation?
A: Because, if you want to change the record type, from a person account, to another record type, or vice versa, you have to do it separately. Than you modify the fields, accordingly, to your business logic. This is workaround is also the only way suggested by Salesforce.