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
Hermann OuréHermann Ouré 

aura component duplicate value

Hello,
I have an aura component to show / hide field depending on checkbox values. What I'm trying to do is to when the checkbox for the current user is checked, to show the field on the component. But if the current user profile name is different from the record owner, to make the field on the component disabled.
The problem is that 2 fields on my component is showing when I only want to display one.

aura markup
How do I change my markup to apply the logic on the same lightning textarea so only 1 is shown
 
<aura:component controller="ActionDescriptionFieldShowHideController" implements="flexipage:availableForRecordHome,force:hasRecordId" access="global" >
    
    <aura:attribute name="recordId" type="String"/>
    <aura:attribute name="actionRec" type="Action__c" default="{'sObjectType':'Action__c'}"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <lightning:spinner alternativeText="Loading" size="large" aura:id="spinner" class="slds-hide" />
    
    <aura:attribute name="CurrentUser" type="Object"/>
    <force:recordData recordId="{!$SObjectType.CurrentUser.Id}"
                      fields="Profile.Name"
                      targetFields="{!v.CurrentUser}"/>
    
    
    <form class="slds-form_stacked">
    
        
    <aura:if isTrue="{!and(v.actionRec.Commercial__c == true, v.CurrentUser.Profile.Name == 'Sales')}">
        <lightning:textarea label="Description" name="desc" value="{!v.actionRec.Description__c }"/>  
        <aura:set attribute="else">
            <aura:if isTrue="{!and(v.actionRec.Commercial__c == true, v.CurrentUser.Profile.Name != 'Sales')}">
                <lightning:textarea label="Description" name="desc" disabled="true" value="{!v.actionRec.Description__c }"/> 
            </aura:if>
        </aura:set>  
    </aura:if>
        
    <footer class="slds-modal__footer">
        <lightning:button variant="brand"
                          label="Save"
                          title="Save"
                          onclick="{! c.actionSave}" />
    </footer>
    </form>
</aura:component>

 
mukesh guptamukesh gupta
Hi Hermann,

Please use below code:-
 
<aura:component controller="ActionDescriptionFieldShowHideController" implements="flexipage:availableForRecordHome,force:hasRecordId" access="global" >
    
    <aura:attribute name="recordId" type="String"/>
    <aura:attribute name="actionRec" type="Action__c" default="{'sObjectType':'Action__c'}"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <lightning:spinner alternativeText="Loading" size="large" aura:id="spinner" class="slds-hide" />
    
    <aura:attribute name="CurrentUser" type="Object"/>
    <force:recordData recordId="{!$SObjectType.CurrentUser.Id}"
                      fields="Profile.Name"
                      targetFields="{!v.CurrentUser}"/>
    
    
    <form class="slds-form_stacked">
    
        
    <aura:if isTrue="{!and(v.actionRec.Commercial__c == true, v.CurrentUser.Profile.Name == 'Sales')}">
        <lightning:textarea label="Description" name="desc" value="{!v.actionRec.Description__c }"/>  
        
    </aura:if>
	
	<aura:if isTrue="{!and(v.actionRec.Commercial__c == true, v.CurrentUser.Profile.Name != 'Sales')}">
		<lightning:textarea label="Description" name="desc" disabled="true" value="{!v.actionRec.Description__c }"/> 
	</aura:if>
     
    <footer class="slds-modal__footer">
        <lightning:button variant="brand"
                          label="Save"
                          title="Save"
                          onclick="{! c.actionSave}" />
    </footer>
    </form>
</aura:component>

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
Hermann OuréHermann Ouré
Hello Mukesh,
Thanks for your reply.
I have tried the code you provided but it's still showing 2 fields.
Dushyant SonwarDushyant Sonwar
Hermann,

Try this 
<aura:if isTrue="{!v.actionRec.Commercial__c == true}">
	<aura:if isTrue="{! v.CurrentUser.Profile.Name == 'Sales'}">
			<lightning:textarea label="Description" name="desc" value="{!v.actionRec.Description__c }"/>  
			<aura:set attribute="else">
					<lightning:textarea label="Description" name="desc" disabled="true" value="{!v.actionRec.Description__c }"/> 
			</aura:set>  
		</aura:if>
</aura:if>

Hope this helps!
mukesh guptamukesh gupta
Hi Hermann,

Can you please debug value in your component.

Commercial==>>>>> {!v.actionRec.Commercial__c}

 Profile.Name ==>>>> {!v.CurrentUser.Profile.Name}


if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
Hermann OuréHermann Ouré

Hello Mukesh,
Not sure how to put debug log in aura
Also I tried to update my component in the following manner

 

