The Community forums are being phased out in favor of a new Slack group.
Add your email address below to get an invitation to join the community slack group

Slack Signup
Newsletter Optin
Help Desk

Save more then 1 API Response in a Field

Labels

This Discussion is public

Notifications

Hi everyone,

last year I integrated a API / Automation.

The API Response Code that is shared in the documentation page doesn’t work with my challenge because I got a response of a response and every time all the IDs I set, is getting overwritten with the same value.
Now I try to fix that (2 days coding :'-( ) and sometimes it worked an other time it doesn’t.

I add some code in the snippet as u can see:

######################################

add_action( 'frmapi_post_response', 'frm_save_api_response', 10, 3 );
function frm_save_api_response( $response, $entry, $form_action ) {
$returned_id = $response['body']; // this line will change based on the API you are sending to
$json = json_decode($returned_id);
$id = $json->{'id'};
$field_value150 = (int)FrmProEntriesController::get_field_value_shortcode(array('field_id' => 150, 'entry' => $entry->id)); // Zero 0
$field_value197 = (int)FrmProEntriesController::get_field_value_shortcode(array('field_id' => 197, 'entry' => $entry->id)); // Zero 0
$field_value198 = (int)FrmProEntriesController::get_field_value_shortcode(array('field_id' => 198, 'entry' => $entry->id)); // Zero 0
// echo $field_value150;
// echo $field_value197;
// echo $field_value198;

if ($id && ($field_value150 == 0)) {
FrmProEntryMeta::update_single_field( array(
'entry_id' => $entry->id,
'field_id' => 150, // change to the ID of the field to change
'value' => $id,
) );
}
else if ($id && ($field_value150 != 0 && $field_value197 == 0)) {
FrmProEntryMeta::update_single_field( array(
'entry_id' => $entry->id,
'field_id' => 197, // change to the ID of the field to change
'value' => $id,
) );
}
else if ($id && ($field_value197 != 0 && $field_value198 == 0)) {
FrmProEntryMeta::update_single_field( array(
'entry_id' => $entry->id,
'field_id' => 198, // change to the ID of the field to change
'value' => $id,
) );
}
}

###############################################

I need the Values of each Response to set a new API POST.

  1. POST API > Create Account -> get AccountID Response (Field 150)
  2. POST API > Create a Quote with the AccountID -> get QuoteID Response (Field 197)
  3. POST API > Create a QuoteLineGroup with the QuoteID -> get the QuoteLineGroupID (Field 198)
  4. POST API > Create each QuoteLines with the QuoteLineGroupID -> get the QuoteLineID but i dont need them! Maybe i want to store them just 4 Information

Has anyone a idea what i can do better in this code so that it works reliable?

Thank u for u Answers

 

RSol

I don't understand what you mean by "because I got a response of a response and every time all the IDs I set, is getting overwritten with the same value."

This situation is unique to your site. While you understand what you're trying to accomplish, it's really difficult for us to understand with no knowledge of your site to jump into unfamiliar code and tell you how to improve it without a deeper level of debugging that's only available through the back end.

I suggest you install a Kint plugin so you can easily view the value of the $response array. Truthfully, if all of the fields you are querying are in the same form entry, the entry_id will be the same for all of the returned values anyway. So without a better understanding of your site's architecture, providing you with an accurate answer isn't possible. We'd only be guessing.

Hello vfontjr,

thanks for your "Response". :-)

 

I would like to implement the following function.

On my page, the interested parties can make a booking request.

There are several options that I let the customer choose for the first selection.

As soon as the applicant submits the form, an account in my CRM should be created with the help of the API. The CRM sends a response (AccountID) and I would like to write that in a field with the PHP code.

With a further API post I can then create an offer draft with the AccountID. The API Post again generates a response from my CRM and I would also like to save it in a field since I need it for another API post.

To keep it short. With the standard code I can write an API response from my CRM once in a field. But as soon as I want to save multiple responses in the entry, I have to expand the code accordingly.

With the IF query I have tried to match the empty fields to make sure that the right field is filled with the right response.

THIS IS THE ORIGINAL CODE FROM FORMIDABLE
#############################################
add_action( 'frmapi_post_response', 'frm_save_api_response', 10, 3 );
function frm_save_api_response( $response, $entry, $form_action ) {
$returned_id = $response['body']['CHANGEME']; // this line will change based on the API you are sending to
if ( $returned_id ) {
FrmProEntryMeta::update_single_field( array(
'entry_id' => $entry->id,
'field_id' => 25, // change to the ID of the field to change
'value' => $returned_id,
) );
}
}
#############################################

I hope that is a bit more understandable.

Best regards

RSol

In your code, you're only making 1 API request. It's possible that you need to be making 4 API requests if I'm understanding your requirements properly.

Also, Formidable's code is showing a multidimensional array. $response['body']['CHANGEME'];

In your code, you changed this to just return the body. What are the values in the other dimensions (the CHANGEME part)?

This is why I suggested you use Kint. After you install Kint, you simply add d($response['body']); right after the function statement and you'll see exactly what the multidimensional array contains. You can also do a var_dump if you don't want to install the kint plugin. Either way, you need to be able to see the entire response so you know what you're dealing with.

vfontjr, ur right. i only making 1 API request each time.

I add the code with the "code snippet" plugin.

I bought Formidable Automation. So i use a timer.

After submit Form > save response in Field (the code)
After 1 min (minimum Time) send the next API POST then save in the next Field
After 1 min send next API POST ....

At first I tried to copy the code and adapted the variables. Until then I noticed that with the function all fields with the same response fully writes. That's why I tried to exclude this overwriting in one function.

I would find it best if you submit the form and I can leave these timers. But automation does not offer this function.

So it takes about 5 min till the whole Account and Quote is build in my CRM but its okay for me. I dont get 5 booking request every day. ;-)

AND sorry... i dont need the information about the RESPONSE i saw it in Postman APP. Its everytime the same Type. My CRM only response a ID with the Value. So it matches with the function.

Does anyone please have a solution for my problem?

Short discribtion

"Save delayed incoming API Responses in different entry fields."

My Solution doesnt really work safe.

Discussion closed.