How to recieve response from an API and use that response to send another webhook request

I am using gravity form. I have a webhook to create a lead on my CRM. I have successfully done that. But I need to create another entity on CRM. in order to create that entity I need the id that lead just create through webhook. API returns lead information. here is the response look likes :

2024-05-10 20:26:25.959583 - DEBUG --> GF_Webhooks::process_feed(): Webhook successfully executed. code: 200; body: {"id":"663e2e91dd14cd70c","name":"example name","deleted":false,"firstName":"why lead id is not working","status":"Free Trial Received","source":"Website","industry":null,"website":"hluluasd.com","emailAddress":"leadid@gmail.com","doNotCall":false,"createdAt":"2024-05-10 14:26:25","modifiedAt":"2024-05-10 14:26:25","targetListIsOptedOut":false,"country":"Other","leadType":"Photographer","leadPriority":"High","collectedFrom":"Google Map","companySize":"1-10","timeZone":"Est","emailAddressIsOptedOut":false,"emailAddressIsInvalid":false,"phoneNumberIsOptedOut":false,"phoneNumberIsInvalid":false,"emailAddressData":[{"emailAddress":"leadid@gmail.com","lower":"leadid@gmail.com","primary":true,"optOut":false,"invalid":false}],"phoneNumberData":[],"createdById":"6638bdf8d528b68c3","assignedUserId":"639af1ec711c3e701","assignedUserName":"Minhajur Rahman","teamsIds":[],"teamsNames":{},"isFollowed":false} 
2024-05-10 20:26:25.960020 - DEBUG --> GF_Feed_Processor::task(): Marking entry #88 as fulfilled for gravityformswebhooks 

I have grab this from logging from gravity forms. I want to use this data and tigger another webhook request. I have tried gform_webhooks_post_request and this is the code in functions.php :

add_action( 'gform_webhooks_post_request', 'capture_lead_id', 10, 4 );

function capture_lead_id( $response, $feed, $entry, $form ) {
    // Log the entire response to the server for debugging
    error_log( 'Webhook Response: ' . print_r( $response, true ) );

    // Check if the response is successful
    if ( ! is_wp_error( $response ) ) {
        // Extract lead ID from the response
        $lead_id = isset( $response['id'] ) ? $response['id'] : '';

        // Log the lead ID to the server
        error_log( 'Lead ID: ' . $lead_id );
    }
}

I can’t grab the lead ID. :frowning: it is empty

Logging indicates the response body is JSON, so you’ll need to decode it before you access the id e.g.

$body_json = wp_remote_retrieve_body( $response );
$body_array = json_decode( $body_json, true );
$lead_id = rgar( $body_array, 'id' );
1 Like

Thank you @richardw8k . The code is working. How can I use this data in another webhook payload and trigger that webhook?

Also, I noticed, that even my form submission was invalid, meaning let’s say a field was marked as unique, If I put a duplicate value in my front-end form, it shows an error and didn’t submit the form but Webhook executes!

Mapping response data into fields doesn’t require any code with Gravity Flow’s Outgoing Webhook step.

  • If the CRM API returns a different response code (200 or 500 range?) based on the duplicate value, you can use the Next Step settings to control where the workflow proceeds to. A User Input step to ask the original form submitter for new values perhaps?
  • If the CRM API returns a 400 range response code but different data in the body, you can map that into a specific field, and then use the conditional logic on a subsequent step to determine if it should occur or not.

The User Input doc page linked above describes creating loops with Approval steps, your case would be a loop with the Webhook and User input steps until everything is right to either finish the workflow or move on to whatever post-successful-webhook activity is required.

1 Like