Lee Savidge Posted August 22 Posted August 22 I'm working with PO Transaction definitions. At a certain point in the workflow, the customer wil only get the ability to save a stage as a draft document. I've done this by removing the Post button according to some conditions I need. That's fine and working. Now, when the customer opens to TD for Viewing only, a custom button appears which I need to get that button to call an external endpoint to do some processing. I can do an AJAX call to an endpoint, but I need to pass some data from Intacct along with the call. See the code below which is my page script: <script> jq(document).ready(function() { var html = '<button id="checkOverspend" name="checkOverspend" class="btn btn-primary">Check Overspend</button>'; if (!jq('#_obj__OVERSPEND_PASSED').is(":checked")) { jq('.btn-toolbar').prepend(html); } jq("#checkOverspend").click(function () { alert('Checking overspend'); checkOverspend(); location.reload(true); }); }); function checkOverspend() { console.log('Checking overspend (method)'); var settings = { "url": "*** my endpoint location redacted ***", "method": "POST", "timeout": 0, "headers": { "Content-Type": "application/x-www-form-urlencoded" }, "data": { "sessionid": (new URL(location.href)).searchParams.get('.sess'), "endpoint": "https://api.intacct.com/ia/xml/xmlgw.phtml", "useremail": "*** user email address of currently logged on user ***", "licencekey": "*** some data from a custom field on the COMPANY object ***", "entityid": "*** entity id ***", "docid": "*** current doc id ***" } }; jq.ajax(settings).done(function (response) { console.log(response); }); } //# sourceURL=addOverspendButton.js </script> The script isn't finished yet, but my issue is passing the data in the request header. How can I get the DOCID from the currentrly open record? I can get the session ID from the URL but if there is a better way, then I'm open to ideas. I'm happy to hardcode the endpoint URL. The licencekey is a string value on the COMPANY object in a custom field. How can I get that? In smart events and triggers, there are universal objects which can be used as injection parameters which are CURRENTUSER, COMPANY, and USERPROFILE. How do I get the entity ID of the current entity I'm connected to? If I can get them, I can make the post out to do some processing which ultimately updates the current transaction definition document with a value in a custom field. That bit works. I just can't make the call. Any help appreciated. Lee
Varian Lee Posted August 23 Posted August 23 The merge fields available in smart events are also available in page scripts, this includes getting a session ID and Entity IDs. A merge field selector is directly above the section where you would have put your script. Have a look into using the AJAX SDK to get data from the COMPANY object, though this will require the PO users to have view permissions to the COMPANY object: https://developer.intacct.com/platform-services/ajax-sdk-javascript/ Or you could use a smart event to copy the value you need from the COMPANY object and put it in a hidden custom field on the PO and then pick it up in your script using a merge field.
Lee Savidge Posted August 27 Author Posted August 27 Thanks for this. I fugured they must be available. I will have a play with it later. I have looked at the AJAX SDK. I must admit, I'm new to the page scripting. All my work is usually with the XML and REST API doing integrations. I rarely get involved with UI stuff. Nice idea of copying the value to the PO, however, I will use AJAX to pull from the COMPANY object first I think. It just means there is only one copy of the field value in the system. Much appreciated. Once I get this going I will post more of what I did. I think the page srcipting side of things is very thin on the ground for examples. Lee
Members Sterio, Louis Posted September 3 Members Posted September 3 Posting this on behalf of Lee. ----------------------- Ok, I've got the field values in the way I want. When I hit the custom button I get this in the console: VM4162 jquery-3.5.1.min.js:2 Refused to connect to '*******************************************' because it violates the following Content Security Policy directive: "connect-src 'self' *.intacct.comhttps://*.grammarly.comhttps://*.grammarly.iowss://*.grammarly.comhttps://app.pendo.iohttps://data.pendo.iohttps://pendo-static-5671022460534784.storage.googleapis.com". In the controller of my API, I have these set: [HttpPost, HttpOptions] [EnableCors("CorsPolicy")] [Consumes(MediaTypeNames.Application.Json)] file:///C:/Users/Lsterio/AppData/Local/Temp/msohtmlclip1/01/clip_image001.gif When configuring the request pipeline I have this in the startup.cs: app.UseCustomHeaders((opt) => { opt.HeadersToAdd.Add("Access-Control-Allow-Origin", "*.intacct.com"); file:///C:/Users/Lsterio/AppData/Local/Temp/msohtmlclip1/01/clip_image001.gif On other page scripts that call other endpoints on this API, this seems to be ok. However, for this page script it is refusing to work. The script now reads: <script> jq(document).ready(function() { var html = '<button id="checkOverspend" name="checkOverspend" class="btn btn-primary">Check Overspend</button>'; if (!jq('#_obj__OVERSPEND_PASSED').is(":checked")) { jq('.btn-toolbar').prepend(html); } jq("#checkOverspend").click(function () { alert('Checking overspend'); checkOverspend(); location.reload(true); }); }); function checkOverspend() { console.log('Checking overspend (method)'); var settings = { "url": "*** my endpoint redacted ***", "method": "POST", "timeout": 0, "headers": { "Content-Type": "application/json", "Access-Control-Allow-Origin": "*.intacct.com" }, "data": { sessionid: "{!#CURR_SESSION!}", endpoint: "{!API_ENDPOINT!}", useremail: "{!CURRENTUSER.email!}", licencekey: "{!CURRENTCOMPANY.LICENCE_KEY!}", entityid: "{!CURRENTENTITY#id!}", podocrecordno: "{!PODOCUMENT.RECORDNO!}", podocparid: "{!PODOCUMENT.DOCID#text!}" } }; jq.ajax(settings).done(function (response) { console.log(response); }); } //# sourceURL=addOverspendButton.js </script> Any thoughts?
Tshego Posted September 30 Posted September 30 Hi all, I'm also experiencing Refused to connect to '*******************************************' because it violates the following Content Security Policy directive block. Is there a solution to allow connection to external API links through page scripts? Your help will be appreciated. Best regards, Tshego
Solution Josh Kenzer Posted September 30 Solution Posted September 30 Try adding the url to the script Content security policy. I'm not sure if this will work as I think this is intended to allow scripts to run off another server, but it might do the trick. 1
Lee Savidge Posted October 2 Author Posted October 2 Thanks for this. It did work. I set my target URL in the content security policy by pressing the AJAX button. This seems to allow things to go through. The target API that I wrote accepts it as it adheres to the CORS policy. However, all I am trying to do in the API call is to update the record that I had open to make this AJAX call. It passes out the following XML: <?xml version="1.0" encoding="utf-8"?> <request> <control> <senderid>REDACTED</senderid> <password>REDACTED</password> <controlid>80016a47-0000-d200-b63f-84710c7967bb</controlid> <uniqueid>false</uniqueid> <dtdversion>3.0</dtdversion> <includewhitespace>false</includewhitespace> </control> <operation transaction="true"> <authentication> <login> <userid>REDACTED</userid> <companyid>REDACTED</companyid> <password>REDACTED</password> <locationid>REDACTED</locationid> </login> </authentication> <content> <function controlid="80016a47-0000-d200-b63f-84710c7967bb"> <update_potransaction key="Submit-RQN38906"> <customfields> <customfield> <customfieldname>OVERSPEND_PASSED</customfieldname> <customfieldvalue>true</customfieldvalue> </customfield> </customfields> </update_potransaction> </function> </content> </operation> </request> I then get the following response: <?xml version="1.0" encoding="UTF-8"?> <response> <control> <status>success</status> <senderid>REDACTED</senderid> <controlid>80016a47-0000-d200-b63f-84710c7967bb</controlid> <uniqueid>false</uniqueid> <dtdversion>3.0</dtdversion> </control> <operation> <authentication> <status>success</status> <userid>REDACTED</userid> <companyid>REDACTED</companyid> <locationid>REDACTED</locationid> <sessiontimestamp>2024-10-02T15:02:20+00:00</sessiontimestamp> <sessiontimeout>2024-10-02T21:02:20+00:00</sessiontimeout> </authentication> <result> <status>aborted</status> <function>update_potransaction</function> <controlid>80016a47-0000-d200-b63f-84710c7967bb</controlid> <errormessage> <error> <errorno>BL03000215</errorno> <description></description> <description2>Cannot update a document that's 'In Progress' state. [Support ID: _qxd8EB033%7EZv1geP054l2-Q_oWhEWwcgAAABU]</description2> <correction></correction> </error> <error> <errorno>XL03000009</errorno> <description></description> <description2>The entire transaction in this operation has been rolled back due to an error.</description2> <correction></correction> </error> </errormessage> </result> </operation> </response> The curious thing is that the document is not "In progress". It is draft. If I modify the document via the UI and set this field value it works. Just not via the API. So, we're a step closer. On an unrelated note, I also had to modify the AJAX call because it doesn't pick up fields from the universal objects, so <!CURRENTCOMPANY.AT_KEY!}. I also tried <!COMPANY.AT_KEY!} but it didn't pick the string value up from that field from the company object. I can live with that if needed.
Recommended Posts