TeamDesk Knowledge Base & Support

   Home      FAQ      Forum      Idea Exchange      Ask a Question      My Stuff      Help   
  
REST API - Connect 2 Databases
Details:

Database 1
Table 1
Table 2
Database 2
Table 3

Goal:

When records are created in Table 2 in Database 1, Table 3 in Database 2 is updated with the new records as well.

Action Taken:

Created a custom button in Table 1 that creates records in Table 2.
Set up a trigger when records are added into Table 2.
I want to set up a Call URL action within the Table 2 trigger.
*** I’m not sure about how to proceed in the Call URL. ***
The image is for the Call URL.

Generated a REST API token in Database 2
Got the URL for Table 3 (Describe)
https://teamdesk/secure/api/v2/86702/Test%20Table%203/describe.json
*** Not sure if this is the correct URL to use in the trigger ***
Would I use the Upsert method instead of Describe?


ID
2007
Category
Integration/API
Author

Aniarka Diaz
Date Created
8/30/2023 4:24:20 PM
Date Updated
9/1/2023 9:57:03 AM
Comments
calvin peters 8/31/2023 8:01:33 AM
So 'DESCRIBE' is simply the initial required method to load your db to the playground area so you can test and experiment with it.

UPSERT is handy for instances where you may be either CREATING or UPDATING a record in a given db but can become confusing later if your system begins to get busy or bloated with numerous triggers and actions.

The example you provided would become something more like one of the following:
https://teamdesk/secure/api/v2/86702/Test%20Table%203/upsert.json
https://teamdesk/secure/api/v2/86702/Test%20Table%203/create.json
https://teamdesk/secure/api/v2/86702/Test%20Table%203/update.json

The better defined methods are CREATE and UPDATE methods.
These are POST moethods which do exactly what the method suggests.

So if you are only ever creating records with a trigger from one tbl to another db tabl then use CREATE method.
If it is possible you may require either option you can use UPSERT or the better solution is to use trigger filters and have separate CREATE and UPDATE methods attached to the appropriate trigger to accomplish your goal.

Your CallURL action will require 4 things for 'METHOD' = POST:

1) a generated access key ' REST API Authorization Tokens ' for the target db

2) an appropriate URL endpoint for the db2 and tbl3 in under

3)for POST you require HEADERS containing:

a) Authorization: Bearer *************token************** << (this get generated in the playground of your target db and sets the access limits based on the user it simulates)

b) Content-Type: application/json (to be included in the HEADER section under the AUTH token)

4) PAYLOAD formatted as json that looks a bit like this:
[
{
"column1.tbl3":<%=[value1.tbl2]%>,
"column2.tbl3":<%=[value2.tbl2]%>,
"column3.tbl3":<%=[value3.tbl2]%>
}
]

If your target db is other than a TeamDesk db it may require a different payload type but that is all based on the API for the specific db...
ie: Quickbooks, DropBox or some other API that may use FORM or XML or TEXT payload setup

The <%=[...]%> code brackets around the column

workflows are switched on by default for CALLURL actions in TD so if you do not want triggers to be tripped on a record in the target db then at the end of your URL endpoint
?...upsert.json?&workflow=0 so in your example as a create it becomes:

https://teamdesk/secure/api/v2/86702/Test%20Table%203/create.json?&workflow=0 which turns workflow triggers OFF and prevents potential runaway server actions if improperly managed. Cascading workflow triggers can get tricky but can also be very useful if properly managed.

Hope that gives you a running start to this CallURL stuff.
I found it was a bit daunting when I first dove in.

Aniarka Diaz 8/31/2023 8:23:44 AM
Thank you so much. I'm super new to all of this and trying to learn, I appreciate the explanation.
calvin peters 8/31/2023 8:41:06 AM
NP

There's lots of people in here who have been at this for years and some (like Cooper , Bret, Pierre, to note a few of the many worldwide) often show up to assist so don't be shy to ask.

The 'gurus' who developed this platform are an invaluable source of help when you get lost too, and Slava, Kirill and Co are quick to go the extra mile to help you succeed.

Good luck
Aniarka Diaz 8/31/2023 1:54:58 PM
So far I managed to work with the code and manually write in some data entries to see if it would appear and it works a new record was created in Table 3.

Now I have gone into the Call URL for Table 2 and entered the following code in the Body. But I get an error.

