An article on one of my first client scripts for ZohoCRM. This retrieves and populates an email field based on when a contact is selected in a lookup field.
Why?
I could do a normal automation/workflow on save of the record, but the client wants the email to be prefilled in the field as soon as a contact is selected.
How?
This needs to behave like a formula field which changes and displays before saving the record. At time of print however, I can't get a formula field to retrieve a field from another CRM record.
// freezes the GUI? ZDK.UI.freeze(true); // attempt try { // get the ID from the lookup field called "Vendor Contact" var v_VendorContactID = ZDK.Page.getField('Vendor_Contact').getValue().id; // get the record details for this contact record (note this is ZDK.Apps.CRM but my syntax highlighter lowercases this) var r_ContactDetails = ZDK.Apps.CRM.Contacts.fetchById(v_VendorContactID); // get the email on this contact record var v_ContactEmail = r_ContactDetails.Email; // check it isn't blank if (v_ContactEmail != "") { // display the found value in the field called "Vendor Contact Email" ZDK.Page.getField("Vendor_Contact_Email").setValue(v_ContactEmail); } } catch (e) { // return error (don't display it, just show it in the logs) log(e); //ZDK.Client.showMessage("Client Script error"); } // unfreeze the GUI ZDK.UI.freeze(false); // return false to prevent saving of record return false;
- // freezes the GUI?
- ZDK.UI.freeze(true);
- // attempt
- try {
- // get the ID from the lookup field called "Vendor Contact"
- var v_VendorContactID = ZDK.Page.getField('Vendor_Contact').getValue().id;
- // get the record details for this contact record (note this is ZDK.Apps.CRM but my syntax highlighter lowercases this)
- var r_ContactDetails = ZDK.Apps.crm.Contacts.fetchById(v_VendorContactID);
- // get the email on this contact record
- var v_ContactEmail = r_ContactDetails.Email;
- // check it isn't blank
- if (v_ContactEmail != "") {
- // display the found value in the field called "Vendor Contact Email"
- ZDK.Page.getField("Vendor_Contact_Email").setValue(v_ContactEmail);
- }
- } catch (e) {
- // return error (don't display it, just show it in the logs)
- log(e);
- //ZDK.Client.showMessage("Client Script error");
- }
- // unfreeze the GUI
- ZDK.UI.freeze(false);
- // return false to prevent saving of record
- return false;
Caveat(s)
- This only happens if the record is being edited (as in user clicked on "Edit") rather than the inline edits that can be done (eg. user clicked on field in view mode and edited just one field). The only other event you can use this in inline editing mode in is the Canvas mode.
Error(s) Encountered
- Contacts doesn't seem to appear in Client Script IDE. Didn't for me but then it isn't too hard to guess what the module name is.