Altaf Kalpatt
Altaf Kalpatt

Reputation: 1

Facing "code 404. Truncated server response" error when using Pipedrive API for fetching leads

I am trying to create a code where I can fetch all the leads from Pipedrive via an App Script and allocate the leads there.

I am getting this error "Exception: Request failed for https://api.pipedrive.com returned code 404. Truncated server response: {"status":false,"error":"Unknown method ."} (use muteHttpExceptions option to examine full response)" and not sure what to do.

The error is pointing to this fuction

function fetchLeads() {

  // Fixed 
  custom_fields = {
      'Annual Volume':'a34088d0a923af5494c5f3ce114cb4b2f3c40b85',
      'Platform': '0be41c69233227d207bb44530185c7ef9a7f8b97',
      'Application ID': '9f2a22d43753f142be8d6f07dc40978393b81b06',
      'Channel':'4e105d77d6d4f99413f8434dfb781ceb3eb77999',//480=online, 481=offline, 482=online&offline
      'CandidateStage':'b90ecc9f2633d78bad3102a085150f06a7009510' //399=Completed, 463=Rejected, 395=not submitted, 396=submitted, 397=pending scoring, 398=bo incomplete
  }

  annual_volumes = {
      132: 'less_than_1m',
      133: 'from_1m_to_5m',
      134: 'from_5m_to_15m',
      135: 'more_than_15m'
  }

  filter_id = 603

  Logger.log("https://api.pipedrive.com/v1/leads?filter_id=" + filter_id + "&api_token=" + api_token)

  // Get unassigned leads
  var response = UrlFetchApp.fetch("https://api.pipedrive.com/v1/leads?filter_id=" + filter_id + "&api_token=" + api_token);
  var resp = JSON.parse(response.getContentText());
  leads = resp.data



  Logger.log(leads.length);

  leads_final = []

  for (i=0; i<leads.length; i++ ) {

    // Get person for each lead (we need this to get phone numbers)
    var response = UrlFetchApp.fetch("https://api.pipedrive.com/v1/persons/" + leads[i].person_id + "?api_token=" + api_token)

    var resp = JSON.parse(response.getContentText());
    var person = resp.data
  
    phone_num = person.phone[0].value

    // Infer country based on phone numbers
    if (phone_num.startsWith('+971') | (phone_num.startsWith('971'))) {
      leads[i].country = 'emirates'
    }
    else if (phone_num.startsWith('+966') | (phone_num.startsWith('966'))) {
      leads[i].country = 'saudi'
    }
    else if (phone_num.startsWith('+20') | (phone_num.startsWith('20'))) {
      leads[i].country = 'egypt'
    }
    else if (phone_num.startsWith('+965') | (phone_num.startsWith('965'))) {
      leads[i].country = 'kuwait'
    }
    else if (phone_num.startsWith('+974') | (phone_num.startsWith('974'))) {
      leads[i].country = 'qatar'
    }
    else {
      leads[i].country = 'unknown'
    }  
    
    // leads_final.push({
    //   "lead_id": leads[i].id,
    //   "lead_title": leads[i].title,
    // })

    lead_id = leads[i].id
    lead_title = leads[i].title
    annual_volume = leads[i][custom_fields['Annual Volume']]
    phone_num = leads[i].phone_num
    platform = leads[i][custom_fields['Platform']]
    annual_volume = annual_volumes[annual_volume]
    country = leads[i]['country']
    channel = leads[i][custom_fields['Channel']]
    candidatestage = leads[i][custom_fields['CandidateStage']]

    leads_final.push([lead_id, lead_title, annual_volume, platform, country,channel,candidatestage])

  }
  //console.log(channel);
  for (i=0;i<leads_final.length;i++ ) {
    console.log(leads_final[i][6]);
  }
  return leads_final
}

To get a list of all the leads that are present in that filter of Pipedrive

Upvotes: 0

Views: 30

Answers (1)

Altaf Kalpatt
Altaf Kalpatt

Reputation: 1

Okay turns out this isn't the error everyone else is facing due to URLFetchApp, it's because some data fields in Pipedrive were empty but there still seems be something wrong with it because it shows error with complete leads as well

Upvotes: 0

Related Questions