Skip to main content
deleted 7 characters in body
Source Link
Vamsi Krishna Gosu
  • 10.5k
  • 5
  • 33
  • 50
        trigger StudentInsert on Student__c(before insert) {
       set<ID> classIds=new set<ID>();
    for(Student__c s:trigger.new){
     classIds.add(s.Class__c);
       }
           List<class__c> cls = [select NumberOfStudents__c,MaxSize__c,Class_Id__c from class__c where Class_Id__c IN :classIds];
           Map<Id,Class__c> classMap = new Map<Id,Class__c>();
         for(class__c c :cls){
             if(!classMap.containsKey(c.Class_Id__c)) {
                  classMap.put(c.Class_Id__c,c);
             }
         }
        for(student__c s1 : trigger.new){
         if(classMap.get(s1.class__c).NumberOfStudents__c > classMap.get(s1.class__c).MaxSize__c) {
                 s1.addError('Cannot insert Record - Class is full');
           }
     } 
    update classMap.values();
  }

my student max size is 5,whuile am adding 5 th record in class am getting error like" StudentInsert: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.StudentInsert: line 14, column 1 "like

StudentInsert: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.StudentInsert: line 14, column 1

please help to resolve

        trigger StudentInsert on Student__c(before insert) {
       set<ID> classIds=new set<ID>();
    for(Student__c s:trigger.new){
     classIds.add(s.Class__c);
       }
           List<class__c> cls = [select NumberOfStudents__c,MaxSize__c,Class_Id__c from class__c where Class_Id__c IN :classIds];
           Map<Id,Class__c> classMap = new Map<Id,Class__c>();
         for(class__c c :cls){
             if(!classMap.containsKey(c.Class_Id__c)) {
                  classMap.put(c.Class_Id__c,c);
             }
         }
        for(student__c s1 : trigger.new){
         if(classMap.get(s1.class__c).NumberOfStudents__c > classMap.get(s1.class__c).MaxSize__c) {
                 s1.addError('Cannot insert Record - Class is full');
           }
     } 
    update classMap.values();
  }

my student max size is 5,whuile am adding 5 th record in class am getting error like" StudentInsert: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.StudentInsert: line 14, column 1 "

please help to resolve

  trigger StudentInsert on Student__c(before insert) {
       set<ID> classIds=new set<ID>();
    for(Student__c s:trigger.new){
     classIds.add(s.Class__c);
       }
           List<class__c> cls = [select NumberOfStudents__c,MaxSize__c,Class_Id__c from class__c where Class_Id__c IN :classIds];
           Map<Id,Class__c> classMap = new Map<Id,Class__c>();
         for(class__c c :cls){
             if(!classMap.containsKey(c.Class_Id__c)) {
                  classMap.put(c.Class_Id__c,c);
             }
         }
        for(student__c s1 : trigger.new){
         if(classMap.get(s1.class__c).NumberOfStudents__c > classMap.get(s1.class__c).MaxSize__c) {
                 s1.addError('Cannot insert Record - Class is full');
           }
     } 
    update classMap.values();
  }

my student max size is 5,whuile am adding 5 th record in class am getting error like

StudentInsert: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.StudentInsert: line 14, column 1

please help to resolve

Source Link

Trigger to restrict user to insert record in Child record

        trigger StudentInsert on Student__c(before insert) {
       set<ID> classIds=new set<ID>();
    for(Student__c s:trigger.new){
     classIds.add(s.Class__c);
       }
           List<class__c> cls = [select NumberOfStudents__c,MaxSize__c,Class_Id__c from class__c where Class_Id__c IN :classIds];
           Map<Id,Class__c> classMap = new Map<Id,Class__c>();
         for(class__c c :cls){
             if(!classMap.containsKey(c.Class_Id__c)) {
                  classMap.put(c.Class_Id__c,c);
             }
         }
        for(student__c s1 : trigger.new){
         if(classMap.get(s1.class__c).NumberOfStudents__c > classMap.get(s1.class__c).MaxSize__c) {
                 s1.addError('Cannot insert Record - Class is full');
           }
     } 
    update classMap.values();
  }

my student max size is 5,whuile am adding 5 th record in class am getting error like" StudentInsert: execution of BeforeInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.StudentInsert: line 14, column 1 "

please help to resolve