This article will walk you through how to call a Web Service in your Form via the Field2Base JavaScript Library.
To initialize a call, you will need to assign the endpoint for your Web Services provider.
WebServices.startRestService(Endpoint, Method);
where
Endpoint: Web Services provider URI
Method: GET, POST
You can add an HtmlHeader if Authorization or other information is required from your Web Services provider.
WebServices.addHtmlHeader(Header, Value);
where
Header: Header Name (ex: Authorization)
Value: Header Value (ex: Basic Token)
You can also add a parameter to your API request.
WebServices.addParameters(Key, Value);
where
Key: Parameter Key (ex: ID)
Value: Parameter Value (ex: 5)
Sample Code to call the Field2Base UserAPI Web Service
WebServices.startRestService("https://fieldconnect.field2base.com/v2/api/users", "GET");
WebServices.addHtmlHeader("Authorization", "Authentication CompanyID:Username:Password:API_Token);
WebServices.call("handleUserList");
Please note that the Authorization required in calling F2B Web Services is in the following format:
Authentication: Basic
CompanyID: Your F2B company ID
Username: Your API Account
Password: Your API Account Password
API_Token: Your API Sync Key
The result from the F2B UserAPI Web Service
{
"UserKey": "aaaaaaaa-e0b8-4bd0-8553-ac46bf854fad",
"UserName": "HowardChen",
"EnableWebAccess": true,
"Password": null,
"PasswordNeverExpires": false,
"ForcePasswordChangeOnLogin": null,
"FirstName": "Howard",
"MiddleName": null,
"LastName": "Chen",
"Phone": "9194628500x261",
"Fax": null,
"Email": "howard-chen@field2base.com",
"Projects": [
"bbbbbbbb-eb1a-44b6-8b8a-5aeb661ffb97",
"cccccccc-f475-44a9-a8df-8502c54e3de2"
],
"Groups": [],
"PrefersPDFAttachments": null,
"OptionalRecipient": null
}
Once you have called a Web Service, you will need to define a function that handles the result of that call.
WebServices.call("handleUserList");
The handleUserList is a JavaScript function that handles the return of your RESTful call.
The Web Services call will return a string(result) that contains the JSON data we see above.
To be able to parse this set of data, we use eval() to convert the result into an JavaScript Object.
We can get the current project ID by using Field2Base.getProjectId(), then match each of the returned Users' project records into userList[] array. Finally, assign the Datasource region in the Form with this array to populate the drop-down on the Form.
Form.getRegion("Page1@UserName").setDatasourceList(userList);
Sample code for the handleUserList function
function handleUserList(result)
{
var projectID = Field2Base.getProjectId();
var username = Field2Base.getUsername();
Form.getRegion("Page1@projectID").setValue(projectID);
var resultObj = eval(result);
var userList = [];
for (var i=0; i < resultObj.length; i++)
{
if(resultObj[i].UserName == username) continue; // Skip current user
for(var j=0; j< resultObj[i].Projects.length; j++)
{
if(resultObj[i].Projects[j] == projectID)
{
userList.push(resultObj[i].UserName); // If project ID matched, push into
userList array
break;
}
}
}
Form.getRegion("Page1@UserName").setDatasourceList(userList);
Form.getRegion("Page1@msg").clearValue()
}
Please download the attached eForm for a working sample. You can try out this Form by uploading to it your Company in the F2B Admin Portal. This sample Form will populate the Users with access to the Folder the Form is in. By doing so, you can manage your Users via the Admin Portal, and let the Form pick up the Users that are available dynamically without using a Data File.
Note | You will need to change the Authorization information under the Web Services header to match your Company in the Field2Base Admin Portal. |
Now, you can enjoy the power of Web Services by using REST API in F2B's JavaScript Library to integrate with almost anything.
Comments
0 comments
Please sign in to leave a comment.