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
Salesforce2015Salesforce2015 

Help for Trigger

Hi Experts,
 
Below are our objects and relation between them.

Objects and Relations

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.
pconpcon
t sounds like you're looking for someone to write the code for this project. These forums are not meant for that; you'll want to post over on the AppExchange site [1] where you can match up with a developer looking to work on your project. If you have code that you have written and are having problems I am happy to help you troubleshoot any issues that have.

[1] https://appexchange.salesforce.com/developers
Salesforce2015Salesforce2015
Hi pcon,

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');
            }
        }
        
    }
}
pconpcon
I've taken the liberty of cleaning up your code and reformatting it a little bit.
 
trigger PartnerGroupDel on Partner_Group_Requirement__c (before delete) {
    for (Workflow_Instance__c workflow : [
        select 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.oldMap.keySet() and
            Request_Status__c != 'Approved'
    ]) {
        WorkflowUtil.cancelRequest(workflow.Request__c);
    }
}

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.