Creating WebHooks in CRMScript?

Hi guys,

Trying to get WebHooks to work in CRMScript, but can't seem to make it work in Online.

I suspect the API for CRMScript is not up to date?
Has anybody made it work or have any examples?

Here is my script:

#setLanguageLevel 3;
String secret = "";
DateTime dt = getCurrentDateTime();
String[4] events;
events[0] = "contact.created";
events[1] = "contact.changed";
events[2] = "person.created";
events[3] = "person.changed";

NSWebhookResult result;
NSWebhookAgent agent;
NSWebhook wh = agent.CreateDefaultWebhook();
wh.SetName("Tress_test");
wh.SetTargetUrl("http://ss-sistest03.cloudapp.net/sicagent/sicagentevent.asmx/Event?integrationId=123PS123&type=Pers&id=123");
wh.SetSecret(secret);
wh.SetUpdated(dt);
wh.SetEvents(events);
agent.SaveWebhook(wh);
result = agent.TestWebhook(wh);

if(result.GetSuccess())
  log("Webhook succeeded with message: " + result.GetMessage());
else
  log("Webook failed!");

log(wh.GetSecret());

When executing the script, it fails when initializing WebHook objects wit the following error message:

"EjScript::RunTimeException: An exception occurred in script: "Testwebhooks" at: line 10, char 1 Original exception: EjScript::ParserException: EjScript::Value::Value: cannot create variable of this type: NSWebhookResult"

Thanks in advance.

/Kim

ADMIN EDIT: Moved to the Correct Forum

RE: Creating WebHooks in CRMScript?

The WebHook agent is currently not available in CRMScript, that why it is not working.

When Online is ready for using WebHooks, we will open for it in CRMScript as well.

By: Stian Andre Olsen 19 Jun 2018

RE: Creating WebHooks in CRMScript?

Any thoughts on when the WebhookAgent will be available in CRMScript for OnSite?

By: Frode Lillerud 24 Nov 2018

RE: Creating WebHooks in CRMScript?

Note, seems like the NSWebHookAgent works in Online now.

By: Frode Lillerud 5 Apr 2019

RE: Creating WebHooks in CRMScript?

Seems like webHooks are availible in Online and CRMScript now. 

Is there any tutorial for how to make it work ?

//Ummair

By: Ummair Tahir 9 Jul 2019

RE: Creating WebHooks in CRMScript?

Hi,

Here is an article to get you started!

SuperOffice Webhooks

Best regards!

By: Tony Yates 9 Jul 2019

RE: Creating WebHooks in CRMScript?

Hey Tony !

I've seen this article. It doesent say much about how to do it in CRMScript. 

I have also seen some other documentation. 

Question: 

How do I make the WebHook to fire in Online

Do I make a normal CRMScript with my webhook code and it will just trigger or do I need to do some extra configuration ?

 

/Ummair

By: Ummair Tahir 10 Jul 2019

RE: Creating WebHooks in CRMScript?

It is a protected agent in our Online environment, so you will need to ask us for access to it on your app. Include the app id when you send us an email (appdev(at)superoffice.com) so we know which one to enable it on. Also note - it will require re-approval by customer admin.

By: Margrethe Romnes 10 Jul 2019

RE: Creating WebHooks in CRMScript?

Is your question how to set up a web hook using CRMScript? In that case, here is an example (assuming you have access to the Webhook agent). The example uses the online service "beeceptor" as an endpoint, so be sure to register an endpoint (super simple) and change in the script below:

#setLanguageLevel 3;

NSWebhookAgent wh;

NSWebhook hook;

hook.SetName("Testhook");
String[] events;
events.pushBack("person.created");
hook.SetEvents(events);
hook.SetTargetUrl("https://cshook.free.beeceptor.com");
hook.SetState(1); // Active
hook.SetType("webhook");

wh.SaveWebhook(hook);

By: Stian Andre Olsen 10 Jul 2019

RE: Creating WebHooks in CRMScript?

@Margrethe: Thank you. I will write an email to appdev. What AppId do i need to provide ? Do you mean the custxxxx ? Because in our case we will just exchange data between tables in one and the same SuperOffice env.

@Stian: The endpoint here should be my CRMScript with the logic I want to happen on the triggered event ?

By: Ummair Tahir 10 Jul 2019

RE: Creating WebHooks in CRMScript?

Hi Ummair,

If using CRMScript to create a webhook in an online tenant, there is no need to create an online app. Creating a webhook in CRMScript looks nearly identical to doing it with NetServer web services, detailed in the referenced article. The only difference of course is the addition of NS to the class names and the property setters.

Here is an example how to add a webhook via CRMScript:

#setLanguageLevel 3;

String secret = "";
DateTime dt = getCurrentDateTime();
String[4] events;
events[0] = "contact.created";
events[1] = "contact.changed";
events[2] = "person.created";
events[3] = "person.changed";


NSWebhookAgent agent;
NSWebhook wh = agent.CreateDefaultWebhook();
wh.SetName("Ummair Webhook");
wh.SetTargetUrl("https://sod.superoffice.com/Cust?????/CS/scripts/customer.fcgi?action=safeParse&includeId=YOUR_ID&key=YOUR_KEY");
wh.SetSecret(secret);
wh.SetUpdated(dt);
wh.SetEvents(events);

NSWebhook wh2 = agent.SaveWebhook(wh);

// test the webhook...
NSWebhookResult result;
result = agent.TestWebhook(wh2);
 
if(result.GetSuccess())
  log("Webhook succeeded with message: " + result.GetMessage());
else
  log("Webook failed!");

Then you must have a CRMScript receiver to process the raised webhook and process accordingly...

This is the contents CRMScript (with the includeId=YOUR_ID and key=YOUR_KEY) used in the webhook creation script above.

%EJSCRIPT_START%
<%
#setLanguageLevel 3;
/* 
 * Webservice to listen for webhooks from SuperOffice.
 */
struct Event {
  String EventId;
  String Timestamp;
  String Event;
  String PrimaryKey;
  String Entity;
  String ContextIdentifier;
  String ChangedByAssociateId;
  String WebhookName;
  String [] Changes;
  
  String toJson() {
    JSONBuilder jb;
		this.toJson(jb);
		return jb.getString();
  }
  
  Void fromJson(String json) {
    this.fromXMLNode(parseJSON(json));
  }
};

//Load the incoming JSON data into a struct.
String data = getCgiContent();

Event event;
event.fromJson(data);

// Do whatever else you want with the event here.

print(event.PrimaryKey);

%>
%EJSCRIPT_END%

...

WARNING: DO NOT USE CRMScript Triggers and Webhooks to process the same events!

The Webhooks article covers the rest of the details.

 

 

By: Tony Yates 10 Jul 2019