POST 到 Salesforce API 使用 Google 标签管理器和 JSforce 不起作用?
POST to Salesforce API using Google Tag Manager and JSforce not working?
我想使用 Google 跟踪代码管理器将我们网站上某些事件(用户注册、转化等)的数据发送到我们的 Salesforce 组织。经过一些研究,我意识到 JSforce would be the easiest way to achieve this. I created a new connected app in Salesforce, tried out the Salesforce API using Postman and successfully managed to create a new user account via the API. Then I moved on to try and achieve the same thing in Google Tag Manager. I read JSforce's docs 并尝试实现所有内容。但是,经过数小时的故障排除和 Google 搜索,我似乎无法让它工作。
这是我当前的代码,它位于 Google 标签管理器中的 'tag' 中,在所有页面上触发(仅用于测试):
https://jsforce.github.io/start/#web-browser
<script src="//cdnjs.cloudflare.com/ajax/libs/jsforce/1.9.1/jsforce.min.js"></script>
<script>
jsforce.browser.init({
clientId: '<MYCLIENTID>',
redirectUri: 'https://cuttersclub.com'
});
https://jsforce.github.io/document/#access-token
var jsforce = require('jsforce');
var conn = new jsforce.Connection({
instanceUrl : 'https://um5.salesforce.com',
accessToken : '<MYACCESSTOKEN>',
});
https://jsforce.github.io/document/#create
conn.sobject("Account").create({ Name : 'My Account #1' }, function(err, ret) {
if (err || !ret.success) { return console.error(err, ret); }
console.log("Created record id : " + ret.id);
});
</script>
我在浏览器控制台中收到此错误:
Uncaught ReferenceError: require is not defined
编辑:删除 var jsforce = require('jsforce');
解决了这个问题,并且正在 Salesforce 中创建帐户。但是,现在我在浏览器控制台中收到以下错误:
Access to XMLHttpRequest at '<URL>' from origin '<CALLBACKURL>' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
正如 JSforce 文档中提到的,我认为这可能与代理服务器有关:https://github.com/jsforce/jsforce-ajax-proxy
我不太了解 salesforce,但是 "require" 是 node.js 的东西,不是在浏览器中实现的功能。
如果我对文档的理解正确,那么对于浏览器项目来说,通过脚本标签调用 jsforce 脚本应该就足够了。在那之后你不需要任何方法来 "require" 文件,因为 jsforce 脚本已经包含你需要的一切。所以如果你只是删除有问题的行(即所有对 "require('jsforce');" 的引用),你应该没问题。
我想使用 Google 跟踪代码管理器将我们网站上某些事件(用户注册、转化等)的数据发送到我们的 Salesforce 组织。经过一些研究,我意识到 JSforce would be the easiest way to achieve this. I created a new connected app in Salesforce, tried out the Salesforce API using Postman and successfully managed to create a new user account via the API. Then I moved on to try and achieve the same thing in Google Tag Manager. I read JSforce's docs 并尝试实现所有内容。但是,经过数小时的故障排除和 Google 搜索,我似乎无法让它工作。
这是我当前的代码,它位于 Google 标签管理器中的 'tag' 中,在所有页面上触发(仅用于测试):
https://jsforce.github.io/start/#web-browser
<script src="//cdnjs.cloudflare.com/ajax/libs/jsforce/1.9.1/jsforce.min.js"></script>
<script>
jsforce.browser.init({
clientId: '<MYCLIENTID>',
redirectUri: 'https://cuttersclub.com'
});
https://jsforce.github.io/document/#access-token
var jsforce = require('jsforce');
var conn = new jsforce.Connection({
instanceUrl : 'https://um5.salesforce.com',
accessToken : '<MYACCESSTOKEN>',
});
https://jsforce.github.io/document/#create
conn.sobject("Account").create({ Name : 'My Account #1' }, function(err, ret) {
if (err || !ret.success) { return console.error(err, ret); }
console.log("Created record id : " + ret.id);
});
</script>
我在浏览器控制台中收到此错误:
Uncaught ReferenceError: require is not defined
编辑:删除 var jsforce = require('jsforce');
解决了这个问题,并且正在 Salesforce 中创建帐户。但是,现在我在浏览器控制台中收到以下错误:
Access to XMLHttpRequest at '<URL>' from origin '<CALLBACKURL>' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
正如 JSforce 文档中提到的,我认为这可能与代理服务器有关:https://github.com/jsforce/jsforce-ajax-proxy
我不太了解 salesforce,但是 "require" 是 node.js 的东西,不是在浏览器中实现的功能。
如果我对文档的理解正确,那么对于浏览器项目来说,通过脚本标签调用 jsforce 脚本应该就足够了。在那之后你不需要任何方法来 "require" 文件,因为 jsforce 脚本已经包含你需要的一切。所以如果你只是删除有问题的行(即所有对 "require('jsforce');" 的引用),你应该没问题。