0

I am trying to insert records to an external object and get the Id back for the inserted record

I have the following piece of code

 Database.SaveResult[] insertResultAccount = Database.insertAsync(externalAccountsToBeInserted); 
    if(!insertResultAccount[0].isSuccess()){
        String locator = Database.getAsyncLocator(insertResultAccount[0]);  
            completeAccountCreation(locator);
    }


 @future
public static void completeAccountCreation(String locator){
    Database.SaveResult sr = Database.getAsyncSaveResult(locator);
    System.debug(sr.isSuccess()+'locator:::'+locator);
    if(sr.isSuccess()){
        System.debug('2.Successfully inserted Account__x. Account__x ID: ' + sr.getId());     
    }
}

My problem here us that the debug sr.isSuccess() is not coming as success so I am not being able to fetch the external AccountId.

Here I have to bulkify the method completeAccountCreation. For the time being and I just checking for a single record.

I tried another alternative with after extending DataSource.AsyncSaveCallback , but my problem here is that I will not be able to bulkify the processSave method, because we can just pass a single (Database.SaveResult as it's parameter.

 override public void processSave(Database.SaveResult sr) {
    if(sr.isSuccess()){
        System.debug('2.Successfully inserted Account__x. Account__x ID: ' + sr.getId());    
    }
}
  • Actually, it is an sync operation. The saveresult will contain a tracking link from where you can know the status of the operation. See this salesforce.stackexchange.com/questions/190218/… and blog.enree.co/tag/apex/page/2 – manjit5190 Mar 9 at 17:39
  • Also the callback is called for each record, you can bulkify the operation. – manjit5190 Mar 9 at 17:40
  • @manjit5190, In the future method approach, I don't just need the status if the external record has been created or not. I also need to have the Id of the created record, because I have some further logic with the same. Also, in the second approach processSave is a standard method which runs for individual record. How will I be able to bulkify the parameters? – user55791 Mar 9 at 19:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.