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
Tyler Morgan 9Tyler Morgan 9 

Lightning component - Display Unique Opportunity field values for account

I am trying to build a basic component which displays the unique values of a field from all opportunities under an open account.

I'm getting the following error: Illegal conversion from List to List

Any help is greatly appreciated. Here's the code:

Class:

public class testclass {
    @AuraEnabled
    public static List<Opportunity> fetchOpp(String key) {
        List<AggregateResult> listOfOpps = [SELECT Consulting_Engineer__c, COUNT(ID) FROM Opportunity WHERE AccountId =:key GROUP BY Consulting_Engineer__c];
        
        return listOfOpps;
    }
}
 



Component:

<aura:component controller= "testclass" implements="flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId" access="global" >
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <aura:attribute name="Opportunities" type="Opportunity[]"/>
    <ul>
        <aura:iteration items="{!v.Opportunities}" var="opportunity">
            <li>{!opportunity.Consulting_Engineer__c}</li>

        </aura:iteration>
    </ul>
</aura:component>

Controller:

({
    doInit: function(component, event, helper) {
        var action = component.get('c.fetchOpp');
        var rid = component.get("v.recordId");
        action.setParams({key : rid});
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                component.set('v.Opportunities', response.getReturnValue());
            }
        });
        $A.enqueueAction(action);
    }
})
Best Answer chosen by Tyler Morgan 9
Abhishek BansalAbhishek Bansal
Hi Tyler, 

Please find the updated code below:

Component:
<aura:component controller= "testclass" implements="flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId" access="global" >
	<aura:attribute name="recordId" type="String" />
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <aura:attribute name="listOfConsultingEnigeer" type="List<String>"/>
    <ul>
        <aura:iteration items="{!v.listOfConsultingEnigeer}" var="consultingEngineer">
            <li>{!consultingEngineer}</li>

        </aura:iteration>
    </ul>
</aura:component>
Controller:
({
    doInit: function(component, event, helper) {
        var action = component.get('c.fetchOpp');
        var rid = component.get("v.recordId");
        action.setParams({key : rid});
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                component.set('v.listOfConsultingEnigeer', response.getReturnValue());
            }
        });
        $A.enqueueAction(action);
    }
})

Apex Class:
public class testclass {
    @AuraEnabled
    public static List<String> fetchOpp(String key) {
		List<String> conEngList = new List<String>();
		Set<String> conEngSet = new Set<String>();
		for(Opportunity opp : [SELECT Consulting_Engineer__c FROM Opportunity WHERE AccountId =:key AND Consulting_Engineer__c != null]) {
			conEngSet.add(opp.Consulting_Engineer__c);
		}
        conEngList.addAll(conEngSet);
        
        return conEngList;
    }
}
Please let me know if there is any issue with this.

Thanks,
Abhishek Bansal.

All Answers

Abhishek BansalAbhishek Bansal
Hi Tyler, 

Please find the updated code below:

Component:
<aura:component controller= "testclass" implements="flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId" access="global" >
	<aura:attribute name="recordId" type="String" />
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <aura:attribute name="listOfConsultingEnigeer" type="List<String>"/>
    <ul>
        <aura:iteration items="{!v.listOfConsultingEnigeer}" var="consultingEngineer">
            <li>{!consultingEngineer}</li>

        </aura:iteration>
    </ul>
</aura:component>
Controller:
({
    doInit: function(component, event, helper) {
        var action = component.get('c.fetchOpp');
        var rid = component.get("v.recordId");
        action.setParams({key : rid});
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                component.set('v.listOfConsultingEnigeer', response.getReturnValue());
            }
        });
        $A.enqueueAction(action);
    }
})

Apex Class:
public class testclass {
    @AuraEnabled
    public static List<String> fetchOpp(String key) {
		List<String> conEngList = new List<String>();
		Set<String> conEngSet = new Set<String>();
		for(Opportunity opp : [SELECT Consulting_Engineer__c FROM Opportunity WHERE AccountId =:key AND Consulting_Engineer__c != null]) {
			conEngSet.add(opp.Consulting_Engineer__c);
		}
        conEngList.addAll(conEngSet);
        
        return conEngList;
    }
}
Please let me know if there is any issue with this.

Thanks,
Abhishek Bansal.
This was selected as the best answer
Tyler Morgan 9Tyler Morgan 9

Thanks again Abhishek.

I only had to change to following line:  <aura:attribute name="listOfConsultingEnigeer" type="List<String>"/>. It wouldn't accept List<String> so I changed it to just String.

Everything is working except there is a single value that isn't showing. Might it be because there is a '/' in the string?

Abhishek BansalAbhishek Bansal

Hi Tyler,

Having a slash in the string is not an issue. Please change the attribute type from string to just List.
Change List<String> -> List
I hope that will resolve the issue.

Thanks,
Abhishek Bansal