You need to sign in to do that
Don't have an account?
Salesforce2015
Help for Trigger
Hi Experts,
Below are our objects and relation between them.
Trigger Requirement:
When a “partner group member” is inserted or deleted, the following logic needs to occur in the trigger.
1. Query for related Partner Group Requirements related to the Partner Group, associated to the added/removed Partner Group Member.
2. If there are Partner Group Requirements,
- If a partner group member was deleted, then existing "Request" associated to "Partner Group Requirement" should be delete (i.e. Request record delete).
- If there are no Partner Group Requirements, then do nothing.
Below are our objects and relation between them.
Trigger Requirement:
When a “partner group member” is inserted or deleted, the following logic needs to occur in the trigger.
1. Query for related Partner Group Requirements related to the Partner Group, associated to the added/removed Partner Group Member.
2. If there are Partner Group Requirements,
- If a partner group member was deleted, then existing "Request" associated to "Partner Group Requirement" should be delete (i.e. Request record delete).
- If there are no Partner Group Requirements, then do nothing.
[1] https://appexchange.salesforce.com/developers
I have total three trigger requirements.
with your inspiration i have completed below sucessful trigger with test class 77% code coverage.
Please give me good suggestions to improve myself in coding.
Trigger:
trigger PartnerGroupDel on Partner_Group_Requirement__c (before delete) {
list<Partner_Group_Requirement__c> ParGrReqlist=new list<Partner_Group_Requirement__c>();
set<Id> ParGreqIdSet=new set<Id>();
if(Trigger.isDelete) {
list<Workflow_Instance__c> WORKINSTList =[select id, Name, Request_Status__c, Request__c, Request__r.Partner_Group_Requirement__c from Workflow_Instance__c where Request__r.Partner_Group_Requirement__c IN:trigger.old and Request_Status__c!='Approved'];
if(WORKINSTList.size()>0){
for(Workflow_Instance__c WRINS :WORKINSTList){
Id requestId=WRINS.Request__c;
WorkflowUtil.cancelRequest(requestId);
}
}
else{
system.debug('^^^^^^^^^^^Test^^^^^^^^');
for(Partner_Group_Requirement__c PGReq : trigger.old){
PGReq.addError('Before delete "Partner Group Reuirement" please check Task Status; Task Status = Approved');
}
}
}
}
Since your trigger only operates on before delete, there is no reason to add a Trigger.isBefore check and you will never be able to actually test that code. Also, we can just iterate over the Workflow_Instance__c object directly with the for loop. if there are no matches to the query, then the loop will never occur.
It might be worth looking over your WorkflowUtil.cancelRequest method to see if it does and DML operations (insert/update/delete/etc) or any SOQL queries. If it does then you will want to look at making a bulk version of it that takes in a Set if Ids and then modifying this trigger a little bit.