function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MY VF and ApexMY VF and Apex 

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;    
}

 

Best Answer chosen by Admin (Salesforce Developers) 
rohitsfdcrohitsfdc

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

 

trigger dupCheckChild on Job_Application__c (before insert, before update) {

List<Job_Application__c> lstGC = new List<Job_Application__c>();
    for(Job_Application__c g:Trigger.New)
    {
        lstGC=[select id, position__c from Job_Application__c where Name =: g.Name];
        if(lstGC.size()>0)
        {
          for(Job_Application__c PosID : lstGC){
          	if(g.Position__c == PosID.Position__c)
          	  g.Name.addError('Job Task with same Name Exists');
          }}}}

 Please revert back for any clarification or query

 

All Answers

rohitsfdcrohitsfdc

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

MY VF and ApexMY VF and Apex

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!!

rohitsfdcrohitsfdc

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

 

trigger dupCheckChild on Job_Application__c (before insert, before update) {

List<Job_Application__c> lstGC = new List<Job_Application__c>();
    for(Job_Application__c g:Trigger.New)
    {
        lstGC=[select id, position__c from Job_Application__c where Name =: g.Name];
        if(lstGC.size()>0)
        {
          for(Job_Application__c PosID : lstGC){
          	if(g.Position__c == PosID.Position__c)
          	  g.Name.addError('Job Task with same Name Exists');
          }}}}

 Please revert back for any clarification or query

 

This was selected as the best answer
MY VF and ApexMY VF and Apex

Thanks Rohit,

it works!!