[
{
"Market ID":"<%=[Market ID]%>",
"Main Market Name":"<%=[Market Name]%>",
"Date":"<%=[Date]%>",
"Open":"<%=[Open]%>",
"High":"<%=[High]%>",
"Low":"<%=[Low]%>",
"Close":"<%=[Close]%>",
"Adj Close":"<%=[Adj Close]%>",
"Volume":"<%=[Volume]%>"
},
]

ERROR:
3: "Market ID":"<%=[Market ID]%>",
^
Format blocks are not allowed inside quotes

Aniarka Diaz 8/31/2023 2:04:31 PM
It shifted the arrow but it was pointing at the <.

I rewrote the values, and only kept the ""

Method: POST
Url: https://apps.mspllr.net/secure/api/v2/86702/Test%20Table%203/create.json
Headers: None
Body
[
{
"Market ID":"[Market ID]",
"Main Market Name":"[Market Name]",
"Date":"[Date]",
"Open":"[Open]",
"High":"[High]",
"Low":"[Low]",
"Close":"[Close]",
"Adj Close":"[Adj Close]",
"Volume":"[Volume]"
},
]

But wouldn't it just return the phrase "[column]" back?

What would be a better approach to ensure that I don't need to manually enter the values?
Slava Shinderov  Staff  8/31/2023 2:15:06 PM
Please try following:

[
{
"Market ID":<%=[Market ID]%>,
"Main Market Name":<%=[Market Name]%>,
"Date":<%=[Date]%>,
"Open":<%=[Open]%>,
"High":<%=[High]%>,
"Low":<%=[Low]%>,
"Close":<%=[Close]%>,
"Adj Close":<%=[Adj Close]%>,
"Volume":<%=[Volume]%>
},
]

calvin peters 8/31/2023 2:28:30 PM
Told you they were quick...

:)
Aniarka Diaz 8/31/2023 2:32:56 PM
That solved the error thank you. I tested it out, but no records were created in Table 3 after I added them in Table 2.

This is how I have my Call URL set up.

General
Name: New Call URL
Notes
Execute Triggers: No

Authorization
Authorization: None

Request
Method: POST
Url: https://apps.mspllr.net/secure/api/v2/86702/Test%20Table%203/create.json
Headers: None
Body
[
{
"Market ID":<%=[Market ID]%>,
"Main Market Name":<%=[Market Name]%>,
"Date":<%=[Date]%>,
"Open":<%=[Open]%>,
"High":<%=[High]%>,
"Low":<%=[Low]%>,
"Close":<%=[Close]%>,
"Adj Close":<%=[Adj Close]%>,
"Volume":<%=[Volume]%>
},
]

Response
Format: Auto-Detect
On Error: Stop Execution
Error Message: "No Record Created"



Aniarka Diaz 8/31/2023 2:33:27 PM
The body is JSON.
Aniarka Diaz 8/31/2023 2:35:45 PM
You were not kidding Calvin lol
calvin peters 8/31/2023 2:43:13 PM
There needs to be a set of HEADERS for that POST action

Inside the HEADERS you need your Authorization: Bearer *************token************** that you generated in your target db ('token' is that large number generated for authorization)
and you need to set this line below the token bearer: Content-Type: application/json
So it will look something like

Authorization: Bearer 121212121212121212
Content-Type: application/json

in the HEADER section when you're done
Aniarka Diaz 8/31/2023 2:43:41 PM
I'm trying to figure out where the error is. :(
Aniarka Diaz 8/31/2023 2:44:15 PM
Got you I forgot about header thank you.
calvin peters 8/31/2023 2:51:52 PM
If you look to the bottom of your WORKFLOW ACTION setup form you will find
ASSIGNMENTS
ITERATORS
USAGE
USAGE LOG

