Introduction

Deploying a SuiteCloud project in NetSuite sometimes involves encountering various challenges, one of which is the “Integration Record already exists” error. This error occurs when attempting to deploy a project that includes an Integration Record (IR) previously created in your development account. Let’s delve into the cause of this issue and explore an effective solution using the Gulp toolkit.

Understanding the issue

Before diving into the solution, let’s understand why this error occurs. The “Integration Record already exists” error typically surfaces when you attempt to deploy a SuiteCloud project that includes an IR created in your development account. Since you cannot deploy the SuiteCloud Project with the IR to the same Development account where it already exists, the deployment fails. This means the SuiteCloud Project must be redeployed without the IR objects to avoid this error.

Trick to handle the error

The key to resolving this issue lies in using Gulp to modify your deployment process. Specifically, you can configure Gulp to remove IR objects from your project only when deploying to your development account. This way, the deployment can proceed smoothly without encountering the “Integration Record already exists” error.

What is Gulp, anyway?

Gulp is a JavaScript-based toolkit that helps developers automate tasks like file manipulation and code minification. SuiteCommerce developers are likely familiar with Gulp, as it is used in the development of SuiteCommerce themes. For our purpose, we can leverage Gulp to automate the removal of IR objects during the deployment process.

Implementing the Gulp solution

Step 1: Install Gulp

First, ensure that you have Node.js installed. Then, install Gulp:

npm install -D gulp

Step 2: Configure the SuiteCloud Configuration

Modify your SuiteCloud Configuration in suitecloud.config.js to customize the deployment command to use the ‘dist’ folder.

module.exports = {
    defaultProjectFolder: "src",
    commands: {
        'project:deploy': {
            projectFolder: 'dist',
        },
    }
};

Step 3: Create a Gulpfile

In your SuiteCloud project’s root directory, create a gulpfile.mjs and define the tasks for copying entire files into the ‘dist’ folder and removing the IR objects.

import fs from 'fs/promises';
import path from 'path';
import gulp from 'gulp';

const dstDir = 'dist';
const srcDir = 'src';
const srcObjectsDir = path.join(srcDir, 'Objects');
const irFilePrefix = 'custinteg_';

/**
 * Cleans the dist directory.
 */
const cleanDstDir = async () => {
    await fs.rm(dstDir, {
        recursive: true,
        force: true,
    });
};

/**
 * Copies files from the source directory to the destination directory.
 * If the '--dev' flag is present in the command line arguments, integration files are excluded.
 */
const copySrc = async () => {
    // Check if '--dev' flag is present
    const isDeployToDev = process.argv.includes('--dev');

    // Define a filter function based on whether deploying to dev account or not.
    const fileCopyFilter = isDeployToDev ? 
        (source, destination) => {
            // Define a regular expression to identify strings that do not start with the prefix 'custinteg_' in the Object directory.
            const reg = new RegExp(`^(?!${path.join(srcObjectsDir, irFilePrefix)}).`);
            return reg.test(source);
        } : undefined;

    // Copy files from the src directory to the dist directory.
    await fs.cp('src', 'dist', { recursive: true, filter: fileCopyFilter });
};

export const preDeploy = gulp.series(
    cleanDstDir,
    copySrc,
);

Step 4: Configure the Deployment Scripts

Modify your deployment scripts in package.json to run Gulp before deploying to your development account.

 "scripts": {
   "deploy-dev": "gulp preDeploy --dev && suitecloud project:deploy",
   "deploy": "gulp preDeploy && suitecloud project:deploy"
 }

Step 5: Run the Deployment

Now, when you want to deploy to your development account, run the following command:

npm run deploy-dev

This command will execute the Gulp task to remove IR objects from the deployment package before deploying, thereby avoiding the “Integration Record already exists” error.

Conclusion

By leveraging Gulp, you can automatethe removal of the IR from your deployment package when deploying to an account where the IR already exists. This approach ensures a smooth deployment process preventing delays and interruptions in your development. Happy coding!