Help

Mapping Json api data into a certain field in my airtable base

Topic Labels: API Scripting
5334 12
cancel
Showing results for 
Search instead for 
Did you mean: 
Anna_Marie_Wats
6 - Interface Innovator
6 - Interface Innovator

Hello,

 

I am pulling in data via json API and need to know where to go from here?  I have a table (sales team) that has the "salesrep id" and I need to map in the ytd sales data for each salesrep id. I am not sure what to do next to loop through the json object data and map them accordingly.

  

Screenshot 2024-03-11 at 09.08.08.png

Screenshot 2024-03-11 at 09.11.17.png

12 Replies 12

Hi Adam, Thank you so much. I will look into codeAcademy! 

To make sure I follow, I will have to manually put  the below piece into my airtable fields and the script? Also, the "A_Salesrep ID" is the same thing as the P21 Salesrep ID. They are the same thing so I need to find the sales rep id in airtable and match it with the data in the screenshot and update the YTD sales field. Ex for the picture: Salesrep ID 66902 is in airtable and when this automation is triggered it will find the salesrep ID in airtable and map in & updated the YTD sales of "1010899.750..."

 

Screenshot 2024-03-12 at 08.59.29.png

I need to get this data into the script. With the script above when I ran it updated the fields to "5.1", 3.08, etc.. instead of the data from the consol log. Sorry! I know I am super green. When starting Code Academy is it something I can do here and there and in free time or is something I need to be able to dedicate a lot of time too when I start it?

re: Also, the "A_Salesrep ID" is the same thing as the P21 Salesrep ID. They are the same thing so I need to find the sales rep id in airtable and match it with the data in the screenshot and update the YTD sales field. Ex for the picture: Salesrep ID 66902 is in airtable and when this automation is triggered it will find the salesrep ID in airtable and map in & updated the YTD sales of "1010899.750..."

Yeap, on the same page!

re: To make sure I follow, I will have to manually put the below piece into my airtable fields and the script? 

Oh nah, the `jsonData` variable in my script is used as an example and needs to be replaced by the result of your API call, and yeap you'll need to update it with your field names and table names!

And so your code should look a little like below:

 

let jsonData = await [FETCH CODE]

let table = base.getTable("Sales Team");
let query = await table.selectRecordsAsync();

let salesRepRecords = {};
query.records.forEach(record => {
    salesRepRecords[record.getCellValue("P21 Salesrep ID")] = record.id;
});

let updates = jsonData.map(data => ({
    id: salesRepRecords[data["A_Salesrep ID"]],
    fields: {
        "YTD Sales": parseFloat(data["YTD Sales"])
    }
}));

while (updates.length > 0) {
    await table.updateRecordsAsync(updates.slice(0, 50));
    updates = updates.slice(50);
}

 

There's going to be a tricky bit here where you need to return the JSON from your API call; if you can DM me a link to a duplicated example base I could install the script for you real quick!  I totally understand if there's a security concern, and if it works better we could hop on a quick call with screenshare and we could set it up together!