Data Providers
...
REST
Create an app using REST APIs
Create customer (POST)
9min
scenario press the add customer button in the customer list to create a customer complete the new customer form and press the create button add new customer how does this work a function to call the rest api's post operation is configured, allowing the record to be created in the backend the function is referenced in the execute entity action on the form the customer is created using the rest data provider with a create method (which creates the record in the local data provider with a temp id, calling the rest create customer function and providing the parameters for the data values to be created in the rest service specifying the id in the ouputtransform in the function enables the local data provider temp id to be automatically updated with the rest id once it is created in the datastore this code sample builds upon the previous list customers (get) docid 1o2k7 ccuvzfbg7x7f4r2 step, to develop a complete and functional solution rest api rest detail url https //\[your rest service]/api/customers url https //\[your rest service]/api/us states (to get a list of states for selection in the form) operation/method post function the rest apis post operator is used in a {{jigx}} function with body parameters to specify the exact columns to be created for the record the inputtransform specifies how the data should be structured or formatted when being sent to the rest service this transformation process ensures that the data adheres to the expected schema or format required by the rest service for processing the request in the outputtransform the id and status are configured to ensure that the properties are automatically returned once the record is created and the local provider's temp id is updated with the actual id and status making the form completion easier the rest api get opertaion is used on the us states table to return the state name, region and abbreviation in the dropdown field rest create customer jigx provider data provider rest method post # creates new record in the backend url https //\[your rest service]/api/customers #use your rest service url uselocalcall true #direct the function call to use local execution between the mobile device and the rest service parameters accesstoken location header required true type string value service oauth #use manage jigx com to define credentials for your solution firstname type string location body required true lastname type string location body required true companyname type string location body required true address type string location body required false city type string location body required false state type string location body required false zip type string location body required false phone1 type string location body required false phone2 type string location body required false email type string location body required false web type string location body required false region type string location body required false customertype type string location body required false jobtitle type string location body required false \#define the customer data that must be created in the record in the rest service inputtransform | { "firstname" firstname, "lastname" lastname, "companyname" companyname, "address" address, "city" city, "state" state, "zip" zip, "phone1" phone1, "phone2" phone2, "email" email, "web" web, "region" region, "customertype" customertype, "jobtitle" jobtitle } \#specifiying an outputtransform for the id ensure that the id and status created by the rest api is automatically synced back to the jigx local datasource replacing the temp id outputtransform | { "id" custid, "status" status } rest get usstates jigx provider data provider rest method get url https //\[your rest service]/api/us states #use your rest service url forrowswithmatchingids true uselocalcall true #direct the function call to use local execution between the mobile device and the rest service parameters parameters accesstoken location header required true type string value service oauth #use manage jigx com to define credentials for your solution \# $ returns all records from the rest service outputtransform | $ action (global) create a load data jigx file under the actions folder this file is configured with an action that syncs the data from the rest service, by calling the function, to the local sqlite table the action file is referenced in the index jigx file to load the data when the app is opened or is in focus on the device load data jigx # the sync entities action is used to get the data from the rest data provider using the function \# the global action can be referenced throughout the solution for effieicency and performance \# the data is synced from the rest data provider to a local data provider on the device action type action sync entities options provider data provider rest entities \ entity us states function rest get usstates datasource add a file under the datasource folder to configure the local data provider with the data to return from the us states table us states jigx #define a global datasource for the local data provider containing the state data synced from the rest service with the global action type datasource sqlite options provider data provider local entities \ entity us states query | select id, '$ state', '$ abbreviation', '$ statecapital', '$ region', '$ flag' from \[us states] order by '$ state' jig (screen) on the list of customers jig configure an go to action that adds the add customer button to the list and links to the create customer {{jig}} in a default jig add a local data provider datasource with a query to get the us states data add a query parameter to set the state of the region field once the state field is selected add a form component to capture the customer details with each field's instanceid containing the same name as the body parameters in the function for the state field configure a dropdrown with an expression to get the list of states for selection for the region use an expression that uses the datasource queryparameters value this allows the region to auto populate on the form on the state is selected in the dropdown add an execute entity action to call the function that will create the customer record in the local table (using method create ) and in the rest service ( function rest create customer ) use an expression to specify the value for each of the function's parameters create customers jigx title new customer type jig default icon global collaboration handshake header type component jig header options height small children type component image options source uri https //www dropbox com/scl/fi/ha9zh6wnixblrbubrfg3e/business 5475661 640 jpg?rlkey=anemjh5c9qsspvzt5ri0i9hva\&raw=1 \# add the reset state to clear the data in the form for the state dropdown onfocus type action reset state options state =@ctx jig components customerform state data \#define the data to use in the jig, the data has been synced by the global action to the local data provider from the rest service datasources region type datasource sqlite options provider data provider local entities \ entity us states query | select uss id as id, json extract(uss data, '$ state') as state, json extract(uss data, '$ abbreviation') as abbreviation, json extract(uss data, '$ statecapital') as statecapital, json extract(uss data, '$ region') as region, json extract(uss data, '$ flag') as flag from \[us states] as uss where json extract(uss data, '$ abbreviation') = @selectedstate queryparameters selectedstate =@ctx components usstate state value \#use static datasource as the data is predefined and will not change customertype type datasource static options data \ id 1 type new value new \ id 2 type gold value gold \ id 3 type silver value silver children \ type component form instanceid customerform options isdiscardchangesalertenabled false children \ type component text field instanceid companyname options label company name \ type component field row options children \ type component text field instanceid firstname options label first name \ type component text field instanceid lastname options label last name \ type component text field instanceid jobtitle options label job title \ type component text field instanceid email options label email \ type component text field instanceid phone1 options label mobile \ type component text field instanceid web options label web \ type component text field instanceid address options label street \ type component text field instanceid city options label city \ type component field row options children \ type component dropdown instanceid usstate options label state data =@ctx datasources us states item type component dropdown item options title =@ctx current item state value =@ctx current item abbreviation leftelement element avatar text =@ctx current item abbreviation uri =@ctx current item flag \ type component text field instanceid zip options label zip \ type component field row options children \ type component text field instanceid region options label region value =@ctx datasources region region \ type component dropdown instanceid customertype options label customer type data =@ctx datasources customertype item type component dropdown item options title =@ctx current item type value =@ctx current item value actions \ children \ type action execute entity #action to create the record options title create customer provider data provider rest entity customers method create #create the record in the local sqlite table function rest create customer #create the record in the rest service functionparameters #define the data to create in the record firstname =@ctx components firstname state value lastname =@ctx components lastname state value companyname =@ctx components companyname state value address =@ctx components address state value city =@ctx components city state value customertype =@ctx components customertype state value email =$lowercase(@ctx components email state value) jobtitle =@ctx components jobtitle state value phone1 =@ctx components phone1 state value phone2 =@ctx components phone1 state value region =@ctx components region state value state =@ctx components usstate state value web =$lowercase(@ctx components web state value) zip =@ctx components zip state value onsuccess type action go back list customer jigx title global customers type jig list icon global accelerator header type component jig header options height small children type component image options source uri https //www dropbox com/scl/fi/ha9zh6wnixblrbubrfg3e/business 5475661 640 jpg?rlkey=anemjh5c9qsspvzt5ri0i9hva\&raw=1 \#define the data to use in the jig, the data has been synced by the global action to the local data provider from the rest service datasources customers type datasource sqlite options provider data provider local entities \ entity customers query | select cus id as id, json extract(cus data, '$ firstname') as firstname, json extract(cus data, '$ lastname') as lastname, json extract(cus data, '$ companyname') as companyname, json extract(cus data, '$ address') as address, json extract(cus data, '$ city') as city, json extract(cus data, '$ state') as state, json extract(cus data, '$ zip') as zip, json extract(cus data, '$ phone1') as phone1, json extract(cus data, '$ phone2') as phone2, json extract(cus data, '$ email') as email, json extract(cus data, '$ web') as web, json extract(cus data, '$ customertype') as customertype, json extract(cus data, '$ jobtitle') as jobtitle, json extract(cus data, '$ logo') as logo from \[customers] as cus \ order by \ json extract(cus data, '$ companyname') data =@ctx datasources customers item type component list item options title =@ctx current item companyname subtitle =@ctx current item firstname & ' ' & @ctx current item lastname leftelement element avatar text =@ctx current item state uri =@ctx current item logo label title =$uppercase((@ctx current item customertype = 'silver' ? @ctx current item customertype @ctx current item customertype = 'gold' ? @ctx current item customertype '')) color \ when =@ctx current item customertype = 'gold' color color3 \ when =@ctx current item customertype = 'silver' color color14 swipeable left \ label delete icon delete 2 color negative onpress type action confirm options isconfirmedautomatically false onconfirmed type action execute entity #action to execute the delete options \#use the rest data provider to call the delete function provider data provider rest entity customers method delete #delete the record from the local sqlite table goback stay function rest delete customer #delete the record from the rest service \#specifiy the function parameters required by the function to delete the customer, in this example custid functionparameters custid =$number(@ctx current item id) #id of customer record to be deleted in rest service data id =@ctx current item id #id of customer to be deleted from local data provider modal title are you sure? description =('press confirm to permanently delete ' & @ctx current item companyname) \# add an action which adds a button to the bottom of the jig to go to the new customer form (jig) actions \ children \ type action go to options title add customer linkto new customer index for performance and offline support the data is synced from the rest service as soon as the app is opened or receives focus this is achieved by calling the global action in the onfocus and onload events index jigx name hello rest example title hello rest solution category sales \# onfocus is triggered whenever the jig is displayed the sync entities action in the global action calls the jigx rest function and populates the local sqlite tables on the device with the data returned from rest service onfocus type action execute action options action load data \# onload is triggered when the solution is loaded on the device the sync entities action in the global action calls the jigx rest function and populates the local sqlite tables on the device with the data returned from rest service onload type action execute action options action load data tabs home jigid list customers icon home apps logo