[How-to with Example] Getting field value/data in Client Script from lookup field record

[How-to with Example] Getting field value/data in Client Script from lookup field record

Imagine you have a field you would like to prefill a field based on a value from another record in another module. In this case, we are looking into a scenario where an Asset (custom module) module is being created from a Deal module. Meaning, there is a lookup to the deal module from the asset module.


There is a need to validate a field called ' Business Unit' in the asset record that is being created. This field needs to be the same as it is with the 'Business Unit' value in the deal record of which this asset record looks up to. At the same time, the 'Business Unit' field, having supposedly being prefilled, should be disabled for the users.
Business Unit in Deal record
has to be the same with Business Unit in Asset record
And, if the deal field is null, the 'Business Unit' field in the asset record shall be nulled and reenabled again for users to fill it in manually. The following is the script written and the event is onChange
  1. //checking if there is a value in the lookup field called 'Deal'
  2. if (ZDK.Page.getField('Deal').getValue() != null) {
  3. //getting the value of the 'Deal' lookup field
  4.     var dealRec = ZDK.Page.getField('Deal').getValue();
  5. //getting the Deal record ID
  6.     var dealId = dealRec.id;
  7.     //get business unit value/data from deal record
  8.     var businessUnit = ZDK.Apps.CRM.Deals.fetchById(dealId).Business_Unit;
  9. //we use 'log' to see the value it produces during testing
  10.     log(businessUnit);
  11.     var bu = ZDK.Page.getField('Business_Unit');
  12. //setting the Business Unit to be disabled
  13.     bu.setReadOnly(true);
  14. //prefilling the Business Unit field with the fetched value from Deal record
  15.     bu.setValue(businessUnit);
  16. }
  17. else
  18. {
  19. //if there is no Deal specified, then set the Business Unit field as null and reenable the field
  20.     ZDK.Page.getField('Business_Unit').setValue(null);
  21.     ZDK.Page.getField('Business_Unit').setReadOnly(false);
  22. }
Again this will:
  1. When the Deal field is populated, it will get the Business Unit value from the deal record and prefill it to Business unit field in Asset record.
  2. When Deal field is populated, it will disable the Business Unit field.
  3. When the field is null, it will empty the Business Unit field in Asset record.
  4. When the field is null, it will reenable Business Unit field.
When Deal is populated
This is something that myself as a non-coder is able to do. And being a non-coder, I took it upon myself to share this in a (hopefully) much simpler way so you can understand and start your own journey of using client script.

Hope this is useful for you. As always, good luck!

Nik, aplikasi.us
Zoho Premium Partner