You need to sign in to do that
Don't have an account?

Avoid Duplicates in Master-Detail Relationship
Hi,
I have two custom object Job and Task.
They are having master detail relationship where Job(Master) --> Task(Child)
Each Job must contain multiple Tasks.
For e.g. Job Name: Prining Books
Task
Task Name: Task 1
Task Name : Task 2
It should not allow another record with student name as 'Task 1', because it is already available under Job.
But Task Name "Task 1" can be allowed to add under different Job (say under Job Design Graphics), simply because here Task 1 is belongs to different Job.
How to resolve this problem?
Thanks,
Devendra S
try this, i did not test it but it should work in most case. if you are try to switch names in a single batch you may have some issues, you may need to do this in two steps or put in more logic.
EX. A=C
B=A in single batch will faill since A has yet to commit.
All Answers
Create a trigger to check all the sibliing names on insert/update a native alternative would be to create a new unique( External Id) field, and create workflow to populate with Job__c.id+Task__c.Name
I would agree with the previous poster.
You'll create a short "before insert, before update" Trigger on the "Task" object to query all other Task objects in the scope of the parent record, check the field for duplicates and return a "record.addError('You can't have duplicate Tasks on a Job!'); message.
-Andy
Hi techman97,
I put the below the trigger, it works when i enter Task data from User Interface. But when i import data through Import Custom Wizard, it fails and duplicate Task records are entered.
I am looking for an trigger that will work for bulk records too. What changes are required in above code, so that it will work for bulk records too.
Thanks,
Devendra S
That would be the expected behavior - you're only checking records that currently exist in the system. Going through the import wizard is going to load a bunch of Tasks in the trigger.new list and your code isn't set to check that.
Look into the Trigger.newMap functionality and add that into your logic.
You need to include your batch to the collection of siblings to test for matches.
Ex...
Hi tes2,
The solution which you have given works with single record. But it does not work when i upload bulk data through import custom wizard.:(
Thanks,
Devendra
Please post your code, the code i provided checks both the batch and the data commited to the database.
Hi tes2,
Sorry mate. My mistake. The code you have given is perfect. Thank you very much..!!
The below code is working well. Hope this will work at the time of update too. Not tested for update operation though. Will it work update also??
Thanks & Regards,
Devendra
Hi tes2,
Can you please tell me, What changes are required for update? and It should apply for bulk records also.
Its urgent..:(
Thanks,
Devendra
Sorry I didnt get this to you the first time, and I dont have the time code now here is a summary:
1. create the inDB to a map<uniquestring,old task__c> instead of a set.
2. when loop through the trigger.new ... in the inDB match logic is check to see if the Trigger.IsUpdate and additionally check to see if the id of the match is the same as the Task__c in the map done in 1 above.
Hi tes2,
This is what i have tried today. Thank you so much.
try this, i did not test it but it should work in most case. if you are try to switch names in a single batch you may have some issues, you may need to do this in two steps or put in more logic.
EX. A=C
B=A in single batch will faill since A has yet to commit.
i have a problem .. help me..
Avoid Duplicate Name Only on master in Master-Detail relationship
Hi,
I have two custom object Claim__c(master) and Labour_Cost__c(detail).
They have master detail relationship.
I want to create a Trigger which prevent duplicate Name(standard field which is created default when we create any custom object) only on master object.
but we can multipe record with same Name from detail object and we select it from lookup field (Claim_Name__c) on detail object.
plz write a trigger for same.
for example: I have a record on Master Object with Name='S1 Claim' .
if i create a new record with Name 'S1 Claim' then it will display an error 'record already exists with same name'.
But,I can create multiple record with Name 'S1 Claim' from detail object.
Thanks,
Ghulam