Nov 20, 2022 03:14 AM
Hi, can anyone help with scripting?
I am loading a record from Salesforce with a number, an ID, and a quantity.
I want to then (with a script) automatically create more records (depending on quantity) with each record adding a 1 to the first records number.
So if the salesforce imported record had number: 000001, ID: XYZ, quantity: 5, this would be pulled into airtable and then a script would create 4 more rows as follows, then stop.
000002, XYZ
000003, XYZ
000004, XYZ
000005, XYZ
Can anyone help? Thank you so much in advance if so!
Nov 21, 2022 01:29 AM
Heya @Joe_Allen,
I wrote a script for you which does what you ask. I must admit, I was intrigued by the numbering and record creation order :thinking: , very peculiar… but I think the script I created should work for you - just adjust the input to work with the incoming Sales Force data (instead of the Script Extension manual input).
The above pic shows what the below Scripting Extension app and logic accomplishes;
let table = base.getTable("Sales Force Import");
let numberField = await input.textAsync('What is number the number string (include leading zeros)?');
let idField = await input.textAsync('What is ID?');
let qtyField = await input.textAsync('What is QTY?');
let recordId = await table.createRecordAsync({
"Number": numberField,
"ID": idField,
"QTY": Number(qtyField)
});
let myData = []
for(let i = 1; i < Number(qtyField); i++) {
myData.push(
{fields : {
"Number" : String(Number(numberField)+i).padStart(numberField.length, '0'),
"ID" : idField,
"QTY" : Number(qtyField)
}}
)
};
await table.createRecordsAsync(myData);
Nov 21, 2022 01:48 AM
That’s brilliant. Is it possible to automate it so it triggers automatically when my salesforce data gets pulled in? Thanks in advance, really appreciate your help!
Nov 21, 2022 02:44 AM
Sure thing.
I’m personally a big fan of Webhooks for simple systems connectivity. Here we have a sample JSON sent from an application called Postman, it’s simulating Data that could arrive from Salesforce that’s posted to the Airtable Automation WebHook;
We post our sample JSON package with Postman (using a raw JSON Body), and then test the Airtable trigger;
If all goes to plan, you’ll see “Step Successful” and your data available for use in the Airtable Automation script.
let inputConfig = input.config();
let table = base.getTable("Sales Force Import");
let numberField = inputConfig.inputNumber;
let idField = inputConfig.inputID;
let qtyField = inputConfig.inputQTY;
let recordId = await table.createRecordAsync({
"Number": numberField,
"ID": idField,
"QTY": Number(qtyField)
});
let myData = []
for(let i = 1; i < Number(qtyField); i++) {
myData.push(
{fields : {
"Number" : String(Number(numberField)+i).padStart(numberField.length, '0'),
"ID" : idField,
"QTY" : Number(qtyField)
}}
)
};
await table.createRecordsAsync(myData);
The main difference between the Scripting Extension script, and an Automation Script is as follows;
If all goes to plan - you can now test, see your test records create, and then turn on your Automation and send some more tests to it via Postman.
When you’re confident it’s working as desired, have a chat with your SalesForce dev team, and see if they can cater of a Webhook solution. There are other methods that they can choose over a webhook, but Webhooks (and postman) will at least allow for a quick demonstration of what you’re trying to achieve.
Right, time for bed! :sleeping: