Help

Script for deleting/updating on API server small modification

Topic Labels: Scripting extentions
753 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Ivan_Matasovic
4 - Data Explorer
4 - Data Explorer

I am working on importing and managing data of device details on the cloud with API. I am a beginner and don’t have experience with coding. I was wondering if you can help me because my team is struggling with the data management of our clients while waiting on developing new tools (released in 5 weeks). The old tool is broken at the moment…

We have airtable that can download (to airtable) and upload (to the server) info for the devices (about 4 fields) on airtable sheet. Using a script for that. I also saw script here in community that can delete all data from airtable sheet, but is it possible to use this script with the modification that deletes corresponding data on the server using api?

For example:

  1. I download a list of devices with info on 4 fields. (name, code, mac address, NFC number)
  2. Copy selected devices or rows in airtable (not all, just ones that I want to delete) to the new airtable sheet.
  3. Click the button in the new sheet to use scripts to delete all corresponding devices for the server using api.

I would really appreciate your help or forward me to the place where I can find similar scripts. Thank you.

EDIT:

Here will be the following steps:

  1. Download data in airtable from the server
  2. Add new records or rename existing
  3. Click on a script to update all data to the server (update or delete if it is deleted on airtable)

So far I have this script for updating records but it asks me to select a specific record or row in the table, I would like to not ask me, just replace/update/delete all records in the table to server - checking them and update them or delete them.

let auth_headers = new Headers();
auth_headers.append('Accept',"application/json, text/plain, */*");
auth_headers.append('Content-Type',"application/json, text/plain, */*");

let UpdateYesNo = await input.buttonsAsync('Are you sure you want to update this record?', [
    {label: 'Yes', variant: 'primary'},
    {label: 'Cancel'},
]);

if (UpdateYesNo === 'Yes'){
    let tableId = cursor.activeTableId;
    let table = base.getTable(tableId);
    let record = await input.recordAsync('', table);
    output.clear()
    if (table.name === 'LOCATIONS-DOWNLOAD'){
        await updateLocation(record);
    }else if(table.name === 'LOCATORS-DOWNLOAD'){
        await updateLocator(record);
    }else if(table.name === 'TAGS-DOWNLOAD'){
        await updateTag(record);
    }else if(table.name === 'ASSETS-DOWNLOAD'){
        await updateAsset(record);
    };
}else{
    output.markdown('_Canceled_')
}



// _________________________________________________________________________________________
// FUNCTION FOR LOGIN TO THE WEBSITE
async function login(){  
    output.markdown('Please wait...')
    let table = base.getTable('CREDENTIALS');
    let records = await table.selectRecordsAsync();
    let record = records.getRecord(records.recordIds[0]);
    let username = record.getCellValue("Username");   
    let password = record.getCellValue("Password");
    let login_url = 'https://api.servername.com/login';
    let creds = {"username": username, "password": password};
    let apiResponse = await fetch(login_url, {method:"POST", body:JSON.stringify(creds)});
    let user_data = await apiResponse.json();
    if (apiResponse.status === 200){
        let token = user_data['Token'];
        auth_headers.append('token',token); 
        return true        
    }else{
        output.markdown(`* ${user_data['message']}*`);
        return false
    }
}


async function updateLocation(record){
    let login_status = await login()
    if (login_status){
        let url = "https://api.servername2.com/location/"
        let location_type
        let payload = {}
        if (record.getCellValue('Location Type').name === 'Active'){location_type = 0}else{location_type =1}
        payload['LocationId'] = record.getCellValue('ID');
        payload['LocationName'] = record.getCellValue('Location Name')
        payload['LocationCode'] = record.getCellValue('Location Code')
        payload['LocationType'] = location_type
        let apiResponse = await fetch(url, {method:"PUT", headers:auth_headers, body: JSON.stringify(payload)});
        let responsText = await apiResponse.text()
        let feedback =  responsText.replace(/["':{}]/g, "").replace('message','');
        if (feedback === 'OK'){
            output.clear()
            output.markdown('Record updated successfuly.')
        }else{
            output.markdown(feedback)
        }
    }
};

async function updateLocator(record){
    let login_status = await login()
    if (login_status){
        let url = "https://api.servername2.com/locator/" 
        let payload = {}
        let locator_type
        if (record.getCellValue('Type').name === 'Wi-Fi'){locator_type = 3}else if(record.getCellValue('Type').name === 'Mobile'){locator_type =1}else{locator_type=2} 
        payload['LocatorName'] = record.getCellValue('Locator Name');
        payload['LocatorId'] = record.getCellValue('MAC')
        payload['LocatorType'] = locator_type
        payload['LocatorSerialNumber'] = record.getCellValue('Serial Number')
        payload['LocatorImsi'] = record.getCellValue('Locator IMSI')       
        let apiResponse = await fetch(url, {method:"PUT", headers:auth_headers, body: JSON.stringify(payload)});
        let responsText = await apiResponse.text()
        let feedback =  responsText.replace(/["':{}]/g, "").replace('message','');
        if (feedback === 'OK'){
            output.clear()
            output.markdown('Record deleted successfuly.')
        }else{
            output.markdown(feedback)
        }
    }
};

async function updateTag(record){
    let login_status = await login()
    if (login_status){
        let url = "https://api.servername2.com/tag/" 
        let tag_type
        if (record.getCellValue('Type').name === 'Mobile'){tag_type = 1}else {tag_type=2}                    
        let payload =  {"TagId": record.getCellValue('MAC'), "NFCId": record.getCellValue('NFC'), "TagType": tag_type};
        let apiResponse = await fetch(url, {method:"PUT", headers:auth_headers});
        let responsText = await apiResponse.text()
        let feedback =  responsText.replace(/["':{}]/g, "").replace('message','');
        if (feedback === 'OK'){
            output.clear()
            output.markdown('Record deleted successfuly.')
        }else{
            output.markdown(feedback)
        }
    }
};

async function updateAsset(record){
    let login_status = await login()
    if (login_status){
        let url = "https://api.servername2.com/asset/" + record.getCellValue('Asset ID')
        let apiResponse = await fetch(url, {method:"PUT", headers:auth_headers});
        let responsText = await apiResponse.text()
        let feedback =  responsText.replace(/["':{}]/g, "").replace('message','');
        if (feedback === 'OK'){
            output.clear()
            output.markdown('Record deleted successfuly.')
        }else{
            output.markdown(feedback)
        }
    }
};
0 Replies 0