+ Start a Discussion
Mark Maslow 4Mark Maslow 4 

VF Page can't find sforce.one.publish method

When attempting to execute this in a VF page:

sforce.one.publish(locationChannel, payload);

I get an error sforce is not defined.

I tried adding this, which got me sforce, but not sforce.one.

<apex:includeScript value="/soap/ajax/48.0/connection.js"/>
<apex:includeScript value="/soap/ajax/48.0/apex.js"/>

Does anyone know how to get the sforce.one.publish method to work in a VF page?
ANUTEJANUTEJ (Salesforce Developers) 
Hi Mark,

I found the below documentation where in they mentioned on the implementation of the method.

>>https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/message_channel_publish.htm

Is this the similar way the method is implemented?

Regards,
Anutej
VinayVinay (Salesforce Developers) 
Hi Mark,

To publish on a Message Channel from a Visualforce page, include the $MessageChannel global variable in your page's JavaScript code and write a method that calls sforce.one.publish().

Below is the sample code.
 
<apex:page >
    <script>
    // Load the MessageChannel token in a variable
    var SAMPLEMC = "{!$MessageChannel.SampleMessageChannel__c}";
    function handleClick() {
        const payload = {
            recordId: "some string",
            recordData: {value: "some value"}
        }
        sforce.one.publish(SAMPLEMC, payload);
      }
    </script>
    <div>
    <p>Publish SampleMessageChannel</p>
    <button onclick="handleClick()">Publish</button>
    </div>
</apex:page>

Review below links which gives you more information.

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/message_channel_publish.htm
https://amitsalesforce.blogspot.com/2019/10/lightning-message-service-lms.html
https://www.jitendrazaa.com/blog/salesforce/data-exchange-between-aura-lightning-web-components-lwc-and-visualforce/

Hope above information was helpful.

Please mark as Best Answer so that it can help others in the future.

Thanks,
Vinay Kumar
Mark Maslow 4Mark Maslow 4
Yes, that's exactly the code that I tried. But when I step through the code in the debugger, I see an error that sforce is not defined. Presumably, the code for method sforce.one.publish is expected to be declared in some script(s) somewhere. Where? 
VinayVinay (Salesforce Developers) 
Hi Mark,

Did you import both the methods to interact with LMS like below format.

import { publish,subscribe,unsubscribe,createMessageContext,releaseMessageContext } from 'lightning/messageService';
import SAMPLEMC from "@salesforce/messageChannel/MyMessageChannel__c";

Also review working example and try to implement same and that should work.

Thanks,
Vinay Kumar
Mark Maslow 4Mark Maslow 4
The problem is not with a Lightning Web Component, but with a VisualForce page. I am attempting to publish a message on a VF page that will be received by a LWC. There are samples of this shown, but none of the sample VF pages show any special setup - they just call the sforce.one.publish method. I can't figure out how to get the VF page to recognize that method. I have the same problem when trying to deploy the sample code.
Mark Maslow 4Mark Maslow 4
Below is the sample code that would seem to do what I want, but it does not work. When it gets to "sforce.one.publish", it fails with an exception:
Uncaught ReferenceError: sforce is not defined

Sample code at:
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/message_channel_publish.htm

<apex:page >
<script> // Load the MessageChannel token in a variable
var SAMPLEMC = "{!$MessageChannel.SampleMessageChannel__c}";
function handleClick() {
    const payload = { recordId: "some string", recordData: {value: "some value"}
}
sforce.one.publish(SAMPLEMC, payload); }
</script>
<div>
<p>Publish SampleMessageChannel</p>
<button onclick="handleClick()">Publish</button>
</div>
</apex:page>
Mark MaslowMark Maslow
The problem is that I was attempting to use Lightning MessageChannel in a community, which is not supported. It is supported in Lightning Experience, but not in Lightning Communities.

Fortunately, I found a workaround here:

https://developer.salesforce.com/blogs/developer-relations/2017/01/lightning-visualforce-communication.html
 
Team Codekiat.comTeam Codekiat.com
Just in case if anyone looking for more details on the lightning message channel and lightning message service 

https://www.codekiat.com/2020/08/Implement-lightning-message-service-lms-salesforce.html