# URL Based Scripts - Concurrents

These are scripts which are executed through endpoint calls. These scripts are stored in the folder /integrationm/concurrent/ of the project directory, this is why they are also called concurrent scripts. They are only accessed through HTTP/POST methods with the url /integrationm/concurrent/<script_name>.

# How to read the arguments sent on the body

The posted arguments are stored in the argsMap map object, and can be accessed by invoking their names directly. Example:

fetch(`/integrationm/concurrent/script_name`, {
		method: "POST",
		headers: {
				"Content-Type": "application/json",
		},
		body: JSON.stringify({email}),
});

...
In the groovy file:

def requestedEmail = argsMap.email ? argsMap.email : null;

# The response

In order to send a response to the frontend, the developer must include a return statement at the end of the groovy file. This return statement must not be inside any function. For example: /integrationm/concurrent/test.groovy

String requestedEmail = argsMap.email ? argsMap.email : null;
if (requestedEmail == null || requestedEmail.length() == 0 || !requestedEmail.contains("@")) {
    return json(400, [msg: "Missing required properties"]);
}

...

return json(200, ["success": true])

# Permission

The user performing the action that calls a URL to execute a script must have the required permissions to execute the script: actions:execute:<script_name_without_the_groovy_exxtension>

# Example

# Backend groovy file

The groovy file /integrationm/concurrent/registerColaborator.groovy

def getOrganization(emailOrDomain) {
    def query = "dominios_email_associados:\"${emailOrDomain}\"";
    def searchResult = recordm.search("Organizations", query, ["size": "2"]);
    return searchResult.getHits().get(0).getId()
}

String email = argsMap.email ? argsMap.email : null;

def emailDomain = email.split("@")[1];
def idOrganization = getOrganization(emailDomain);

if (idOrganization > 0) {
    def result = recordm.create("Colaborators", ["E-mail": email, "Company": idOrganization])
    if (result.success()) {
        return json(200, ["success": true])
    } else {
        return json(500, ["success": true])
    }
}

return json(404, ["notFound": true])

The user can find the documentation about the methods createand search here (opens new window)

# Frontend code

The frontend code:

fetch(`/integrationm/concurrent/registerColaborator`, {
		method: "POST",
		headers: {
				"Content-Type": "application/json",
		},
		body: JSON.stringify({"[email protected]"}),
});

# The permission that the user must have:

actions:execute:<registerColaborator>