Never successful making an HTTP request

Multiple times I’ve tried to use the http service in an automation script, and I’ve never been able to get past unresolved promise error.

My script is just trying to create a lead in a outreach automation tool when the entity meets a certain criteria inside of Fibery.

Can someone help me debug this issue?

const http = context.getService('http');
const fibery = context.getService('fibery');



async function main() {
    // Extract necessary data from the triggering entity
    for (let i = 0; i < args.currentEntities.length; i++) {
        const companyEntity = await fibery.getEntityById('CRM/Companies', args.currentEntities[i]['Companies 1'], ['Name', 'Website']);
        const triggerEntity = args.currentEntities[i];
        const companyName = companyEntity['Name'];
        const companyWebsite = companyEntity['Website']
        const linkedinUrl = triggerEntity['Linkedin'];
        const x = triggerEntity['X'];

        console.log(companyEntity + ': ' + companyName);
        console.log(companyEntity + ': ' + companyWebsite);
        console.log(`${linkedinUrl}, ${x}`);

    //Prepare data for the LaGrowthMachine API request
        const lagrowthmachineData = {
            audience: 'VSAAS',
            companyName: companyName || '',
            companyUrl: companyWebsite || '',
            linkedinUrl: linkedinUrl,
            twitter: x,
            // Add more fields as necessary
        };

        // Make POST request to LaGrowthMachine API
        try {
            const response = await http.postAsync('https://apiv2.lagrowthmachine.com/flow/leads?apikey=Your-external-apikey', {
                body: lagrowthmachineData,
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                }
            });

            console.log('Lead added to LaGrowthMachine successfully:', response);
        } catch (error) {
            console.error('Error adding lead to LaGrowthMachine:', error);
        }
    }
};

main();

Is there a reason why you are declaring main and then calling it?
You could just have the for loop followed by the POST request.

Anyway, I think this is not correct:

for (let i = 0; i < args.currentEntities.length; i++) {
        const companyEntity = await fibery.getEntityById('CRM/Companies', args.currentEntities[i]['Companies 1'], ['Name', 'Website']);
        const triggerEntity = args.currentEntities[i];

Assuming that the ‘company’ entity is linked to the ‘trigger’ entity via a to-one relation called Companies 1, then I think you would be better off doing this instead:

for (const triggerEntity of args.CurrentEntities) {
        const companyEntity = await fibery.getEntityById('CRM/Companies', triggerEntity['Companies 1']['Id'], ['Name', 'Website']);

If Companies 1 is a to-many relation, then you need to rethink what you’re trying to do, since there could be more than one company linked to a trigger entity.

Simple solution - since main() is async, you need to call await main()

Ahh thank you. Chalk it up to no being good at programming. Appreciate the insight.

It is actually a to many relationship, though 99% of cases there is only 1 entity linked. In every case I’m using this script I would be fine with calling the 1 item in the array. So should I always reference the [0] when getting the entity?

If you want to get info from a to-many relationship, you have to explicitly request it (it is not included in the entity by default) so you would probably need something like this:

for (const triggerEntity of args.CurrentEntities) {
    const triggerEntityPlus = await fibery.getEntityById(triggerEntity.type, triggerEntity.id, ['Companies 1']);
    const companyEntity = await fibery.getEntityById('CRM/Companies', triggerEntityPlus['Companies 1'][0]['Id'], ['Name', 'Website']);