Jump to content

Page scripting queries


 Share

Recommended Posts

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

 

Link to comment
Share on other sites

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.

image.png.ec7a5f86565de3dc6b227dd322976502.png

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • Members

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?

Link to comment
Share on other sites

 Share

×
×
  • Create New...