<aura:component controller="ActionDescriptionFieldShowHideController" implements="flexipage:availableForRecordHome,force:hasRecordId" access="global" >
    
    <aura:attribute name="recordId" type="String"/>
    <aura:attribute name="actionRec" type="Action__c" default="{'sObjectType':'Action__c'}"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <lightning:spinner alternativeText="Loading" size="large" aura:id="spinner" class="slds-hide" />
    
    <aura:attribute name="CurrentUser" type="Object"/>
    <force:recordData recordId="{!$SObjectType.CurrentUser.Id}"
                      fields="Profile.Name"
                      targetFields="{!v.CurrentUser}"/>
    
    <aura:attribute name="recordOwner" type="Object"/>
    <force:recordData recordId="{!v.recordId}"
                      fields="Profile.Name"
                      targetFields="{!v.recordOwner}"/>
    
    <form class="slds-form_stacked">

<!-- ***Nested Conditions*** -->
     <aura:if isTrue="{!v.actionRec.Sales__c == true}">
        <aura:if isTrue="{! v.CurrentUser.Profile.Name == 'Sales''}">
			<lightning:textarea label="Description" name="desc" value="{!v.actionRec.Description__c }"/>  
			<aura:set attribute="else">
                <aura:if isTrue="{!v.CurrentUser.Profile.Name != v.recordOwner.Profile.Name}">
					<lightning:textarea label="Description" name="desc" disabled="true" value="{!v.actionRec.Description__c }"/>
				</aura:if> 
			</aura:set>  
		</aura:if>
    </aura:if> 
    
    <aura:if isTrue="{!v.actionRec.Business__c == true}">
        <aura:if isTrue="{! v.CurrentUser.Profile.Name == 'Business'}">
			<lightning:textarea label="Description" name="desc" value="{!v.actionRec.Description__c }"/>  
			<aura:set attribute="else">
                <aura:if isTrue="{!v.CurrentUser.Profile.Name != v.recordOwner.Profile.Name}">
					<lightning:textarea label="Description" name="desc" disabled="true" value="{!v.actionRec.Description__c }"/>
				</aura:if> 
			</aura:set>  
		</aura:if>
    </aura:if> 
    
    <aura:if isTrue="{!v.actionRec.Marketing__c == true}">
        <aura:if isTrue="{! v.CurrentUser.Profile.Name == 'Marketing'}">
			<lightning:textarea label="Description" name="desc" value="{!v.actionRec.Description__c }"/>  
			<aura:set attribute="else">
                <aura:if isTrue="{!v.CurrentUser.Profile.Name != v.recordOwner.Profile.Name}">
					<lightning:textarea label="Description" name="desc" disabled="true" value="{!v.actionRec.Description__c }"/>
				</aura:if> 
			</aura:set>  
		</aura:if>
    </aura:if> 
      
    <footer class="slds-modal__footer">
        <lightning:button variant="brand"
                          label="Save"
                          title="Save"
                          onclick="{! c.actionSave}" />
    </footer>
    </form>
</aura:component>

But still showing the lightning:textarea twice 
Hermann OuréHermann Ouré
I also tried this but still showing duplicate value
<!-- ***Nested Conditions*** -->
     <aura:if isTrue="{!v.actionRec.Commercial__c == true}">
        <aura:if isTrue="{! v.CurrentUser.Profile.Name == 'Sales'}">
			<lightning:textarea label="Description" name="desc" value="{!v.actionRec.Description__c }"/>  
			<aura:set attribute="else">
			     <lightning:textarea label="Description" name="desc" disabled="true" value="{!v.actionRec.Description__c }"/>
				
			</aura:set>  
		</aura:if>
    </aura:if> 
    
    <aura:if isTrue="{!v.actionRec.Business__c == true}">
        <aura:if isTrue="{! v.CurrentUser.Profile.Name == 'Business'}">
			<lightning:textarea label="Description" name="desc" value="{!v.actionRec.Description__c }"/>  
			<aura:set attribute="else">
				<lightning:textarea label="Description" name="desc" disabled="true" value="{!v.actionRec.Description__c }"/>
				
			</aura:set>  
		</aura:if>
    </aura:if> 
    
    <aura:if isTrue="{!v.actionRec.Marketing__c == true}">
        <aura:if isTrue="{! v.CurrentUser.Profile.Name == 'Marketing'}">
			<lightning:textarea label="Description" name="desc" value="{!v.actionRec.Description__c }"/>  
			<aura:set attribute="else">
				<lightning:textarea label="Description" name="desc" disabled="true" value="{!v.actionRec.Description__c }"/>
				
			</aura:set>  
		</aura:if>
    </aura:if>

 
