You need to sign in to do that
Don't have an account?
Jasmeen 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.
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.
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
>> 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.
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
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:
Thanks in advance
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
I wrote this snippet as a sample for before insert please extend the functionality to before update as well as per your need.
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.
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.
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.
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