You need to sign in to do that
Don't have an account?
Sindhu 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.
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.
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
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');
}
}
}
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.
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
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
Thanks & Regards,
Sindhu Ambarkar.
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