Presenter not firing events

Hi,

I’m trying to fire events from the Set Public and Set Internal buttons on this list edit widget. However, no joy! I’m using a Presenter on a text field whose code is below.

main.js

return {
get_template_data: function() {
let baseRecordId = presenter.get_base_record_id();
let record = cs.record(baseRecordId);
cs.log('baseRecordId is ’ + baseRecordId)
return {
baseRecordId: baseRecordId,
};
}
};

frontend.js

export default {
methods: {
firePublicEvent(e) {
e.preventDefault();
cs.log(‘Firing Public Event Action’);
cs.event_action(
cs.ref(‘event_public_event_action’),
baseRecordId, // This must be available in the global template data
{ source: ‘public_button’ }
);
},
fireInternalEvent(e) {
e.preventDefault();
cs.log(‘Firing Internal Event Action’);
cs.event_action(
cs.ref(‘event_internal_event_action’),
baseRecordId,
{ source: ‘internal_button’ }
);
}
}
};

main.htm

<button @click=“firePublicEvent”>Set Public
<button @click=“fireInternalEvent”>Set Internal

Any help much appreciated, as always.

Mark

cs.event_action is only usable in your backend code, e.g. main.js.

You need to make a call from your frontend code using this.trigger_action() to a new method in your backend code named action_{something}.

Not easy to paste a full example here as there are three parts, but have a look at the help for main.js → action_xxxxx() in the right hand help bar for a full example.

So your HTML code will call the frontend function (e.g. fireInternalEvent), which you already have. But that then needs to call a backend function (e.g. action_fireInternalEvent), which can be passed any relevant data (probably just record ID), and it can then fire the actual event on the record.

Hi, Bob,

So I have got thus far, but it’s still not firing the event. If I set up the same event reference with cs.fire_event in an event action it fires and is logged by detective.

I am just using 1 button to try and get the event to fire:

main.htm:

<button @click=“firePublicEvent”>Set Public

frontend.js:

export default {
props: [‘baseRecordId’],
methods: {
firePublicEvent(e) {
e.preventDefault();
this.trigger_action(‘firePublicEvent’, { baseRecordId: this.baseRecordId }, function(e){
if (e && e.result === “success” && e.response) {
console.log(e.response);
}
});
}
},

};

main.js:

return {
action_firePublicEvent: function(e) {
cs.log('Firing backend event for record: ’ + e.baseRecordId);
cs.fire_event(cs.ref(‘event_public_event’), e.baseRecordId);
return { response: ‘Event triggered successfully’, result: ‘success’ };
}
,
get_template_data: function() {
let baseRecordId = presenter.get_base_record_id();

  return {
    baseRecordId: baseRecordId,
  
  };

},

};

Thanks, Mark

Are you seeing this log in Detective at all?

If not try also putting a console.log() above your this.trigger_action line, to try and narrow down if the issue is the HTML firing the frontend function, or the frontend function trying to call the backend function.

You should also be able to see the request triggered by your this.trigger_action() in your Browser’s Network debugging panel, to double check something is happening.

Yes that is visible in detective:
16:17:12.3983
Firing backend event for record: 20857

Yes, things are happening:

If I put console.log() above the this.trigger_action line, nothing is logged.

Ok so that is getting through, just spotted that you are using cs.fire_event rather than record.fire_event which I think is what you need.

cs.fire_event only works in certain contexts where a record ID is already known, such an an Event Action.

In this case because you’re firing on an arbitrary record you first need to create a record object for it. E.g:

let record = cs.record(e.baseRecordId);
record.fire_event(cs.ref('event_public_event'));

Cheers Bob that is working nicely now.