Creating read-only conditional fields

To control when a field becomes read-only or read/write dynamically in a Maximo Anywhere app, you can make the field conditional to be read-only.

About this task

You specify an event handler in the app.xml file to control whether a field that depends on values of another field is read-only. Event handlers are implemented in JavaScript files that are associated with a view in the mobile app.

For example, if a work order has a work type of Emergency Maintenance, the Priority field should become read-only. The Priority field is not needed for other types of work orders, so you need to apply a condition to control when the field is read-only. You create an event handler for the Priority field to monitor the resource and attribute that is associated with the work type and make the field read-only if a mobile user selects Emergency Maintenance in the Work Type field. If a mobile user changes the value for the work type, the handler also checks the value to determine whether the Priority field is read-only.

Procedure

  1. Open the app.xml file for the app that you want to update, and add the field that you want to apply the condition to.
  2. In the UI section of the app.xml file, add an initializer type of event handler to the fields.
    1. In the <text> element of the new field, add a child element named <eventHandlers>.
    2. In the <eventHandlers> element, add a child element named <eventHandler>.
    3. Specify initialize as the event attribute, and add the method and class attributes.

    For example, to make the Priority field read-only for emergency work types, add an initializer type of event handler to the view that the fields belong to. The event that is associated with the handler for the Work Order Details view is named WODetailExtensionHandler.

    <view id="WorkExecution.WorkDetailView" label="Work Order Details">
    ...
    		<eventHandlers>
    			<eventHandler event="initialize" method="fetchAllListSizes"
    				class="application.handlers.WODetailHandler" />
    			
    			<eventHandler event="initialize" method="handleConditionalReadOnlyPriority"
    				class="application.customerExtensions.WODetailExtensionHandler" />
    			
    			<eventHandler event="render"	class="application.handlers.WODetailHandler" 
    				method="refreshAllListSizes" />
    			<eventHandler event="initialize"
    				class="application.handlers.MetersListHandler" method="initializeMeters" />
    		</eventHandlers>
    ....
    </view>
  3. Apply the condition for the event handler to a JavaScript file.
    1. Create a folder for the handler in the MaximoAnywhere\apps\app_name\common\js\application directory. The folder name must match the name that is applied to the class attribute of the event handler.
    2. Add the JavaScript file to the MaximoAnywhere\apps\app_name\common\js\application directory. The name of the JavaScript file must match the name that is applied to the class attribute of the event handler.
    3. Add the condition for the initialize event handler to the JavaScript file.
    For example, to add the condition for the event handler to make the Priority field read-only for emergency work types, create the WODetailExtensionHandler.js file in the MaximoAnywhere\apps\app_name\common\js\application\customerExtensions directory and add the JavaScript code.
    define("application/customerExtensions/WODetailExtensionHandler", 
    	   		[ "dojo/_base/declare",
    	     		"dojo/_base/lang",
    	     		"platform/handlers/_ApplicationHandlerBase",	  
    	     		"application/handlers/CommonHandler"],
    function(declare, lang, ApplicationHandlerBase, CommonHandler) {
    	
    	return declare( [ApplicationHandlerBase],  {
    			
    		handleConditionalReadOnlyPriority: function(eventContext){
    			// This is an initialize handler for the WO Details View
    			// to handle changes made to the worktype field 
    			// and make the Priority field read-only
    			
    			// current workOrder is on the eventContext
    			var currWO = eventContext.getResource().getCurrentRecord();
    			var worktype = currWO.get('worktype');
    			
    			// Makes the priority field read-only if worktype equals EMERGENCY
    			currWO.getRuntimeFieldMetadata('priority').set('readonly', worktype && worktype=="EM"); 
    							
    			// Hook a listener to watch the worktype attribute, 
    			// and make the priority attribute read-only when the worktype is Emergency
    		 	eventContext.addResourceWatchHandle(currWO.watch('worktype',
    				lang.hitch(this, function(attrName, oldValue, newValue)
    						{
    							currWO.getRuntimeFieldMetadata('priority').set('readonly', newValue && newValue=="EM"); 
    						}
    			)));
    		}
    		
    	});
    });
  4. Save your changes and preview the updated mobile app in a mobile browser simulator.

What to do next

Build and deploy the app.