0

I am new to Salesforce development (apologies in advance for not knowing all the Salesforce jargon) and I am trying to build a lightning component that will read fields from a record, add the values as arguments to a URL for use in calling a custom API and then write the response from the API call back to the record in a specific field.

I have managed to find all the info I needed through the Salesforce development forums to get the reading fields working (I used the force:recordData functionality) and I have setup the code for the HTTP call and the response is correct (I have struggled through the asynchronous JavaScript code and found a solution that is compatible with my API). But now I am now stuck at the last hurdle of trying to get the response into a specific field in the record. I have worked through the Force:editRecord documentation and it appears like it only works for manual editing with a pop-up window where a user can change information and save it to the record. I have tried many ways to set the value of the "recordData field" from the JavaScript Controller but I have not been able to find a working solution and I am beginning to think it might not be possible.

Is there a way to use Force:editRecord (or a similar lightning function) to take the HTTP response (String) and add it to a specific field in the record?

Any info would be greatly appreciated. I would be more than happy if you could just point me in the right direction.

Many thanks, Vince

P.S. Up until now I've managed to stay clear of Apex but I am willing to take the time to learn how to use it if there is no other way to do this

1 Answer 1

0

force:recordData can save data back to the server. To do so, simply grab the record data, modify it, then call the saveRecord function afterwards. Because of the asynchronous nature of data propagation in Lightning, I do believe you need to let the data catch up before attempting the save, however. That probably looks something like this:

var record = component.get("v.record");
record.Some_Field__c = callout.response;
// Trigger aura:valueChange
component.set("v.record", record);
// Allow event to propagate
setTimeout(
  $A.getCallback(
    function() {
      // Now save the record
      component.find("recordData").saveRecord(
        function(saveResult) {
          // handle save result here
        }
      );
    }
  )
);

You can clean this up a bit with some helper methods, but hopefully it should be enough to get you started.

5
  • Thanks for the response! This definitely helps :-) I have tried what you suggested and I think the syntax may be a bit off. When I read the fields from the record I use: var record = component.get("v.record"); var some_field_value = record.fields.Some_Input_Field__c .value; Should the syntax for writing to the field be: var record = component.get("v.record"); record.fields.Some_Output_Field__c .value = callout.response; I am still working through the code on saving the record so I can't test it but knowing the first part of the code is correct will help a lot. Commented Sep 6, 2018 at 10:14
  • @VinceBritz if you're using the complicated layout, yes, that should be approximately correct. I'd recommend using the simpleRecord format instead, since it'd be much easier to use.
    – sfdcfox
    Commented Sep 6, 2018 at 10:19
  • Thanks again, that will improve the readability of the code considerably. With regards to the recordData arguments, should I set the value for mode to "VIEW", "EDIT" or just leave it out? Commented Sep 6, 2018 at 10:56
  • @VinceBritz The default is View, which includes more fields than edit. You don't need to specify a parameter. You might want to look at the documentation.
    – sfdcfox
    Commented Sep 6, 2018 at 10:58
  • It all works, thank you very much for the help! (this is my first post on Salesforce StackExchange so it won't show your answer is the "solved" answer) Commented Sep 10, 2018 at 17:45

You must log in to answer this question.

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