![]() |
彷徨的青蛙 · 解构华润资本链_产经动态_财经纵横_新浪网· 9 月前 · |
![]() |
威武的煎饼 · 不设置等于白买,你的记录仪设置正确吗? - 知乎· 11 月前 · |
![]() |
气势凌人的豆芽 · 告别传奇,KFC疯狂不再-虎嗅网· 11 月前 · |
![]() |
飘逸的手链 · 人物 | 我的大学四年:改变与不变· 1 年前 · |
![]() |
高大的春卷
1 年前 |
![]() |
彷徨的青蛙 · 解构华润资本链_产经动态_财经纵横_新浪网 9 月前 |
![]() |
威武的煎饼 · 不设置等于白买,你的记录仪设置正确吗? - 知乎 11 月前 |
![]() |
气势凌人的豆芽 · 告别传奇,KFC疯狂不再-虎嗅网 11 月前 |
![]() |
飘逸的手链 · 人物 | 我的大学四年:改变与不变 1 年前 |
Follow these steps:
1. Try to separate trigger and it's handler class.
2. Always try to put the null check condition in your code.
3. Never put query inside for loop.
4. In your code, inside query use listId instead of setId.
5. While you are store Id of any record use Set<Id>.
6. In your code when the case list comes in the "trigger.new" why you are query this?
Try this Code:
Trigger: trigger Ticket_Trigger on Case (after insert) { if(trigger.IsInsert && trigger.IsAfter) { Ticket_Trigger_handler.showCase(trigger.new); Handler: public class Ticket_Trigger_handler { public static void showCase(List<Case> caseList) { Set<Id> caseIdSet = new Set<Id>(); List<Case> caList = new List<Case>(); if(caseList.size() > 0) { for(Case c : caseList) caseIdSet.add(c.Id); caList = [SELECT Case.Id, Case.Origin, Case.ContactId, Case.AccountId, Case.Status, Case.Priority, Case.Subject, Case.LastModifiedById, Case.OwnerId, Case.CreatedById, Case.CreatedDate, Case.Description FROM Case WHERE Case.Id IN : caseIdSet]; System.debug(caseList); catch(Exception e) system.debug('The following exception has occured:' +e.getMessage()); system.debug('The following Line:' + e.getLineNumber());
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
All Answers
Follow these steps:
1. Try to separate trigger and it's handler class.
2. Always try to put the null check condition in your code.
3. Never put query inside for loop.
4. In your code, inside query use listId instead of setId.
5. While you are store Id of any record use Set<Id>.
6. In your code when the case list comes in the "trigger.new" why you are query this?
Try this Code:
Trigger: trigger Ticket_Trigger on Case (after insert) { if(trigger.IsInsert && trigger.IsAfter) { Ticket_Trigger_handler.showCase(trigger.new); Handler: public class Ticket_Trigger_handler { public static void showCase(List<Case> caseList) { Set<Id> caseIdSet = new Set<Id>(); List<Case> caList = new List<Case>(); if(caseList.size() > 0) { for(Case c : caseList) caseIdSet.add(c.Id); caList = [SELECT Case.Id, Case.Origin, Case.ContactId, Case.AccountId, Case.Status, Case.Priority, Case.Subject, Case.LastModifiedById, Case.OwnerId, Case.CreatedById, Case.CreatedDate, Case.Description FROM Case WHERE Case.Id IN : caseIdSet]; System.debug(caseList); catch(Exception e) system.debug('The following exception has occured:' +e.getMessage()); system.debug('The following Line:' + e.getLineNumber());
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
Thank you for the quick response, your answer was very helpful and does almost exactly what I needed my trigger to do! As I said, I am new to Apex and have been trying to understand how to use Apex Triggers and make SOQL queries.
I have been tasked with a Jira-Salesforce Integration, and I have managed to successfully make REST API calls to Jira in Apex. I have been trying to create a trigger that will add Salesforce cases to Jira upon the case creation, but I was struggling with how to access new cases on their creation and their fields. Currently I have been simply trying to test whether I could do it or not, and your answer has put me ahead.
I appreciate the time and effort you have put in for your response!
Thanks,
Fayyad