Using API to create a new record with a relation field

I'm currently developing a webform that saves the forms' info into Sugar, using the Sugar API. 

Everything is working fine except for one type of field in Sugar which I am having difficulties trying to save to. 

That field type is relation field.

While the other field is simple and straightforward, I can't find any instruction or examples on how to save data to this field type.

  • Hi Lifu Sun,

    Does this mean that you have a 1:1 relationship from Module_A to Module_B, similar to the 1:1 Opportunities->Accounts stock module?

    Using the Opportunities:Accounts 1:1 stock relationship for reference, we can do the following:

    1. Create an Oauth token. (We assume you are doing this already.)
    2. Prepare a POST request to https://InstanceURL/rest/v11/ModuleName with the following JSON:

      {
          "name": "Opportunity Name",
          "account_name": "Account Name",
          "account_id": "Account Record ID"
      }

    3. Send the request.

    You can find more information on creating records using the REST API in our documentation. You can also find more examples on using the API at https://InstanceURL/rest/v11/help.

    Best wishes,

    Nathaniel

  • Hi Nathaniel, 

    First, I think I maybe using the term, relation field incorrectly. The field is a autocorrect field that references records from their respective modules.

    I have 3 modules in play:

    Module_A, Module_B, Module_C. 

    The module that I'm trying to create a record for is Module_A.

    Module_B and Module_C both have a One to Many relationship with Module A.  

    In Module_A, there are 2 relation fields (and other fields). One for Module_B and the other for Module_C. 

    What I'm trying to achieve through a POST request is to create a Module_A record and with both its' relation fields having value, and correctly linked to respective records. 

    Currently I am unable to achieve this, and can't find any documentation nor Google anything relevant. 

    Thanks

  • Hi Lifu Sun,

    If I understand correctly, records are created for Module_A. You then have a relationship from Module_A->Module_B and Module_A->Module_C. You then have two scenarios:

    Scenario 1: Records for Module_B and Module_C already exist. When creating the record for Module_A, you only need to define the relationships.

    Possible Solution: You can use the link API for this. Documentation on that is located here.

    Scenario 2: Records for Module_B and Module_C do not exist yet. When creating the record for Module_A, you must also create Module_B and Module_C records that are related to Module_A.

    Possible Solution: Please see our documentation here for details on how to create related records.

    Hope this helps.

    Nathaniel

  • Hi Nathaniel, 

    Scenario 1 is the correct one for my case. 

    And yes, the API does indeed work which is great. 

    I misinterpreted how Sugar handles relation, therefore didn't fully understand how those API functioned in relation to the module fields.

    Thanks a lot