+ Start a Discussion
Agnibha Chakrabarti 10Agnibha Chakrabarti 10 

Trigger in junction object

hi,
my requirement is

 After the batch is over, Teacher will mark the batch as complete. Then the course will be moved from Requested Courses to Completed Coursed for all the students of the batch. Before marking a batch as complete, the teacher can remove any student from the batch. This will not be possible after the batch is completed.
Relationship:
User-added imageim trying to access the students whose batch is completed(batch has a checkbox mark as complete or not)
trigger Batch on Batch__c (before insert,after update) {
    List<String> b= new List<String>();  
     for(Batch__c p: Trigger.new){
         b.add(p.Name);
     }
    List<Student__c> c=new List<Student__c>([select Student__r.Name from Student_Batch_Junction__c where Batch__r.Name in :b]);
    
    
}

I tried to access student in this way..it is showing error
Best Answer chosen by Agnibha Chakrabarti 10
Deepali KulshresthaDeepali Kulshrestha
Hi Agnibha,

Please check below code:
trigger Batch on Batch__c (before insert,after update) {
    List<String> b= new List<String>();  
     for(Batch__c p: Trigger.new){
         b.add(p.Name);
     }
    List<Student_Batch_Junction__c > c=new List<Student_Batch_Junction__c>([select Student__r.Name from Student_Batch_Junction__c where Batch__r.Name in :b]);   
}

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

Krishna.AvvaKrishna.Avva
Can you please post the error?
Agnibha Chakrabarti 10Agnibha Chakrabarti 10
The error was....invalid assignment:list should be of integer type or Student__c type ..
Deepali KulshresthaDeepali Kulshrestha
Hi Agnibha,

Please check below code:
trigger Batch on Batch__c (before insert,after update) {
    List<String> b= new List<String>();  
     for(Batch__c p: Trigger.new){
         b.add(p.Name);
     }
    List<Student_Batch_Junction__c > c=new List<Student_Batch_Junction__c>([select Student__r.Name from Student_Batch_Junction__c where Batch__r.Name in :b]);   
}

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
Agnibha Chakrabarti 10Agnibha Chakrabarti 10
Thanks Deepali for the help......can you help me with later part of the requirement?
Agnibha Chakrabarti 10Agnibha Chakrabarti 10
trigger Batch on Batch__c (before insert,after update) {

    List<String> b= new List<String>();  

     for(Batch__c p: Trigger.new){

         b.add(p.Name);

     }

    List<Student_Batch_Junction__c > c=new List<Student_Batch_Junction__c>([select Student__r.Name from Student_Batch_Junction__c where Batch__r.Name in :b]);   
    system.debug(c);
    List<Student__c> s=new List<Student__c>([select Student__c.Name from Student__c]);
    
    for(Student__c o : s)
    {     if(o.Name==c)
    {
        
        	o.Completed_Courses__c=o.Requested_Courses__c;
    }
        
    }
}

Error is: Comparison arguments must be compatible types: String, List<Student_Batch_Junction__c>
Agnibha Chakrabarti 10Agnibha Chakrabarti 10
trigger Batch on Batch__c (after update) {

    List<String> b= new List<String>();  

     for(Batch__c p: Trigger.new){

         b.add(p.Name);

     }

    List<Student_Batch_Junction__c > c=new List<Student_Batch_Junction__c>([select Student__r.Name from Student_Batch_Junction__c where Batch__r.Name in :b]);   
    system.debug(c);
    List<Student__c> s=new List<Student__c>([select Student__c.Name from Student__c]);
    integer i=0;
    for(Student__c o : s)
    {     if(o.Name.equals(c))
    {
        
        	o.Completed_Courses__c=o.Requested_Courses__c;
           update o;
    }
        
    }

This trigger has no error.........but it is not working
​​​​​​​