+ Start a Discussion
Sindhu AmbarkarSindhu Ambarkar 

Trigger to restrict child record

Hi,

I have to restrict the number of child records on master record upto 5
Job application is master,review is child.
Number of review is roll up field on master object.

trigger Saivenkat on review__c (before insert) {
    List<id> parent=new List<id>();
    List<job_application__c> a=new List<job_application__c>(); 
    for(review__c ar:Trigger.new)
    {
        parent.add(ar.job_application__c);
    }
   a=[select Name,number_of_review__c from job_application__c where Name in:parent ];
    for (job_application__c vara:a)
    {
      if(vara.number_of_review__c>=5)
      {
            Apexpages.addMessage(new Apexpages.message(Apexpages.Severity.ERROR,'it is not filter match'));
        }
    }
}
Please help me
Thanks & Regards,
Sindhu Ambarkar.
Best Answer chosen by Sindhu Ambarkar
Amit Chaudhary 8Amit Chaudhary 8
Please try below code. I hope that will help you
trigger Saivenkat on review__c (before insert) 
{
    Set<id> SetParent=new Set<id>(); // Try to use set to remove duplicate
    Map<Id, job_application__c> a=new Map<Id,job_application__c>(); 
    for(review__c ar:Trigger.new)
    {
        SetParent.add(ar.job_application__c); // I hope this is a lookup field 
    }
	
	Map<Id, job_application__c> MapJP =new Map<Id,job_application__c>([select Name,number_of_review__c from job_application__c where id in:SetParent ]); // IF that us a look field then filter should be id

    for (review__c ar : Trigger.new )
    {
		if(MapJP.containsKey(ar.Job_application__c))
		{
			if(MapJP.get(ar.Job_application__c).number_of_review__c >=5)
			{
				ar.addError('it is not filter match');
			}
		}	
    }
}

SetParent.add(ar.job_application__c);
I hope this is a lookup field if yes the query should be like below :-


Map<Id, job_application__c> MapJP =new Map<Id,job_application__c>([select Name,number_of_review__c from job_application__c where id in:SetParent ]);

let us know if this will help u

All Answers

Pankaj_GanwaniPankaj_Ganwani
Hi,

You will have to use addError method for showing error to user instead of using apexpages messages.

trigger Saivenkat on review__c (before insert) {
    List<id> parent=new List<id>();
    Map<Id, job_application__c> a=new Map<Id,job_application__c>(); 
    for(review__c ar:Trigger.new)
    {
        parent.add(ar.job_application__c);
    }
 Map<Id, job_application__c> mapIdToJA=new Map<Id,job_application__c>([select Name,number_of_review__c from job_application__c where Name in:parent ]);
    for (review__c ar:Trigger.new)
    {
      if(mapIdToJA.get(ar.Job_application__c).number_of_review__c >=5)
      {
            ar.addError('message');
        }
    }
}
Nagendra Prasad ChNagendra Prasad Ch
You can use validation rule on the master object to restrict. If Number of reviews >5, throw an error by using validation rule on master object. It stops adding more than 5 child records. It triggers while child records insert.
Sindhu AmbarkarSindhu Ambarkar
Hi,

I tried with the code.

trigger Saivenkat on review__c (before insert) {
    List<id> parent=new List<id>();
    List<job_application__c> a=new List<job_application__c>(); 
    for(review__c ar:Trigger.new)
    {
        parent.add(ar.job_application__c);
    }
   a=[select Name,number_of_review__c from job_application__c where Name in:parent ];
    for (job_application__c vara:a)
    {
      if(vara.number_of_review__c>=5)
      {
            vara.addError('match');
        }
    }
}
It is not working fine.

Thanks & Regards,
Sindhu Ambarkar.
Amit Chaudhary 8Amit Chaudhary 8
Please try below code. I hope that will help you
trigger Saivenkat on review__c (before insert) 
{
    Set<id> SetParent=new Set<id>(); // Try to use set to remove duplicate
    Map<Id, job_application__c> a=new Map<Id,job_application__c>(); 
    for(review__c ar:Trigger.new)
    {
        SetParent.add(ar.job_application__c); // I hope this is a lookup field 
    }
	
	Map<Id, job_application__c> MapJP =new Map<Id,job_application__c>([select Name,number_of_review__c from job_application__c where id in:SetParent ]); // IF that us a look field then filter should be id

    for (review__c ar : Trigger.new )
    {
		if(MapJP.containsKey(ar.Job_application__c))
		{
			if(MapJP.get(ar.Job_application__c).number_of_review__c >=5)
			{
				ar.addError('it is not filter match');
			}
		}	
    }
}

SetParent.add(ar.job_application__c);
I hope this is a lookup field if yes the query should be like below :-


Map<Id, job_application__c> MapJP =new Map<Id,job_application__c>([select Name,number_of_review__c from job_application__c where id in:SetParent ]);

let us know if this will help u
This was selected as the best answer
ShivaKrishna(Freelancer)ShivaKrishna(Freelancer)
Hi Sindhu,

You are trying to controll the flow depends on the roll up summary field which would be calculated after the record got saved so you need to keep complete logic insdie the the event "after insert'

so your code should be some thing like this

trigger Saivenkat on review__c (after insert) {
//your code
}

let me know, if it helps you or need any help :)
shiva.sfdc.backup@gmail.com
Sindhu AmbarkarSindhu Ambarkar
Thanks Amit.It is working fine.Can you explain me the difference between apexmessages and adderror.I want to know that why my code did not work.

Thanks & Regards,
Sindhu Ambarkar.
Amit Chaudhary 8Amit Chaudhary 8
Major problem in your code was your SOQL .

You was using the below query :-
a=[select Name,number_of_review__c from job_application__c where Name in:parent ]; // In which you was comparing the Name with ID

apexmessages is used for Visual force page
adderror for Trigger

I hope that will help you
Sindhu AmbarkarSindhu Ambarkar
Thanks a lot Amit