Hermann OuréHermann Ouré
it seems that the trick is to conditionally change <lightning:textarea/> disabled attribute using the controller.js rather than using aura:if
if anyone knows how to implement it. that would be very much appreciated
so 
<lightning:textarea label="Description" name="desc" disabled="true" value="{!v.actionRec.Description__c }"/>
would become something like
<lightning:textarea aura:id="descrpt" label="Description" name="desc" value="{!v.actionRec.Description__c }" disabled="{!v.disabled}"/>


 
Hermann OuréHermann Ouré
Almost got it working.
The only issue now is that as soon as I enter a value, the <lightning:textarea/> becomes disabled
The issue either comes from my function or from the 
<aura:attribute name="recordOwner" type="Object"/>
    <force:recordData recordId="{!v.recordId}"
                      fields="Profile.Name"
                      targetFields="{!v.recordOwner}"/>

here is how I have updated my cmp
<aura:component controller="ActionDescriptionFieldShowHideController" implements="flexipage:availableForRecordHome,force:hasRecordId" access="global" >
    
    <aura:attribute name="recordId" type="String"/>
    <aura:attribute name="actionRec" type="Action__c" default="{'sObjectType':'Action__c'}"/>
    <aura:attribute name="disabled" type="Boolean" default="false"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <lightning:spinner alternativeText="Loading" size="large" aura:id="spinner" class="slds-hide" />
    
    <aura:attribute name="CurrentUser" type="Object"/>
    <force:recordData recordId="{!$SObjectType.CurrentUser.Id}"
                      fields="Profile.Name"
                      targetFields="{!v.CurrentUser}"/>
    
    <aura:attribute name="recordOwner" type="Object"/>
    <force:recordData recordId="{!v.recordId}"
                      fields="Profile.Name"
                      targetFields="{!v.recordOwner}"/>
    
    <form class="slds-form_stacked">
    
        <!-- ***Single Condition*** -->
    <aura:if isTrue="{!and(v.actionRec.Commercial__c == true, v.CurrentUser.Profile.Name == '[AZ] ASH')}">
        <lightning:textarea aura:id="desc" label="Description" name="desc" value="{!v.actionRec.Description__c }" 
                            disabled="{!v.disabled}" onchange="{!c.update}"/>                     
    </aura:if>
    
    <aura:if isTrue="{!and(v.actionRec.Medical__c == true, v.CurrentUser.Profile.Name == '[AZ] RMR')}">
        <lightning:textarea aura:id="desc" label="Description" name="desc" value="{!v.actionRec.Description__c }" 
                            disabled="{!v.disabled}" onchange="{!c.update}"/>                     
    </aura:if>
    
     <aura:if isTrue="{!and(v.actionRec.Diagnostic__c == true, v.CurrentUser.Profile.Name == '[AZ] DxL')}">
        <lightning:textarea aura:id="desc" label="Description" name="desc" value="{!v.actionRec.Description__c }" 
                            disabled="{!v.disabled}" onchange="{!c.update}"/>                     
    </aura:if>
        
    <aura:if isTrue="{!and(v.actionRec.Commercial__c == true, v.CurrentUser.Profile.Name == 'System Administrator')}">
        <lightning:textarea aura:id="desc" label="Description" name="desc" value="{!v.actionRec.Description__c }" 
                            disabled="{!v.disabled}" onchange="{!c.update}"/>   
    </aura:if>
      
    <footer class="slds-modal__footer">
        <lightning:button variant="brand"
                          label="Save"
                          title="Save"
                          onclick="{! c.actionSave}" />
    </footer>
    </form>
</aura:component>

then I needed to create a function on the controller.js
update: function(component, event, helper) {
        let currentUserProfileName = component.get('v.CurrentUser.Profile.Name');
        let recordOwnerProfileName = component.get('v.recordOwner.Profile.Name');
        if(currentUserProfileName != recordOwnerProfileName) {
            component.set('v.disabled', component.find('desc'));
        }
    },

and the apex for the recordOwnerProfileName
@AuraEnabled
    public static String getProfileName() {
        Id currentUserProfileId = UserInfo.getProfileId();
        String profileName = [SELECT Name FROM Profile WHERE Id =: currentUserProfileId LIMIT 1].Name;
        System.debug('profileName ' +profileName);
        return profileName;
    }
    
    @AuraEnabled
    public static String getOwnerProfileName(String recordId) {
        Set<Id> OwnerIds = new Set<Id>();
        List<Action__c> actionList = [SELECT OwnerId FROM Action__c WHERE Id =:recordId];
        for(Action__c act : actionList) {
            if(act.OwnerId != null) OwnerIds.add(act.OwnerId);
        }
        Id recOwnerProfileId = UserInfo.getProfileId();
        String ownerProfileName = [SELECT Profile.Name FROM User WHERE Id =:OwnerIds LIMIT 1].Profile.Name;
        System.debug('ownerProfileName ' +ownerProfileName);
        return ownerProfileName;
    }