Is it possible to add a unsaved record as a draft? Yes, it is possible. It is possible using our form script capability of object.
I assume that the reader of this article has a good understanding of Java script, Jquery. User should have some knowledge on add or delete DOM elements. User should have some knowledge on Agileapps exposed Rest API and Ajax calls.
In draft capability of a record has following challenges.
- Where to store those draft data and how to store?
- How to get previously saved data in the current form?
- How to differentiate among multiple drafts of a record created by different users?
- How to apply a draft, if record form layout got changed?
- First we will create an object, let’s name it “DraftStorage”. Create some fields of these object “related_record, related_object, user_id, draft_data”. Make draft_data field as of type “text area” and others can be text field.
- This DraftStorage will contain all of our draft records.
- Now pick any object where you want to apply this draft capability and write some form script
- Go to Setting>> Object >> select an object >> select from >> select a layout >> select Form script >> Onload script >> New ui script
In Onload Script box write below
In reusable script function write below
4. Achived Challenges
- Using above script for any record user can see a "save draft" button (Button name and style can be changed).
- Once User clicks the "save draft" button it will collect all the metadata of current record (Meta data will be collected by traversing all the section with a fraction of second), then it will form a JSON string using those meta data.
- At the same time it will create a record in DraftStorage , that record will capture its ObjectId, RecordId, LoggedInUserId, and draft meta data.
- On loading of the object it will execute a SQL statement to check wheather a draft exist with the reference of current record id , object id and logged in user id. If it exitst user can see "apply draft " button.
- By clicking the "apply draft " button , it will populate the saved draft data with the fields.
- Once a draft applied the same draft referenced record will delete from draftStorage object.
- If a draft already exist and user again save the draft with some other modification then it will override with previous draft.
- Once a draft applied, changes will apply to all fields, then "apply draft" button will disappear, Now the user has to save the record manually.
- If a form layout structure changed, like if a section deleted then draft will apply for other sections
- If a section renamed then for that section draft will not apply.
- If a field is deleted or renamed then the draft will not apply to that field.
- if a field order is changed or section order is changed then the draft will apply with out any issue.
- if a field is deleted and another field is created with the same name then draft will add the field value as of last field value.
6.Anonymous user access
- This draft can be accessible to the creator of the draft only.
- Users can not access others draft and apply.
- For a record, user can store one draft only.
- This draft capability is not applicable for file, image, and junction object fields.
- If a field is hidden or disabled then draft will not apply to that field.
- User should not change anything in records of draft object, otherwise it will effect in onload script of the object.
- For good practice user should hide the draft object from tab section.