Client-side scripting design and processing

0
1168

Client-side processing depends on the form loading first. Making record updates prior to form load can produce unexpected results that bypass client-side processing.

Let’s see some useful stuff which will be used at client end.

Minimize the server call/lookups
    1. Try to use client data as much as possible to eliminate the server call.
    2. Client scripting uses either data available on the form or retrieved it from the server. The best ways to get information from the server are asynchronous GlideAjax and g_scratchpad.
    3. GlideRecord and g_form.getReference() are also used for retrieving data from server. However, these methods are not recommended to use at client place as it leads to performance issue.
Retrieve server data using g_scratchpad

When we requires information at client side but data are not available on the form on that case we can use g_scratchpad to make that available on form.

Real World Scenarios:

If we know what information are required from the server before the form is loaded then we can create display business rule to hold the information using g_scratchpad

 

Requirement: Change Request:

If logged-in user if a member of CAB Approval group then enable the fields:

            1. CAB Delegate
            2. CAB Date and
            3. Cab recommendation

Solution:

        1. Create display business rule on change request table with below code.
          g_scratchpad.cabApproval =gs.getUser().isMemberOf('CAB Approval');
        2. Create a onLoad client script on Change request table with below code.
          var isMemberOfcabApproval = g_scratchpad.cabApproval;
          
          if (isMemberOfcabApproval)
              g_form.setReadOnly('cab_delegate', false);
              else
              g_form.setReadOnly('cab_delegate', true);
          

           

Use the setValue() displayValue parameter for reference fields
For Live Demonstration you can watch this video.
        1. setValue() method will be used to set the field value on the form. It accept three parameter where 2 are mandatory and one is optional.
        2. When using setValue() on a reference field then it’s a best practice to include the displayValue parameter to avoid additional server calls.
        3. If you set the value without the displayValue then the system make a synchronous call to server to retrieve the display value for the record you specified. This extra call to the server impact the performance.
        4. System will make extra server call in this case
          var sys_id = '1234153cc611227c000bbd1bd8ru2022';
           
          g_form.setValue('assigned_to', sys_id); 
          
          // Client needs to go back to the server to fetch the name that goes with this ID
          
        5. System will not make extra server call in this case
          var sys_id = '1234153cc611227c000bbd1bd8ru2022';
          var name = 'Runjay Patel';
          
          g_form.setValue('assigned_to', sys_id, name); 
          
          // No server call required
          

           

Avoid DOM manipulation
      1. Avoid Document Object Model (DOM) manipulation if possible. It can cause a maintainability issue when browsers are updated.
      2. Instead of DOM manipulation we can use the GlideForm API or think a different approach for the solution.
For Live Demonstration you can watch this video.

Requirement: Incident

Disable/Hide the attachment icon if state of incident equal to closed.

Solution:

      1. Create Onload client script on incident table
      2. We can hide attachment icon using DOM manipulation, which is not recommended.
        if(g_form.getValue('state') == 7)
            document.getElementById('header_add_attachment').style.display = 'none';
        
      3. We can use g_form.disableAttachments();
        if(g_form.getValue('state') == 7)
        g_form.disableAttachments();
        

         

Note: ‘Isolate script’ must be selected as false, if you want to keep the dom enabled only for this client script.
Enclose code in functions

Client scripts without a function cause issues with variable scope. It is always a good practice to write code inside a function. Write code outside of function may lead to unexpected result that are very difficult to troubleshoot.

For Live Demonstration you can watch this video.
          • var state = "6";
             
            function onSubmit() {
             
            if(g_form.getValue('incident_state') == state) {
               	alert("This incident is Resolved");
               }
            }
            
            function onSubmit() {
             var state = "6";
            
               if(g_form.getValue('incident_state') == state) {
               	alert("This incident is Resolved");
               }
            }
            

             

 

Get Latest Update From Runjay Patel

We don’t spam! Read our privacy policy for more info.

LEAVE A REPLY

Please enter your comment!
Please enter your name here