You need to sign in to do that
Don't have an account?
can we restrict to create dupliacte record of a child object for any particular master record??
suppose i have created two object as Job and Job Task..
their is a master detail relationship between job(master) and job task (detail)...
and i want that their should not be a repeated value for a job task (for a particular job)..
if i am creating a record for job as Painting and size is the job task...(then there should not be any more reord(Duplicate) named as size for painting job)...
and if i am creating any other record of job named as Finishing,here i should allow to create size for this job too...
how can i control...
i have taken job task name as Text (Record Name)...
i have written a trigger but there i am not able to create any duplicate value for Job Task.
here is my code... is there any need to make modification in my trigger...?
trigger duplicateJobTasks on Job_Task__c (before insert,before update) { List<Job_Task__c> lstGC = new List<Job_Task__c>(); if(Trigger.isInsert) { for(Job_Task__c g:Trigger.New) { lstGC=[select id from Job_Task__c where Name =: g.Name]; if(lstGC.size()==1) { for(Job_Task__c g1:lstGC) g.Name.addError('Job Task with same Name Exists'); } } } else if(Trigger.IsUPdate) { for (Job_Task__c no : Trigger.new) { Job_Task__c old = Trigger.oldMap.get(no.id); if (no.Name != old.Name) { lstGC = [select id from Job_Task__c where Name =: no.Name]; if (lstGC.size() > 0) { for(Job_Task__c noOld : lstGC) { no.Name.addError('Job Task with same Name Exists'); } } } } } if(lstGC.size() > 0) update lstGC; }
replace your trigger with the following code and search and replace job_applications__c with your child object api name, and position__c with your parent object api name
Please revert back for any clarification or query
All Answers
hi,
there is no need to create trigger for that.
1) Create a rollup summary on parent, counting child records.
2) create a validation rule, condition is if rollupsummaryfield > 1, show error.
Hope this will help u
Rohit,
this was not my requirement... here you are restricting me to create child Record (if it is more than) for any particular Parent...
whatever you have given me it works fine...
but i want that, Suppose you have created a master record named as JOB then for that JOB there should not be any child record with repeated value,
means you shuld not create child record named as ABC more than one time.... but you can create child record with same name for anyother master record...
hope you get it!!
replace your trigger with the following code and search and replace job_applications__c with your child object api name, and position__c with your parent object api name
Please revert back for any clarification or query
Thanks Rohit,
it works!!