+ Start a Discussion
Amit_Amit_ 

Trigger to restrict user to insert record in Child record.

Hi,

I am new to salesfroce, so I'm trying to develop a simple piece of code but I'm having some issue with it, any help is trully appricated.

 

here is what I want, I have two objects Class__c  with fileds NumberOfStudents(RollUP field), Name, MaxLimit (Number)and Studebt__c has field FirstName__c, LastName__c , id.  here what I am doing :

* =================================================================================

    Trigger to restrict user to insert records if the max limit of calss has reached
   
   ================================================================================= */

trigger StudentBeforeInsert on Student__c (before insert) {
    set<id>stids= new set<id>();
        for(student__c stu: trigger.new){
            stids.add(stu.id);
        }
    list<student__c> std = new list<student__c>();
    std =[select class__r.name from student__c where id in :stids];
    set<student__c> stSet = new set<student__c>();
        stSet.addAll(std);   
    system.debug('From Data Loader====  :'+ std);
    List<class__c> cls = new list<class__c>();
    cls=[select MaxLimit__c,NumberOfStudents__c from class__c where name =:stSet]; get error at thisline as Invalid bind expression type of SOBJECT:Student__c for column of type String at line 19 column 69

basic idea I have is I want to find the NumberOfStudent already in the class and the maxList of the class and then to compare these two filed for which the user is trying to insert the record in student record. if value in both the field is asme then it will not allow to insert any record, or else it will go ahead with inserting record into the table.

 

Please give me some suggestion, so that I can learn and code

 

Thanks in Advance.

imutsavimutsav

trigger StudentBeforeInsert on Student__c (before insert) {
set<id>stids= new set<id>();
for(student__c stu: trigger.new){
stids.add(stu.id);
}

list<student__c> std = [select class__r.name from student__c where id in :stids];

set<Id> classIds = new Set<ID>(); 

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

 

for(Student__c student: std) {

classIds.add(student.class__c);

classNames.add(student.class__r.Name);

}

 

List<class__c> cls = [select MaxLimit__c,NumberOfStudents__c from class__c where name IN :classNames];

OR

List<class__c> cls = [select MaxLimit__c,NumberOfStudents__c from class__c where Id IN :classIds];

 

When you want to compae the LIST in a query you have to use 'IN' not '='

 

Thanks

Utsav

 

[Do mark this answer as solution and give KUDOS if it helps you relove the problem]

Amit_Amit_

Hi Utsav thanks for your help, I have written the code but still its not working I dont understand, where I am doing the mistake 

this is what I have wrote : 

trigger StudentBeforeInsert on Student__c (before insert) {
 
set<id>stids= new set<id>();
for(student__c stu: trigger.new){
stids.add(stu.id);
}
// List to get the Class Name
list<student__c> std = [select class__r.name,Age__c,DOB__c,Email__c,FirstName__c,Gender__c,HaveVehicle__c,Id,LastName__c,Married__c,Name from student__c where id in :stids];
// created set of ids to avoid duplicate id's
set<Id> classIds = new Set<ID>();

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

for(Student__c student: std) {

classIds.add(student.class__c);

classNames.add(student.class__r.Name);

}
// get the Number of student and MaxSize of class count
List<class__c> cls = [select NumberOfStudents__c,MaxSize__c from class__c where Id IN :classIds];
for(student__c stdcls : trigger.new){
for(class__c c : cls){
decimal NumStd = c.NumberOfStudents__c;
decimal Maxno = c.MaxSize__c;
system.debug('Std Num value: '+NumStd + 'MaxSizeOf class: ' + Maxno);
if(NumStd == Maxno ){
stdcls.addError('Cannot insert Record');
}
}
}
}

Amit_Amit_

Hi Utsav thanks for your help, I have written the code but still its not working I dont understand, where I am doing the mistake 

this is what I have wrote : 

trigger StudentBeforeInsert on Student__c (before insert) {
 
          set<id>stids= new set<id>();
               for(student__c stu: trigger.new){
                      stids.add(stu.id);
              }

           list<student__c> std = [select  class__r.name  from student__c where id in :stids];

          set<Id> classIds = new Set<ID>();

        // List<String> classNames = new List<String>();

         for(Student__c student: std) {

                  classIds.add(student.class__c);

                 // classNames.add(student.class__r.Name);

         }
         List<class__c> cls = [select NumberOfStudents__c,MaxSize__c from class__c where Id IN :classIds];
         for(student__c stdcls : trigger.new){
                     for(class__c c : cls){
                                 decimal NumStd = c.NumberOfStudents__c;
                                 decimal Maxno = c.MaxSize__c;

                                 if(NumStd == Maxno ){
                                            stdcls.addError('Cannot insert Record');
                                 }
                       }
}
}

When I am trying to insert a record In student object for the class in which  numberOfstudent has reached its MaxSize. but still the New student is getting inserted. 

please help

 

Thanks in Advance

Amit 

imutsavimutsav

Try to change this code 

 List<class__c> cls = [select NumberOfStudents__c,MaxSize__c from class__c where Id IN :classIds];
         for(student__c stdcls : trigger.new){
                     for(class__c c : cls){
                                 decimal NumStd = c.NumberOfStudents__c;
                                 decimal Maxno = c.MaxSize__c;

                                 if(NumStd == Maxno ){
                                            stdcls.addError('Cannot insert Record');
                                 }
                       }
}

 

TO

 

List<class__c> cls = [select NumberOfStudents__c,MaxSize__c from class__c where Id IN :classIds];
Map<Id,class__c> classMap = new Map<Id,class__c>();

for(class__c c : cls){
     if(!classMap.containsKey(c.Id)) {
          classMap.put(c.Id,c); 
     }
}

for(student__c stdcls : trigger.new){
     if(classMap.get(stdls.class__c).NumberOdStudents__c<classMap.get(stdls.class__c).MaxSize__c) {
          classMap.get(stdls.class__c).NumberOdStudents__c++;
     } else {
          stdcls.add('Cannot insert Record - Class is full');
     }
}

update classMap.values(); //you need to update the class to with updated number of students.

 

 

Thanks

Utsav

 

[Do mark this answer as solutiona and give KUDOS if it helps]

 

Amit_Amit_

Hi Utsav,

thanks for your suggestion , your code did worked for me . And after analysing it I have simplified the code  and it too is working. I found out where I was doing the mistake :)

here what i have done :

/* =================================================================================

    Trigger to restrict user to insert records if the max limit of calss has reached
   
   ================================================================================= */

trigger StudentBeforeInsert on Student__c (before insert) {
   
            set<Id> classIds = new Set<ID>();
            for(Student__c student : trigger.new) {

                 classIds.add(student.class__c);
            }
     /*   Method 1  start ******************************* */
           Map<Id,class__c> classMap = new Map<Id,class__c>();

             for(class__c c : cls){
                 if(!classMap.containsKey(c.Id)) {
                      classMap.put(c.Id,c);
                 }
             }
           for(student__c stdcls : trigger.new){
             if(classMap.get(stdcls.class__c).NumberOfStudents__c == classMap.get(stdcls.class__c).MaxSize__c) {
                     stdcls.addError('Cannot insert Record - Class is full');
             }
         } 
          /*   Method 1  End  ******************************* */

         
         /*   Method 2  start ******************************* */

         List<class__c> cls = [select NumberOfStudents__c,MaxSize__c from class__c where Id IN :classIds];
         system.debug('strength of Class' + cls);
         for(student__c stdcls : trigger.new){
                     for(class__c c : cls){
                                 if(c.NumberOfStudents__c == c.MaxSize__c){
                               
                                                     stdcls.addError('Cannot insert Record');
                                  }
                       }
          }
}

 

Thanks Utsav for yout valuable time and suggestions

 

Regards,

Amit

imutsavimutsav

Welcome Amit; One more thing you need to write an update on the class object, because you need to update its value too for the future records. Suppose there are 3 seats left in a class and your list contains 5 students to insert. With your code you will allow all of them which is incorrect however only 3 students should be allowed and the rest 2 should be denied. Also you have written a for loop inside a for loop which is not a good design for performance. Anyways its your use case. However please mark my answer as solution so that it would help others to understand and please do give KUDOS by clicking on the star if my efforts helped you.

Thanks
Utsav

Amit_Amit_
Thanks Utsav for a valuable suggestion. I'll modify my code and try not to use for loop inside another for loop. And will try to make more real based senarios.

Thanks & Regards,
Amit