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
Сергей Жугин 7Сергей Жугин 7 

Data output to the page by clicking on the button

Hi everyone, I'm started to learn Lightning and recently faced one problem. I want to create a trivial application. My application has one picklist and one button. I select records in the picklist and output it with the button. But it does not work. 
Please help me solve this problem.
Component:
<aura:component controller="MyObjController" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global">
    <aura:handler name="init" value="{!this}" action="{!c.fetchListOfRecordTypes}"/>
    <aura:attribute name="lstOfRecordType" type="String[]"/>
	<aura:attribute name="atrOgj" type="Sensor__c"/>
    <aura:attribute name="isOpen" type="boolean" default="false"/>
    <div class="slds-modal__container">
    	<div class="slds-modal__content slds-p-around--medium">
                    <div class="slds-grid slds-wrap">
                        <div class="slds-size--1-of-2 slds-large-size--1-of-2">
                             <div class="slds-align--absolute-center">Select a Record Type</div>                            
                        </div>
                        <div class="slds-size--1-of-2 slds-large-size--1-of-2">
                            <ui:inputSelect aura:id="selectid">
                                <aura:iteration items="{!v.lstOfRecordType}" var="contact">                            
                                    <ui:inputSelectOption text="{!contact}" label="{!contact}"  />
                                </aura:iteration>
                            </ui:inputSelect>
                        </div>&nbsp; &nbsp;
                    </div>                   
        </div>
        <div class="slds-modal__footer">
        	<lightning:button class="slds-button slds-button--brand" onclick="{!c.createRecord}">Next</lightning:button>
        </div>
        <aura:if isTrue="{!v.isOpen}">
              
					{!atrOgj.Name}
        </aura:if>
        
       
    </div>
</aura:component>
Controller:
({
    fetchListOfRecordTypes: function(component, event, helper) {
      var action = component.get("c.fetchRecordTypeValues");
      action.setCallback(this, function(response) {
         component.set("v.lstOfRecordType", response.getReturnValue());
      });
      $A.enqueueAction(action);
   },
    
    
    createRecord: function(component, event, helper) { 
      
      var action1 = component.get("c.getRecTypeId");
      var recordTypeLabel = component.find("selectid").get("v.value");
      action1.setParams({
         "recordTypeLabel": recordTypeLabel
      });
        alert(recordTypeLabel);
      action1.setCallback(this, function(response) {
         	var state = response.getState();
          if (state === "SUCCESS")  {
             component.set('v.atrOgj', response.getReturnValue());   
             component.set("v.isOpen", true);
          }
          else {alert('Error');}
      });
      $A.enqueueAction(action1);
   }
})
Apex class:
public with sharing class MyObjController {
    public static Map<Id, String> recordtypemap {get;set;}
    
   @AuraEnabled        
    public static List<String> fetchRecordTypeValues(){
       	List<Sensor__c> recordtypes = [SELECT ID,Name FROM Sensor__c];    
        recordtypemap = new Map<Id, String>();
        for(Sensor__c rt : recordtypes){
            recordtypemap.put(rt.Id, rt.Name);
        }
        system.debug(recordtypemap.values());
        return recordtypemap.values();
    }  
        @AuraEnabled
    public static Sensor__c getRecTypeId(String recordTypeLabel){
        return [SELECT Max_Vectors_Difference__c FROM Sensor__c WHERE Name =:recordTypeLabel Limit 1];
    }
}



 
Best Answer chosen by Сергей Жугин 7
Alain CabonAlain Cabon
You must also get the Name or/and use Max_Vectors_Difference__c
 
@AuraEnabled
public static Sensor__c getRecTypeId(String recordTypeLabel){
        return [SELECT Name,Max_Vectors_Difference__c FROM Sensor__c WHERE Name =:recordTypeLabel Limit 1];
}
if (state === "SUCCESS")  {
        component.set('v.atrOgj', response.getReturnValue());   
        component.set("v.isOpen", true);
}
<aura:if isTrue="{!v.isOpen}">
     Difference: {!v.atrOgj.Max_Vectors_Difference__c}  Name: {!v.atrOgj.Name}
 </aura:if>

