+ Start a Discussion
ethanoneethanone 

LWC/Apex returning user list with parameters

I'm trying to display the list of acitve users in the same branch office as the logged in user in a LWC.
I have an apex class that looks like this:
@AuraEnabled(cacheable=true)
    public static List<User> getMktUsers(String usrId) {
        User ThisUser = [SELECT Id, Name, Office__c FROM User WHERE Id = :usrId LIMIT 1];
        return [SELECT Id, Name FROM User WHERE Office__c = :ThisUser.Office__c and IsActive = True];
    }
I have refer to it in my LWC js file like this:
import { LightningElement, api, track, wire } from "lwc";
import getMktUsers from "@salesforce/apex/Top100.getMktUsers";
import Id from "@salesforce/user/Id";

export default class Top100 extends LightningElement {
  @wire(getMktUsers, { usrId: { Id } }) wiredUsers;
}
I call it in my html file like this:
<ul class="slds-list_ordered">
            <template for:each={wiredUsers.data} for:item="wuser">
                <li key={wuser.Id}>
                    {wuser.Name}
                </li>
            </template>
        </ul>
I'm able to get lists of users to appear if I hard code the office name, but when I try to pass in the Id to determie the current user's office, i get nothing appearing. I do not know how to debug this. Can anyone help?
 
Team  CodEngine.inTeam CodEngine.in
Debug in the apex code using system.debug
1. check if you are getting the id in the apex code
2. check if the query is returning values to client-side

If it is working with hardcoded value, then it will be an issue at Apex side

- Naveen K N 
ethanoneethanone
For #1, how do I view the debug log when loading a LWC in a lightning record page? Do I need to write a test class? I'm only familiar with VF testing.

For #2, I can't seem to get the return value out in a console.log statement - i mean, i can't find it in the logs, but no deployment or runtime errors. using alert jsut gives me [object object], so how do I view the user list value in the browser?
Team  CodEngine.inTeam CodEngine.in
#1 
    public static List<User> getMktUsers(String usrId) {
         System.debug('Id is ' + usrId);
}

check the value of this one in the developer console > logs section. In case if you are getting id everything is fine from the js code. 

#2

again the same, check the value of this query return [SELECT Id, Name FROM User WHERE Office__c = :ThisUser.Office__c and IsActive =True];
in the system.debug and see if you are returning any records. 

In case both of them are fine in these two, you need to troubleshoot at the client side. 

- Naveen K N
ethanoneethanone
I did not know i could see System.debug messages in the dev console log, So, thank you for that. I'm able to get debug messages back on another function, but I'm unable to get any debug messages back from the getMktUsers function.  It appears to bomb out right after [ETERNAL]Aura line.But there are no errors. just CODE_UNIT_FINISHED and EXECUTION_FINISHED. I think it is getting to the function, but I can't be sure because there is no evidence. I can display the Id correctly on the compoenet, so I'm sure the Id variable contains the user id, but I'm not able to get to the debug statement. Below is the full class. getUserList works perfectly, but getMktUsers does not.
public with sharing class Top100 {
    @AuraEnabled(cacheable=true)
    public static List<User> getMktUsers(String usrId) {
        System.debug('theuserid:'+usrId);
        User ThisUser = [SELECT Id, Name, Office__c FROM User WHERE Id = :usrId LIMIT 1];
        System.debug('TheUserOfficeis: ' + ThisUser.Office__c);
        return [SELECT Id, Name FROM User WHERE Office__c = :ThisUser.Office__c and IsActive = True];
    }

    @AuraEnabled(cacheable=true)
    public static List<User> getUserList(String TheOffice) {
        System.debug('TheOfficeIs:' + TheOffice);
        return [SELECT Id, Name FROM User WHERE Office__c = :TheOffice and IsActive = True LIMIT 5];
    }
}

 
ethanoneethanone
I figured this out. The issue is how to pass the variable to Apex. Need the @api on the variable and need to reference variable with dollar sign surrounded by quotes.
@api userId;
userId = Id;
@wire(getMktUsers, { usrId: "$userId" }) MktUsers;
Team  CodEngine.inTeam CodEngine.in
Thanks for updating this. 

- Naveen K N