function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
fiona gentryfiona gentry 

How To Make Level_2 and Level_3 as ReadOnly in Custom Salesforce Lightning component

How To Make Level_2 and Level_3 as ReadOnly in Salesforce Custome Lightning component

I have a controller which has 3 text boxes ,now i want level2 and level3 as ReadOnly ,how can i do that

Here is custom component currently which has editable Level2 and Level3,my need is to make these 2 fields as read only
User-added imagehere is the .cmp file
 
<aura:component implements="force:hasRecordId,force:appHostable,flexipage:availableForAllPageTypes,force:lightningQuickAction" access="global" controller="Stack">
						<aura:attribute access="private" type="List" name="selection" default="[]"/>
						<aura:attribute access="private" type="List" name="errors" default="[]"/>

						<lightning:card title="ERT Case Type">
							
							<lightning:recordEditForm aura:id="myForm" objectApiName="ERT_Case_Type__c" onsubmit="{!c.onSubmit}" onsuccess="{!c.onSuccess}">
							<lightning:messages />
							<c:Lookup selection="{!v.selection}" onSearch="{!c.lookupSearch}" onSelection="{!c.useSelected}" errors="{!v.errors}" label="Search" placeholder="Search ERT Case Types Data"/>
							<lightning:inputField aura:id="Level_1__c" fieldName="Level_1__c" />
							<lightning:inputField aura:id="Level_2__c" fieldName="Level_2__c" />
							<lightning:inputField aura:id="Level_3__c" fieldName="Level_3__c" />
							<lightning:button class="slds-m-top_small" variant="brand" type="submit" name="save" label="Save" />
						</lightning:recordEditForm>
						</lightning:card>
					</aura:component>
here is .js file
 
({
						lookupSearch : function(component, event, helper) {
							// Get the lookup component that fired the search event
							const lookupComponent = event.getSource();
							const serverSearchAction = component.get('c.search');
							lookupComponent.search(serverSearchAction);
						},

						useSelected: function(component, event, helper) {
							const selection = component.get('v.selection');
							const errors = component.get('v.errors');
							
							if (selection.length) {
								if(errors.length){  // Clear errors, if any
									component.set('v.errors', []);
								}
								let levels = selection[0].subtitle.split('; ');
								component.find('Level_1__c').set('v.value', levels[0]);
								component.find('Level_2__c').set('v.value', levels[1]);
								component.find('Level_3__c').set('v.value', levels[2]);
							}else {
								// Somebody "selected" empty option = cleared the search box
								component.find('Level_1__c').set('v.value', '');
								component.find('Level_2__c').set('v.value', '');
								component.find('Level_3__c').set('v.value', '');
							}
						},
						onSubmit: function(component, event, helper) {
							debugger;
							event.preventDefault();       // stop the form from submitting
							var fields = event.getParam('fields');
							fields.Case__c = component.get('v.recordId'); // link to "this" Case
							component.find('myForm').submit(fields);
						},
						onSuccess: function(component, event, helper){
							var toastEvent = $A.get("e.force:showToast");
							toastEvent.setParams({
								"title": "Success!",
								"message": "Case Type saved OK, refreshing",
								"type": "success"
							});
							toastEvent.fire();
							$A.get('e.force:refreshView').fire(); // reload page
						}
					})

Your response is highly appreciated

Regards,
Fiona
 
Best Answer chosen by fiona gentry
AnudeepAnudeep (Salesforce Developers) 
There is a readonly attribute in <lightning:inputField> as per the documentation (Specifies whether an input field is read-only. This value defaults to false. Not supported for the following field types: rich text, picklist, multi-select picklist, and lookup. A read-only field is not disabled by default)

However, I have seen a few questions in the past around the community saying it actually does not work. I recommend giving a try, if it doesn't help you should go with the disabled attribute instead. See the following posts

readonly property of lightning:input not working

Lightning inputText component readonly
 
<lightning:recordEditForm objectApiName="Account" recordId="001R00000000000000">
    <lightning:inputField fieldName="Name" readonly="true"/>
    <lightning:inputField fieldName="Account_Site__c"/>
    <lightning:button class="slds-m-top_small" variant="brand" type="submit" name="update" label="Update" />
</lightning:recordEditForm>

Let me know if this helps, if it does, please close the query by marking it as solved. It may help others in the community. Thank You!