All Answers

Alain CabonAlain Cabon
@Сергей Жугин 7

Minor error:  {!  v.atrOgj.Name}
<aura:if isTrue="{!v.isOpen}">             
	{! v.atrOgj.Name }
</aura:if
Сергей Жугин 7Сергей Жугин 7
Sorry, it doesn't help. Maybe my code has other mistakes.
Alain CabonAlain Cabon
You must also get the Name or/and use Max_Vectors_Difference__c
 
@AuraEnabled
public static Sensor__c getRecTypeId(String recordTypeLabel){
        return [SELECT Name,Max_Vectors_Difference__c FROM Sensor__c WHERE Name =:recordTypeLabel Limit 1];
}
if (state === "SUCCESS")  {
        component.set('v.atrOgj', response.getReturnValue());   
        component.set("v.isOpen", true);
}
<aura:if isTrue="{!v.isOpen}">
     Difference: {!v.atrOgj.Max_Vectors_Difference__c}  Name: {!v.atrOgj.Name}
 </aura:if>
This was selected as the best answer
Сергей Жугин 7Сергей Жугин 7
Thank you, it's realy cool. But I have the last question: i want to output filds of object Sensor_Event__c which connect master detail with object
Sensor__c. How can I do it? Sorry for my english. Is my relationship:
User-added image
 
Alain CabonAlain Cabon
Hello,
 
@AuraEnabled
public static Sensor__c getRecTypeId(String recordTypeLabel){
        return [SELECT Name,Max_Vectors_Difference__c, (select Name from Sensor_Events__r ) FROM Sensor__c WHERE Name =:recordTypeLabel Limit 1];
}
 
if (state === "SUCCESS")  {
        console.log('response:' + JSON.stringify(response.getReturnValue()));
        component.set('v.atrOgj', response.getReturnValue());   
        component.set("v.isOpen", true);
}

Could you show me the message for "response:" that you get in the Chrome console (developer tools ) ?

To open the dedicated Console panel, either:
  • Press Ctrl+Shift+J (Windows / Linux) or Cmd+Opt+J (Mac).
  • If DevTools is already open, press the Console button.

https://developers.google.com/web/tools/chrome-devtools/console/
 
Alain CabonAlain Cabon
Copy/paste modified:

@AuraEnabled
public static Sensor__c getRecTypeId(String recordTypeLabel){
            return [SELECT Name,Max_Vectors_Difference__c, (select Name from Sensor_Events__r )
                       FROM Sensor__c
                       WHERE Name =:recordTypeLabel Limit 1];
}
Сергей Жугин 7Сергей Жугин 7
It's return only Id Sensor_Event record
User-added image
Alain CabonAlain Cabon
It is not the "Network" tab but the "Console" tab that is interesting here.

There is another "Console" tab at the bottom of your snapshot indeed but there is also a console at the left of the "Network" at the top.
Сергей Жугин 7Сергей Жугин 7
You wanted to see it?
User-added image
Сергей Жугин 7Сергей Жугин 7
User-added image
Alain CabonAlain Cabon
 console.log('response:' + JSON.stringify(response.getReturnValue())); 

I don't see "response:" so I cannot help you, sorry.

> response: { .... content of the response ... }
 
Сергей Жугин 7Сергей Жугин 7
User-added image
Alain CabonAlain Cabon
Ok this snapshot is correct and you can also just do a "right click" with the mouse and copy/paste only the text.

You can see the exact content of the response and this Id is always present by default so there is no data for (select Name from Sensor_Events__r ) here.

The relationship Sensor_Events__r is correct because your apex code is compiled and saved but there is no related data for this selected record in  Sensor__c currently.