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
Jasmeen Kaur 22Jasmeen Kaur 22 

Trigger for a specific record type

Hello Experts

I have very basic developing skills but trying to learn more.
I created a trigger in salesforce to detect potential duplicate cases which is working as expected. But I want to apply this trigger to a specific record type. However, at the moment it is working for every record type.

Here is my apex trigger;


trigger DuplicateCase on Case (before insert, before update) {

  for (Case c : Trigger.new) {
       List<Case> cases = [SELECT id, ISClosed, Subject FROM Case WHERE Subject = :c.Subject AND IsClosed = False];
        if (cases.size() > 1) {
            c.Is_Duplicate__c = TRUE;
        }else
            c.Is_Duplicate__c = FALSE;
}
}

I followed very smiliar posts but no help as such. May be those solutions are not working for me.

The name of record type which I want to use is : Credit Limit Exceeded. 

Can an expert write that for me or suggest me how to write the trigger for any particular record type? 
Looking forward to it.

Thanks in advance.
 
Best Answer chosen by Jasmeen Kaur 22
ANUTEJANUTEJ (Salesforce Developers) 
Can you try 

RecordType CaseRecordType = [SELECT Id
                                         FROM RecordType
                                         WHERE SobjectType = 'Case' AND Name = 'Credit Limit Exceeded'
                                         LIMIT 1];

and replace if(s.RecordTypeId == recordTypeId) with if(s.RecordTypeId == CaseRecordType.id)

also I would suggest running the above query in query editor to see if the above soql is returning any record in case if there are any errors modify it accordingly.

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Jasmeen,

>> https://developer.salesforce.com/forums/?id=906F0000000AprlIAC

>> https://trailblazers.salesforce.com/answers?id=90630000000hcDrAAI

I found these above examples that are having a similar use case you can try checking them once.

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
Jasmeen Kaur 22Jasmeen Kaur 22
Hi Anutej

Thanks for your reply, I tried these posts but no luck. May be I am using it for before insert and before update. Does that make any difference?

Regards
 
Jasmeen Kaur 22Jasmeen Kaur 22
I rewrite the trigger as:

trigger DuplicateCase on Case (before insert, before update) {

 Id recordTypeId = [Select Id From RecordType Where DeveloperName = 'Credit_Limit_Exceeded'].Id;
    List<Case> casesList = new List<Case>();

    for (case s : Trigger.new){
        if(s.RecordTypeId == recordTypeId)
            casesList.add(s);
    }

   for (case c : Trigger.new){
       List<Case> cases = [SELECT id, ISClosed, Subject FROM Case WHERE Subject = :c.Subject AND IsClosed = False];
        if (cases.size() > 1) {
            c.Is_Duplicate__c = TRUE;
        }else
            c.Is_Duplicate__c = FALSE;
}
}

Now when I try to create a record:

Below is the error I am getting:
User-added image

Thanks in advance
ANUTEJANUTEJ (Salesforce Developers) 
Can you mention the scenario you are implementing so as to check further and respond back?
Jasmeen Kaur 22Jasmeen Kaur 22

Yes sure Anutej.

In case object, there is a case record type called "credit limit exceeded". Every case in this record type comes in with a subject. This subject for every account and every sales order is unique. The Subject is in a format : "Credit Hold: Account name: Sales Order".

I want to allow salesforce to create duplicate cases but once a case is created with an exsiting subject line, it should flag that case.

For that I have created a field called "Is Duplicate". So when a case comes in with a subject which already exist in case object, it ticked that field.

But as in our organisation we have other case record types as well, such as accounts enquiry. In that case every case has the same subject, so now trigger is also marking those cases as duplicate. Which I dont want to do.

Now the concern is, I only want to allow this trigger to work for one record type.

But following other posts and solution above error is coming in.

Hope that helps.

Regards 

ANUTEJANUTEJ (Salesforce Developers) 
You can try the below snippet and modify accordingly:

I wrote this snippet as a sample for before insert please extend the functionality to before update as well as per your need.
 
trigger DuplicateCase on Case (before insert, before update) 
{
	if(trigger.isbefore && trigger.isinsert)
	{
	List<Case> casesList = new List<Case>();
    set<string> subs= new set<string>();
    Map<String, Integer> casemap= new Map<String, Integer>();    

 	Id recordTypeId = [Select Id From RecordType Where Name = 'Credit_Limit_Exceeded'].Id;
    	for (case s : Trigger.new)
    	{
        	if(s.RecordTypeId == recordTypeId)
           	{ 
           		String Str= '%'+s.Subject+'%';
				subs.add(Str);
			}
    	}
	
	casesList=[select Id, Subject from Case Where RecordTypeId= :recordTypeId AND Subject LIKE :subs];

	for(Case c: casesList)
	{
		if(casemap.containsKey(c.Subject))
		{
		Integer temp= ((Integer)casemap.get(c.Subject))+1;
		casemap.put(c.Subject,temp);
		}
		else
		{
		casemap.put(c.Subject,1);
		}
	}

   	for(Case c:trigger.new)
   	{
   		if(casemap.get(c.Subject)>1) { c.Is_Duplicate__c = TRUE;}
   		else{c.Is_Duplicate__c = FALSE;}
   	}
	}
}

Do note this is a sample snippet and you will have to modify it or extend it as per your usecase.

Please close the thread by marking it as solved so that it can help others in the future.  

Thanks.
Jasmeen Kaur 22Jasmeen Kaur 22
Hello Anutej

I used this trigger and again the same error. The error is coming for this line:

Id recordTypeId = [Select Id From RecordType Where Name = 'Credit_Limit_Exceeded'].Id;

Line 9 column 1

Earlier it was line 5 column 1 and line was this that time as well.

 
ANUTEJANUTEJ (Salesforce Developers) 
what is the error you are getting?
 
Jasmeen Kaur 22Jasmeen Kaur 22
Error is the same :

User-added image
 
ANUTEJANUTEJ (Salesforce Developers) 
Can you try 

RecordType CaseRecordType = [SELECT Id
                                         FROM RecordType
                                         WHERE SobjectType = 'Case' AND Name = 'Credit Limit Exceeded'
                                         LIMIT 1];

and replace if(s.RecordTypeId == recordTypeId) with if(s.RecordTypeId == CaseRecordType.id)

also I would suggest running the above query in query editor to see if the above soql is returning any record in case if there are any errors modify it accordingly.
This was selected as the best answer
Jasmeen Kaur 22Jasmeen Kaur 22
Thanks Anutej 

With this condition, It is working now.

What I have used now is:

trigger DuplicateCase on Case (before insert, before update) 
{
    RecordType CaseRecordType = [SELECT Id
                                         FROM RecordType
                                         WHERE SobjectType = 'Case' AND Name = 'Credit Limit Exceeded'
                                         LIMIT 1];
    
    List<Case> casesList = new List<Case>();

    for (case s : Trigger.new){
        if(s.RecordTypeId == caserecordType.Id)
            casesList.add(s);
    }

   for (case c : Trigger.new){
       List<Case> cases = [SELECT id, Subject FROM Case WHERE Subject = :c.Subject];
        if (cases.size() > 1) {
            c.Is_Duplicate__c = TRUE;
        }else
            c.Is_Duplicate__c = FALSE;


Thank you so much for your time and efforts. Much Appreciated.

Regards 
Jasmeen
ANUTEJANUTEJ (Salesforce Developers) 
Glad to know it helped :)