Help

Re: Script Extension conversion to Button click script

22522 0
cancel
Showing results for 
Search instead for 
Did you mean: 
ericatihs
4 - Data Explorer
4 - Data Explorer

Here the working script extension:

 

// Create script output description
output.markdown( "# Stores with 2+ WO in 30 days" ); 

// Set Table
const woTable1 = base.getTable( "BR_Details" ) 

// Set View
const woView1 = woTable1.getView( 'Stores with 2+ WO in 30 days' ) 

// Get records from view
const woRecords1 = await woView1.selectRecordsAsync().then(result => result.records ) 
console.log( woRecords1 ) 

// Set new report name and description
const reportName1 = 'Stores with 2+ WO in 30 days v01' 
const reportDescription1 = 'Locations with 2 or more Work Orders in the last 30 days v01' 

// create new report including linking of tasks
const reportsTable1 = base.getTable( "Reports" ) 
const reportRecord1 = await reportsTable1.createRecordAsync( 
{
"Name" : reportName1 
, "Description" : reportDescription1 
, "Linked BR_Details" : woRecords1 
} )

if( reportRecord1 ) // Update
{
const res1 = await remoteFetchAsync( 'https://api.docsautomator.co/createDocument' 
, {
method : 'post'
, headers : {
'Content-Type' : 'application/json'
, 'Authorization' : 'Bearer 00fb-removed-f0a4ce67a72d'
}
, body : JSON.stringify(
{
recId : reportRecord1 
, docId : '669428e9ce0e670ac3eeaf03' 
} )
}
)
console.log( res1 ) 
console.log( await res1.json() ) 
}

Here is the revised Button click script:

// Create script output description
// output.markdown( "# Stores with 2+ WO in 30 days" ); 

// Set Table
const woTable1 = base.getTable( "BR_Details" ) 

// Set View
const woView1 = woTable1.getView( 'Stores with 2+ WO in 30 days' ) 

// Get records from view
const woRecords1 = woView1.selectRecordsAsync().then(result => result.records ) 
console.log( woRecords1 ) 

// Set new report name, description, status and report nbr
const reportName1 = 'Stores with 2+ WO in 30 days v01' 
const reportDescription1 = 'Locations with 2 or more Work Orders in the last 30 days v01' 
//const reportStatus1 = 'selKruKeOGJP69cpI' 
const reportNbr1 = 50 

// create new report including linking of tasks
const reportsTable1 = base.getTable( "Reports" ) 
const reportRecord1 = reportsTable1.createRecordAsync( 
{
"Name" : reportName1 
, "Description" : reportDescription1 
, "Linked BR_Details" : woRecords1 
, "Report Nbr" : reportNbr1 
} )
// , "Status" : reportStatus1 

if( reportRecord1 ) 
{
, {
method : 'post'
, headers : {
'Content-Type' : 'application/json'
, 'Authorization' : 'Bearer 00fb617c-removed-f0a4ce67a72d'
}
, body : JSON.stringify(
{
recId : reportRecord1 
, docId : '669428e9ce0e670ac3eeaf03' 
} )
}
)
console.log( res1 ) // Update
// console.log( res1.json() ) // Update
}

// Create script output description
// output.markdown( "# Stores with 3+ WO in 60 days" ); 

// Set Table
const woTable2 = base.getTable( "BR_Details" ) 

// Set View
const woView2 = woTable2.getView( 'Stores with 3+ WO in 60 days' ) 

// Get records from view
const woRecords2 = woView2.selectRecordsAsync().then(result => result.records ) 
console.log( woRecords2 ) 

// Set new report name, description, status and report nbr
const reportName2 = 'Stores with 3+ WO in 60 days v01' 
const reportDescription2 = 'Locations with 3 or more Work Orders in the last 60 days v01' 
// const reportStatus2 = 'In-Progress' 
const reportNbr2 = 60 

// create new report including linking of tasks
const reportsTable2 = base.getTable( "Reports" ) 
const reportRecord2 = reportsTable2.createRecordAsync( 
{
"Name" : reportName2 
, "Description" : reportDescription2 
, "Linked BR_Details" : woRecords2 
, "Report Nbr" : reportNbr2
} )
// , "Status" : reportStatus2 

if( reportRecord2 ) 
{
, {
method : 'post'
, headers : {
'Content-Type' : 'application/json'
, 'Authorization' : 'Bearer 00fb617c-removed-f0a4ce67a72d'
}
, body : JSON.stringify(
{
recId : reportRecord2 
, docId : '6696bbaace0e670ac3eff807' 
} )
}
)
console.log( res2 ) 
// console.log( await res2.json() ) 
}

The revised script fails with:

 

Error: Field "fld7oGCicGXHK4kjP" cannot accept the provided value.
    at main on line 83

 Line 83: const res2 = fetch( 'https://api.docsautomator.co/createDocument'
FYI: Community said it removed HTML ... Thanks for that!!!!!!!!!!!! 

3 Replies 3

What specific issues are you facing with the conversion? 

Or if you're looking to hire someone to do this I'd be happy to help too!  Might want to open a thread in the Job Board as well: https://community.airtable.com/t5/job-board/bd-p/jobs

Hello @ericatihs ,
As per my first glance if this is an error on line 83 then it's just an issue with the fetch function you're using.
while you use it on automation script that time fetch works because it's run on the Airtables server.

while we use it on extension script you need to use remoteFetchAsync. Because it's run on our browser.

If this is not your solution then write a message or come up with original screenshots.

Hi,
use this script to see field name by ID or part ('4kjP' is enough)

let f_id=await input.textAsync('field ID or part'); 
let fid=arr=>arr.filter(el=>el.id.includes(f_id));
output.table(base.tables.filter(n=>fid(n.fields).length).
map(t=>[t.name,fid(t.fields).map(f=>f.name).join()]))

I suppose your "Report Nbr" field is text and you are trying to put a number there.
change field type or quote the number to use it as text 

const reportNbr2 = '60' 


But I'm just guessing.

Also, please use code tag for code,

Alexey_Gusev_0-1721940076424.png

otherwise it's too hard to read.

p.s. sometimes, when you want to submit and it complains about HTML, you just need to press submit again.