How to persist a value related to a webhook node during its execution

I’m creating a custom webhook node and facing to some challenges on how I should deal with the flow within the webhook function.

It’s a microsoft sharepoint webhook node, the notifications behavior is described here https://docs.microsoft.com/en-us/sharepoint/dev/apis/webhooks/overview-sharepoint-webhooks#receiving-notifications

There is an additional step after getting events from the webhook, I have to grab manually what has changed in the targeted resource, that should return an array of change events.

What I want to do now is to store the value of a marker provided by the last execution of this retrieval so that the next time I retrieve the dataset it will ignore all entries before the marker.

Is there a built-in feature to implement such a flow?

thanks

Welcome to the community @irzhywau!

If the data you are storing is very small you can use getWorkflowStaticData as for example here:

I hope that helps!

thanks for your quick reply @jan
I already use it in webhookMethods.default.checkExists and webhookMethods.default.create, but I could not make it work within webhook function.

I tried something like this, I checked in database but the value has not been stored

const workflowData = this.getWorkflowStaticData('node');
workflowData.lastChangeToken = "a string here"

That is strange. It should work. Does the workflow run successfully?

Ah also important to mention that it only gets saved to the database if you run in production-mode and not if you run it manually via the editor-UI.

yes, workflow succeeds
what do you mean by manually via editor-UI, webhook Test Mode?

anyway for both it’s not stored, I explictly activated the workflow to have it in production mode. Still same result.

What is the flow behind webhookData.<some-property> = value, where can I find it.
I just noticed that this statement seems working like a charm within webhookMethods functions but not in webhook implementation

PS. I’m quite new with n8n

So you activated the workflow and you used the production webhook and it did still not save it?

What do you mean with what is the flow behind it?

exact!

I’d just like to know wether the scope of execution of this statement matters. I noticed that in the webhook method this is as IWebhookFunctions and in webhooks.<name>.<method> it’s a IHookFunctions

So now I’m wondering if the concrete object returned by this.getWorkflowStaticData('node') on both scopes are the same. I don’t even know if that make sense actually.

Anyway here is the code:


async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
		const credentials = this.getCredentials(credentialName);
		if (credentials === undefined) {
			throw new Error('No credentials got returned!');
		}

		const webhookData = this.getWorkflowStaticData('node') as IDataObject;
		const listEvents = await customApi.retrieveListEvent();

        if (listEvents.length > 0) {
             // the issue is that the value I set here is not persiting
             webhookData.lastChangeToken = listEvents.pop().ChangeToken.StringValue as string
        }

       // ... return statement
}

@jan I’m wondering if I shouldn’t call a workflow save here to persist what has changed in webhookData. Is there a common way to do that witin this webhook function scope?

Sorry @irzhywau had to do some testing and check the code. You are right there was a bug and it did not get saved. I just fixed it and will release it with the next version. Will update here once released.

ah that’s good news, thanks for further checking this.
Is there a branch with the fix I can see?

No problem. Is simply in master.

thanks @jan for your valuable help

Got released with [email protected]

1 Like