+ Start a Discussion
nikki goudanikki gouda 

fetch user details

How to fetch user details with user "(username,email)and opportunity(id,stagename) object fields based on username by using JSON 
Suraj Tripathi 47Suraj Tripathi 47

Hi Nikki,

So basically your question is a bit uncleared but still, I managed to solve it. So basically, I could think of two scenarios from your question.

 

1. I'm assuming that your are passing Username as params, if that is the case then use this:
 

public class FetchOppoUse{
    
    
    public static void getData(String uName){
        
        List<User> userDetail = [SELECT Id, Username, Email FROM User WHERE Username = :uName];
        set<Id>UserID = new set<Id>();
            
            for(user each : userDetail){
            UserID.add(each.Id);
        }
        
        List<opportunity> getAllData = [SELECT Id, StageName, CreatedById FROM Opportunity Where CreatedById IN :UserID];
        
        system.debug('userDetail : '+ userDetail);
        system.debug('getAllData : '+getAllData);
    }
    

}

2. If you are fetching all the data from the DB and you want them to return in JSON format, then you can try this:
public class FetchOppoUse{
    
    
    public static void getData(){
        
        List<User> userDetail = [SELECT Id, Username, Email FROM User];
        set<Id>UserID = new set<Id>();
            
            for(user each : userDetail){
            UserID.add(each.Id);
        }
        
        List<opportunity> getAllData = [SELECT Id, StageName, CreatedById FROM Opportunity Where CreatedById IN :UserID];
        
        system.debug('userDetail : '+ userDetail);
        system.debug('getAllData : '+getAllData);

    }
    

}


If you still feel like this Is not what you want then feel free to get back. I'll be happy to assist.

 

I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.
Thanks and Regards,
Suraj

nikki goudanikki gouda
Fetch the user details:
let us assume that our requirements

class: details
method:getdata
input:username
method shold be in JSON format array to fetch the user and opportunity information
if no records then return null
user object fields:username,email
opportinity fields: stagename,id