That USAGE LOG at the bottom will eventually be your debugging best buddy ... (other than the PROFILER ; that's a realtime execution stack trace that you can engage when needed to view how your spaghetti unravels when you trigger everything...great tool but different book for now...)
The usage log will allow you to view the actual raw server response that your call generated with the server code
CODES below
200
201
3xx
4xx
5xx
along with the error message it returned whixh will help you trace the problem.

Your last call when you view it would likely say something like a 4xx code with a text message that access denied or unauthorized or some such similar return.
They are also color coded in RED or GREEN.

calvin peters 8/31/2023 2:54:18 PM
You can also find a very useful REST API manual in the link found on this page.

The terminology will likely be like reading sanskrit to begin with but it eventually begins to gel when you're experimenting at the same time.
https://teamdesk.crmdesk.com/answer.aspx?aid=21930
Aniarka Diaz 8/31/2023 3:55:53 PM
Its not running.

I wanted to make sure the Header was correct before troubleshooting anything else.

Authorization: Bearer B2804E*******84217D87
Content-Type: application/json

I copied the exact token that was generated.



Aniarka Diaz 8/31/2023 4:02:59 PM
This is a breakdown of what actions I have set up currently. Any advice would be appreciated.

Database 1
Table 1
Custom Button
Create Record
Navigate (to Table 2)
Table 2
Record Trigger (Added)
Call URL (POST)

Database 2
Table 3

calvin peters 8/31/2023 4:12:52 PM
what does your USAGE log say at the bottom of the CallURL setup page?

Open SETUP and navigate to your CallURL form.

Scroll to the bottom and at under USAGE your should see all of the attempts you made to create your new records. The STATUS column should have a number in it...2xx; 3xx; 4xx or something like that

Click VIEW 'button' (not VIEW RECORD) to view the server response from bd2

1st questions would be
1) the URL address is aimed at db2 ?
2) the token was generated in db 2?
3) the CallURL is being triggered from db 1 with those 2 parameters?
They all sound obvious however...
Aniarka Diaz 8/31/2023 4:16:56 PM
That's the issue there are no logs. Its like the record trigger is ignored.
calvin peters 8/31/2023 4:35:40 PM
Well that is a very likely possibility.
Perhaps your trigger is not correctly configured yet (assuming the action is attached to a trigger...LOL...I've done worse.)

The trigger should likely have no filter on it to start with so it will run every time. Then add filter parameters one at a time until it quits triggering. That will tell you where the issue is.

The Trigger needs to be tagged as ACTIVE in the top section
Under the MATCHING section set it to
"Filter" = "All records"
"when record is" = "Added"
and "when value changes in" = "Any column"
This should make it fire .

Then ensure that under ACTIONS you have your CallURL selected.

Then do whatever you do to trigger it and see what happens.

If all of that is set up and it's still not working...see below...

If you log a ticket through the SUPPORT link in the top right using ASK a QUESTION the real superstars will fully engage and walk you through it like a pro. They are the best...no doubt.

In your 'question' include a copy of the URL to your CallURL action and to the TRIGGER it is associated to along with your explanation of the goal (although Slava is likely watching this all unfold since he already chimed in here for you) . They will give you ALL the guidance you could ever need to succeed.
With those links they will be able to go directly into your action and trigger to see how it's all configured then when you say you have it figured out, the whole link is dropped on their end and they nolonger have access.

I'd continue further with you but the 'eyes on' and pro developer is likely your quickest route...



Aniarka Diaz 9/1/2023 8:32:42 AM
Thank you I'll ask them. I appreciate the advice.
Aniarka Diaz 9/1/2023 9:35:23 AM
I made a small test sample with a customer button with only a Call URL. When I press the button to test to see if the URL works I get this error.

Error in 'New Call URL' action: The remote server returned an error: (403) Forbidden.
Aniarka Diaz 9/1/2023 9:36:00 AM
Has anyone has this error? It is because of the token?
Aniarka Diaz 9/1/2023 9:53:59 AM
It was an issue on my user end and the way my account was set up. We fixed it thank you.
calvin peters 9/1/2023 9:56:16 AM
yup...

So that indicates that your auth token does not have the proper authority to access this db2

So to fix that you need to change who/what role the current token is emulating to a role/user profile that has proper access in db2

Do you have roles and users set up in db2?

If not then that is the first task.
Then ensure that your user has a ROLE designated with access to the table you are adding records to.
That is done under the Tbl3 in db2 under SETUP
on the lower right of the Tbl2 tab you will find a heading called TABLE. Under that heading select " Manage access for table records "

When the form opens, find the user you selected for the token and ensure that the column " ALLOW VIEW " is set properly...start with "ALL RECORDS" . You can adjust later if needed. Then go across that form to where it lists "ALLOW ADD" and ensure that is set to YES. Once those permissions are set click save and try again.
calvin peters 9/1/2023 9:57:03 AM
super

Good luck going forward
Feedback
Back to Search Results