Skip to main content

Hi, 

I have an action that retreives multiple records from salesforce. 

I have a cognito form with a dynamic table and I want to populate the entry with the record data. 

I created a zapier code action with the following parameters and I am getting errors. 

I am not sure if I am going about this the right way and would appreciate any assistance. 

 

Code by zapier

Event: Run Javascript

Input data: Records, Records

Code

let records = inputData.Records; // Ensure 'Records' matches the actual field name from input

// Check if records is undefined or null
if (!records) {
  throw new Error('Records input is undefined or null');
}

// Parse records if it's a string
if (typeof records === 'string') {
  records = JSON.parse(records);
}

// Ensure records is an array
if (!Array.isArray(records)) {
  throw new Error('Records input is not an array');
}

// Aggregate the data into the desired format
let aggregatedData = records.map(record => {
  return {
    Name: record.Name,  // Adjust field names to match your specific needs
    CreatedDate: record.CreatedDate,
    LastModifiedDate: record.LastModifiedDate,
    // Add more fields as necessary
  };
});

// Return the aggregated data as a JSON string
return { aggregatedData: JSON.stringify(aggregatedData) };
 

Hi @ABruk 

What is the encountered error? (post screenshots)

Also, post screenshots with how your Zap Code step is configured in EDIT mode.

TIP: Try asking ChatGPT for help checking the JavaScript.


Below is the screenshot of the encountered and the zap code step configuration

 


@ABruk 

2 Issues

 

Mapped variable for ‘Records’ has “No data”

 

Input Data variable is capitalized as “records”. (all lowecase)

In the Code it is referenced as: inputData.Records (propercase)

There is a mismatch in case: records vs Records

 


What I don’t understand is why does mapped variable have no data? The get records action returned two records as can be seen in the screenshot below.


@ABruk 

Here’s why:

The Records variable itself has no data.

But the fields within the records array have data. (e.g. Records > ID, Records > Name, etc.)

Zapier parses the returned array items into their own key/value pairs to then map across Zap steps.

 

 


So what should be the input for the code action? 


@ABruk 

You may not need a Code step because the variables you are trying to return from the Code step are already returned in Step 4.

  1. Name
  2. Date - Created
  3. Date - Last Modified

 

 

If you are trying to iterate on the Records returned from Step 4, then use Looping (Create Loop from Line Items): https://zapier.com/apps/looping/help


Is there any way to use the zapier loop without creating seperate form entries for each record?


@ABruk 

Can you give us more context about what you are trying to do with the data in Step 6 or Step 7? (screenshots showing the fields so we can see the type/description)


Trying to create one cognito entry. 

see screenshots below for the setup on the cognito form


@ABruk 

Can you show screenshots from Zap Step 7 in EDIT mode so we can see the field types and field descriptions for more context?


 


Hi @ABruk,

If I understand your question clearly, I think the Looping by Zapier app will prevent the Zap from creating separate form entries for each records.

More about Looping by Zapier here: https://help.zapier.com/hc/en-us/articles/8496106701453-Loop-your-Zap-actions

Please let me know if I was able to point you in the right direction. If not, please don’t hesitate to let me know. 🤗


Reply