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
Amit Singh1989Amit Singh1989 

Trigger to avoid duplicate Job Name

Hi friends,

I have created a object Job in my developer org, written a trigger to avoid Duplicate Record Name,and it is working fine in case of duplicate case insensitive record Name,
for example -:
i have created a Job with Name -->  JOB
next time if I try to create new job with Name JOB I will get an error message "Duplicate Job Name" , that means it is working fine.

 

but if I try to use case sensitive record say job , it will throw an error "Attempt to de-reference a null object", How to fix this issue.

 

i am using this link for Trigger.

Trigger to avoid duplicate lead email

 

here is my code,

 

trigger AvoidDuplicateJobName on Job__c(before insert, before update) 
{

    Map<String, Job__c> jobMap = new Map<String, Job__c>();
    for (Job__c job : System.Trigger.new) 
    {
 
        if ((job.Name != null) && (System.Trigger.isInsert || (job.Name != System.Trigger.oldMap.get(job.Id).Name))) 
        {
            if (jobMap.containsKey(job.Name)) 
            {
                job.Name.addError('Another new Job has the '+ 'same Name.');
            }
            else 
            {
                jobMap.put(job.Name, job);
            }
       }
    }
    
    for (Job__c job : [SELECT Name FROM Job__c WHERE Name IN :jobMap.KeySet()])
    {   
     
        Job__c newjob = jobMap.get(job.Name);
        newjob.Name.addError('A Job with this Name '+ ' already exists.');
       
    }
}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Rahul SharmaRahul Sharma

Hi Amit,

 

I'm not sure but a simpler logic would be to just use a set.

In trigger first thing must be to collect all the job names existing in a database in  a set. Then iterate all the trigger.new values and keep adding new non duplicate names into set (so as to bulk safe them) and add error for duplicate one's

 

Here is a code for you, i dont think it would help but must give you an idea:

 

trigger AvoidDuplicateJobName on Job__c(before insert, before update) 
{
	Set<String> setJobName = new Set<String>();

	for (Job__c job : [SELECT Name FROM Job__c WHERE Id Not IN: Trigger.new])
	{   
		setJobName.add(job.Name);
	}

	for (Job__c job : System.Trigger.new) 
	{
		if (setJobName.contains(job.Name)) 
		{
			job.Name.addError('Another new Job has the '+ 'same Name.');
		}
		else if(!setJobName.contains(job.Name))
		{
			setJobName.add(job.Name);
		}
	}
}

 

.

All Answers

SvenSven

Hi,

 

Why are u using a trigger for that functionality?

can't you just use the standard setting on a text field?

 

on the general options section you have :

- Do not allow duplicate values

Treat "ABC" and "abc" as duplicate values(case insensitive)

Treat "ABC" and "abc" as different values(case sensitive)

 

Sven

 

 

Rahul SharmaRahul Sharma

Hi Amit,

 

I'm not sure but a simpler logic would be to just use a set.

In trigger first thing must be to collect all the job names existing in a database in  a set. Then iterate all the trigger.new values and keep adding new non duplicate names into set (so as to bulk safe them) and add error for duplicate one's

 

Here is a code for you, i dont think it would help but must give you an idea:

 

trigger AvoidDuplicateJobName on Job__c(before insert, before update) 
{
	Set<String> setJobName = new Set<String>();

	for (Job__c job : [SELECT Name FROM Job__c WHERE Id Not IN: Trigger.new])
	{   
		setJobName.add(job.Name);
	}

	for (Job__c job : System.Trigger.new) 
	{
		if (setJobName.contains(job.Name)) 
		{
			job.Name.addError('Another new Job has the '+ 'same Name.');
		}
		else if(!setJobName.contains(job.Name))
		{
			setJobName.add(job.Name);
		}
	}
}

 

.

This was selected as the best answer
Amit Singh1989Amit Singh1989

Sven,

Job Name is standard Name field and i think there is no such kind of options for Name standard field.

 

 

Thanks

SvenSven

No indeed,

 

but you can do it with a custom field.

 

You create a custom text field that you set those options on and you hide that field from the page layout. then you create a workflow that updates that field with the name field and when you have a duplicate that will throw an error message.

 

Easy and no code and test coverage :-)