+ Start a Discussion
Agnibha Chakrabarti 10Agnibha Chakrabarti 10 

how to get value of one field from list of SObject

in my code
List<Student_Batch_Junction__c > c=new List<Student_Batch_Junction__c>([select Student__r.id from Student_Batch_Junction__c where Batch__r.Name in :b]);
variable c contains:
11:55:42:011 VARIABLE_ASSIGNMENT [11]|this.c|[{"Student__c":"a022v00001GmqnrAAB","Batch__c":"a042v00002CobkCAAR","Id":"a052v00000l4WxtAAE","Student__r":{"Id":"a022v00001GmqnrAAB"}},{"Student__c":"a022v00001GnNHiAAN","Batch__c":"a042v00002CobkCAAR","Id":"a052v00000l4c5dAAA","Student__r":{"Id":"a022v00001GnNHiAAN"}}]

I want to get only Student__r id field values.
how to get that?

 
Best Answer chosen by Agnibha Chakrabarti 10
Deepali KulshresthaDeepali Kulshrestha
Hi Agnibha,


Please check below code:
public class test {
    
    public static void queryTest()
    {
        List<Batch__C> b= [Select Id,Name from Batch__C];
        System.debug(b);
        
        Set<Id> batchIds=new Set<Id>();
        for(Course__c bs: b )
        {
            batchIds.add(bs.Id);
        }
        List<Student_Batch_Junction__c> c=new List<Student_Batch_Junction__c>([select Student__r.id from Student_Batch_Junction__c where Batch__r.Id in : batchIds]);
        System.debug(c);
        
        Set<Id> studIds=new Set<Id>();
        for(Student_Batch_Junction__c sid:c)
        {
            studIds.add(sid.Student__r.Id);
        }
        System.debug(studIds);
    }
}

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

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com

All Answers

tell subwaytell subway
follow this procedure for same.
sObject acc = [SELECT Owner.Profile.Name FROM Account LIMIT 1]; String profileName = (String) acc.getSobject('tellsubway (https://tellsubway.world/)').getSobject('Profile').get('Name'); System.debug(profileName);
i think you will get your student id field value. 
Agnibha Chakrabarti 10Agnibha Chakrabarti 10
your example is for one sObject.......but want to get field record from list of sObject....... im having the problem of variable types
Deepali KulshresthaDeepali Kulshrestha
Hi Agnibha,


Please check below code:
public class test {
    
    public static void queryTest()
    {
        List<Batch__C> b= [Select Id,Name from Batch__C];
        System.debug(b);
        
        Set<Id> batchIds=new Set<Id>();
        for(Course__c bs: b )
        {
            batchIds.add(bs.Id);
        }
        List<Student_Batch_Junction__c> c=new List<Student_Batch_Junction__c>([select Student__r.id from Student_Batch_Junction__c where Batch__r.Id in : batchIds]);
        System.debug(c);
        
        Set<Id> studIds=new Set<Id>();
        for(Student_Batch_Junction__c sid:c)
        {
            studIds.add(sid.Student__r.Id);
        }
        System.debug(studIds);
    }
}

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

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
This was selected as the best answer