Forum Discussion

SamRettig's avatar
SamRettig
Qrew Member
3 years ago

API button to add new parent record

Hello,  I'm wanting to add an API button in a child table that would add a parent record and also add the parent record id to the related field in the child record.
Basically the same thing that adding a record from a record picker does but pre-populate fields in the parent record. Is this possible?
Thanks in advance for any advice!

------------------------------
Sam Rettig
------------------------------
  • Before API Gods reply , I think your button will be combination of two 

    1. API_Add record to add a parent record
    2. Get the record ID of the newly added parent record
    3. Redirect back to to current child record
    4. API_edit record to edit the child record and add related parent record ID 

    I don't know how to do #2

    Pipeline can easily resolve this ,but it would be want to see how API Gods resolve this



    ------------------------------
    Prashant Maheshwari
    ------------------------------
    • PrashantMaheshw's avatar
      PrashantMaheshw
      Qrew Captain
      APIGods still are needed to resolve this, but some progress 


      I correct myself , I don't know how to achieve 4 as I've read at multiple place %%rid%% doesn't like to be URLEncoded in anyways, 

      • Below code creates a child
      • Comes back to child , edits it  . Please see notes with start with // 
      • Returns back to child record
      • AppToken were disabled during my testing, you need to enable these



      var text rd="%%rid%%";

      // Assuming you start from child record, This URL is to come back to the child and edit edit to enter parent id

      var text urltwo=URLRoot()&"db/" &Dbid()& "?a=API_EditRecord"
      //& "&_fid_8="&$rd --> This keeps failing as we cannot Record ID of parent , but works for any other field
      & "&rid="&[Record ID#]

      // This URL takes you to parent table and creates a new record, notice the dbid is hardcoded . The part which works

      URLRoot()&"db/"&"bsguktwqs"&"?a=API_AddRecord"& "&_fid_6=Gobaby"
      & "&rdr="&
      // Next URL BEGINS to go child and edit
      URLEncode($urltwo)

      // Last URL with two encodes to come back to child ID after editing it back 
      & URLEncode("&rdr="&URLEncode(URLRoot() & "db/"&Dbid()& "?a=doredirect&z=" & Rurl()))

      ------------------------------
      Prashant Maheshwari
      ------------------------------
      • MarkShnier__You's avatar
        MarkShnier__You
        Icon for Qrew Legend rankQrew Legend
        I think there are two methods.  @Prashant Maheshwari 's post reminded me of the %%rid%% technique.

        If the goal is to Create a parent record with no data entry from the user, then you would use a regular API_AddRecord API (post back if you need help with that) and populate a field called  [Record ID# of Originating Child] with the Record ID# of the Child record that you launched from.

        Then create a Pipeline to trigger on Add Record where the [Record ID# of Originating Child] is >0 to edit the Child record for that Record ID# and put the Record ID# of the Trigger Added Parent record into the field for Related Parent.

        There will be a bit of a delay while the Pipeline runs.  If you needed a faster reaction time,  I think that you could use an "old fashioned" webhook or Action to update the child record as they will trigger very fast.

        .... now if your intent is to put up an AddRecord form  and have the user fill in some fields and manually save, then you could have a simple Add Record API and once again populate a field in the Parent Record with the Record ID# of the Originating Child.

        The initial Add Record API would just be this.

        URLRoot() & "db/" & [_DBID_PARENT] & "?act=API_GenAddRecordForm"
        & "&_fid_250=" & [Record ID#]  // populating the field called [Record ID# of the Originating Child]

        But then instead of having the user save normally, you would get the user to click a special formula Rich Text button with this code

        var text RID = "%%rid%%";

        var text EditChildToConnectToThisParent = URLRoot() & "db/" & [_DBID_CHILD]
        & "?act=API_EditRecord&rid=" & [Record ID# of Originating Child]
        & "&_fid_7=" & $RID;

        var text DisplayNewParent = URLRoot() & "db/" & [_DBID_PARENT] & "?a=dr&rid=";

        var text URL =
        $EditChildToConnectToThisParent
        & "&rdr=" & URLEncode($DisplayNewParent) & $RID;

        "<a class='SaveBeforeNavigating' data-replaceRid=true style=\"text-decoration:none; background: #4b7097; border-radius: 5px; color: #ffffff; display: inline-block; padding: 0px 0px 0px 0px; width:100px; text-align: center; text-shadow: none; border: 2px solid #030404; font-size: 12px \"href='"
        & $URL
        & "'>Display Parent with Child Attached</a>"

        //NOTE: The %%RID%% does not seem to like being URLEncoded.


        But a better suggestion is to launch from the Child with this code here.


        URLRoot() & "db/" & [_DBID_PARENT] & "?act=API_GenAddRecordForm"
        & "&_fid_250=" & [Record ID#]
        & "&ifv=1"

        .. what extra suffix will do that will do is take away the save button (along with all the header stuff)  and force the user to save with the provided Rich Text Formula button.

        The Rich Text formula button will land the user on the Newly created parent and the child will be attached in the Report link field with it displays.

        You can, of course play with the parameters of the Rich Text button to control the size of the font and the colour of:
        the button,
        the words,
        and the border. 




        ------------------------------
        Mark Shnier (Your Quickbase Coach)
        mark.shnier@gmail.com
        ------------------------------