Dynamic details using hooks in collection

other_details - dynamic text boxes to fetch required key and value and convert the fetched dynamic details to hash format and store in the other_details attribute for respective client_detail.

action ‘Commercial Mapping’, fields: [{
field: ‘unique_identifier’,
type: ‘Number’,
# is_read_only: true,
is_required: false
}, {
field: ‘vendor_dealer_code’,
type: ‘String’,
is_required: false
}, {
field: ‘vendor_dealer_name’,
type: ‘String’,
is_required: false
}, {
field: ‘anchor_code’,
type: ‘String’,
is_required: false
}, {
field: ‘anchor_name’,
type: ‘String’,
is_required: false
}, {
field: ‘program_code’,
type: ‘String’,
is_required: false,
}, {
field: ‘sub_program_code’,
type: ‘Number’,
is_required: false
}, {
field: ‘other_details’,
type: ‘String’,
is_required: false
}]

in place of other details it should come like above mention requirements. help me how to do that

Hello @Kishore_Kumar ,

Welcome to the community! I would like to ask you to give me more details in your request so that we can solve your problem as soon as possible. What is the name of your project? Can you also give us more details about the feature you use and the actions you take when you encounter this problem?
:pray:

Kind regards,

Florian

class Forest::InvestorVendorAnchorProgram
include ForestLiana::Collection

collection :InvestorVendorAnchorProgram

action ‘Commercial Mapping’, fields: [{

And what is your project name ?

viv_bill_discounting … but project name is not required for writing hooks ryt?

Hello @Kishore_Kumar :wave:

Thanks for posting in the community, this looks interesting !

Let me recap things real quick just to ensure we are going in the right direction.

1- The other_details is a field that, contains multiple values that will be stored in a hash (eg an object, let’s say a json field)
2- The values in the other_details are dynamic, meaning that what can be entered in there must first be fetched from somewhere else, then proposed to selection to the operator performing the smart action.

So the Idea would be:
1- fetch the possible values from a specific place for the dropdown other_details. Indeed, using a global hook would be perfect.
2- Store the values selected by the operator into a json field in the database.

Please don’t hesitate to correct me if I’m wrong in the assumptions, otherwise, let’s discuss the solution we have proposed :slightly_smiling_face:

Steve.

yeah I think it will work. Can you provide the logic for the same by referring above collection.

Hello @Kishore_Kumar :wave:

Here is a little smart action i’ve coded quickly for you. Please, read the comments on the code I’m providing, they will guide you and they will explain every steps of what needs to be performed. Observe that mainly, the field in which you want to have the dynamic values has changed, and the way we initialise the smart action form is now dynamic, meaning that based on the current record we want to execute the smart action on, the form will adapt.

By using this smart action and when the operator will validate the form, you should then be provided with the values the operator have filed in. You will then be free on what to do with these values (eg save them in your database). Here is some more info on how to handle smart action values (see the third snippet in the rails section)

class Forest::User
  include ForestLiana::Collection

  collection :User
  
  action 'Commercial Mapping', type: 'single', fields: [{
      field: 'unique_identifier',
      type: 'Number',
      is_required: false
    }, {
      field: 'vendor_dealer_code',
      type: 'String',
      is_required: false
    }, {
      field: 'vendor_dealer_name',
      type: 'String',
      is_required: false
    }, {
      field: 'anchor_code',
      type: 'String',
      is_required: false
    }, {
      field: 'anchor_name',
      type: 'String',
      is_required: false
    }, {
      field: 'program_code',
      type: 'String',
      is_required: false,
    }, {
      field: 'sub_program_code',
      type: 'Number',
      is_required: false
    }, {
      field: 'other_details',
      #set the field to ['Enum'] to indicate multiple values can be selected
      type: ['Enum'],
      is_required: false
    }],
    #Use a load hook to prepar the form values
    :hooks => {
      :load => -> (context) {
        #Get the id of the resource the smart action is being run for
        currentRecordId = context[:params][:data][:attributes][:ids][0];

        #Get the field you need to set selectable values on
        otherDetailsField = context[:fields].find{|field| field[:field] == 'other_details'}

        #Implement you own logic, here is a simple if..else to illustrate
        if currentRecordId == '3'
          #Gather the values selectable for the selected record. It can either be a database call or an API call
          fetchedValues = ['test value for Steve', 'test value 2 for Steve', 'test value 3 for Steve', 'test value 4 for Steve']
        else
          #Gather the values selectable for the selected record. It can either be a database call or an API call
          fetchedValues = ['test value for Morgan', 'test value 2 for Morgan', 'test value 3 for Morgan', 'test value 4 for Morgan']
        end

        #Set the selectable values in the actual field
        otherDetailsField['enums'] = fetchedValues

        #Return the new set of fields
        return context[:fields]
      }
    }
end

I hope this helps, don’t hesitate if you need more help !

Steve.

in other details action , I need to enter 2 values manually, one is key and one is value and have to store both in same in same row .
for example key is “customer id” and value is “123” i have to store it as {“customer_id”:“123”} in other_details column.
help in implementing this

Should the keys (eg customer_id) should be entered by the operator executing the smart action (in other word, should him manually input the key) or should the keys be set automatically from the code ?

key also to be enter manually
need it to be created in a dynamic way where instead of using “load” by using button click or something to create it dynamically. Because in some cases key value may have more fields .
for example :“{”“co_applicants”“: [{”“customer_code”": ““GLBCUST8376376"”, ““customer_type””: ““individual””}]}”

Alright I see. The more complex structure of data you need, the more difficult will your implementation be.

To give you the best answer, can you please past here a complete and concrete example of what the other_details field could be please ? Let’s take the “worst” case scenario.