⚙ Operations Hub

Wakira
Participante

Custom Code workflow - Call External API

Hi ,

 

I try call a external api(third party) in mi workflow using custom code, but its not working.

 

I get this message:

"ERROR Unhandled Promise Rejection"

 

Would it be possible to make an external call for this functionality?

Thanks

 

5 Respostas 5
RVerma4
Membro

Custom Code workflow - Call External API

Deal  assiciated with company andcompany associated with contact then company contact should associate with deal how I can do this with custom code in workflows @Wakira  @stefen 

0 Avaliação positiva
PamCotton
Gerente da Comunidade
Gerente da Comunidade

Custom Code workflow - Call External API

Hello @Wakira could you please provide us with more details on your custom code without any private information? This way we will be able to investigate more about it and provide you with the next steps. 

I would also like to invite our top experts @LMartinez7 @bryce-corey  @stefen any recommendations to @Wakira?

 

Thank you,

Pam

Você sabia que a Comunidade está disponível em outros idiomas?
Participe de conversas regionais, alterando suas configurações de idioma !


Did you know that the Community is available in other languages?
Join regional conversations by changing your language settings !




Wakira
Participante

Custom Code workflow - Call External API

HI @PamCotton , thanks for yout attention.

The process is basically, we use a ticket as a control to monitor the delivery of one of our products, in this ticket there is a property that is the tracking code that needs to be consulted constantly in the carrier's api and update the ticket status property.

I tried something similar to this (with changed values):

const axios = require('axios')

exports.main = (event, callback) => {

  const headers = {
    'Authorization': 'Token 1234',
    'Content-Type': 'application/json'
  };

  const requestBody = {
    "data": {
     "df": 
    { "nf" : "11222",  
      "tpDocumento":1 
      }    
}
  }

  axios
    .post(
      'www.APICarrier.com/api/tracking',
      requestBody,
      { headers: headers }
    )
    .then(response => {
      console.log('Response from API: ${response.body}');
    });
};

 

0 Avaliação positiva
stefen
Conselheiro(a) de destaque | Parceiro
Conselheiro(a) de destaque | Parceiro

Custom Code workflow - Call External API

@Wakira sounds like you need to see what the actual error is. Typically a "unhandled promise rejection" means you aren't handling a failed response. To do that you should be able to update your axios function to something like this:


axios
    .post(
      'www.APICarrier.com/api/tracking',
      requestBody,
      { headers: headers }
    )
    .then(response => {
      console.log('Response from API: ${response.body}');
    }).catch(function (error) {
        if (error.response) {
          // Request made and server responded
          console.log(error.response.data);
          console.log(error.response.status);
          console.log(error.response.headers);
        } else if (error.request) {
          // The request was made but no response was received
          console.log(error.request);
        } else {
          // Something happened in setting up the request that triggered an Error
          console.log('Error', error.message);
        }
    });
Stefen Phelps, Community Champion, Kelp Web Developer
0 Avaliação positiva
Wakira
Participante

Custom Code workflow - Call External API

 @LMartinez  @bryce-corey   @stefen @Pam Do you have any guidance or help on how I can resolve this?

thanks

0 Avaliação positiva