Help

Re: Scripting - add records?

2563 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Joe_Allen
4 - Data Explorer
4 - Data Explorer

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!

3 Replies 3

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).

image

The above pic shows what the below Scripting Extension app and logic accomplishes;

image

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);
Joe_Allen
4 - Data Explorer
4 - Data Explorer

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!

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;

image

We post our sample JSON package with Postman (using a raw JSON Body), and then test the Airtable trigger;

image

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;

image

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.

image

Right, time for bed! :sleeping: