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
rajesh kumar 10rajesh kumar 10 

Problem on trigger can any see my problem

I have a doubt on trigger that i created a trigger on Primary application allocation object which is related to opportunity and when i create records in primary application allocation object then in opportunity object the "Primary Application Allocation Set" field should be checked and when i delete all the records in primary  application allocation object which are are related to same opportunity record then that field should be false. but my trigger is creating a problem that when i am creating records then that field is comnig true but when i delete atleast one record then it is becoming false but i dont want like that when i delete all records in primaary application alocation which are related to that opportunity then only the field should be false.. 
Below is my trigger please go through it and suggest me please..

trigger UpdatePrimaryApllicationAllocationSet on Primary_Application_Allocation__c (after insert,after delete) {

    list<id> lst = new list<id>();
    if(trigger.isInsert){
    for(Primary_Application_Allocation__c paa : trigger.new) {
        lst.add(paa.opportunity__c);
    }}
    if(trigger.isDelete){
        for(Primary_Application_Allocation__c paa : trigger.old) {
        lst.add(paa.opportunity__c);
        }
    }
   
    list<Opportunity> opplst = new list<Opportunity>();
    opplst=[SELECT Primary_Application_Allocation_Set__c FROM Opportunity where Id in :lst];

    if(trigger.isInsert) {
        for(Primary_Application_Allocation__c paa : trigger.new) {
            for(Opportunity o : opplst) {
                if(paa.Opportunity__c == o.Id) {
                    o.Primary_Application_Allocation_Set__c = true;
                }
            }
        }
        update opplst;
    }
   
    if(trigger.isDelete) {
        for(Primary_Application_Allocation__c paa : trigger.old) {
            for(Opportunity o : opplst) {
                if(paa.Opportunity__c == o.id) {
                    o.Primary_Application_Allocation_Set__c = False;
                }
            }
        }
        update opplst;
    }
           
}

thanks in advance
Ankit AroraAnkit Arora
There are so many problems with the trigger, like bulk handling etc. Anyways that is not a concern right now, the problem is that even if one Primary Application Allocation record is deleted you are making the checkbox false on opportuntiy here :

if(trigger.isDelete) {
        for(Primary_Application_Allocation__c paa : trigger.old) {
            for(Opportunity o : opplst) {
                if(paa.Opportunity__c == o.id) {
                    o.Primary_Application_Allocation_Set__c = False;
                }
            }
        }
        update opplst;
    }

Rather using this :

list<Opportunity> opplst = new list<Opportunity>();
    opplst=[SELECT Primary_Application_Allocation_Set__c FROM Opportunity where Id in :lst];

You should use aggregate result query which will check what is the current count of Primary Application Allocation on opportunity and if it 0 then make that checkbox false else do nothing.

AggregateResult[] groupedResults
  = [SELECT COUNT(Id) FROM Primary_Application_Allocation_Set__c where Opportunity__c id in: lst];

This might not be the exact code, but I hope now you know how you can get it. Once you have the results you can then loop on it and check if count is 0 then make that checkbox false, else true.
rajesh kumar 10rajesh kumar 10
Hi i am unable to understand ur concept  can u tell me clearly please

Tony TannousTony Tannous
Hello ,

i did some modification in the section isDelete, i just want from your side to make sure that the relation object name is the correct one and it should work.

once it work you need to do the below :

1- move your code to a seperated class instead of having the code in the trigger so you will be able to create a test class.

2- to seperate the after inert, after delete logic into 2 methods so it will be more clear.


trigger UpdatePrimaryApllicationAllocationSet on Primary_Application_Allocation__c (after insert,after delete)
{
    list<id> lst = new list<id>();
    if(trigger.isInsert){
    for(Primary_Application_Allocation__c paa : trigger.new) {
        lst.add(paa.opportunity__c);
    }}


    if(trigger.isDelete){
        for(Primary_Application_Allocation__c paa : trigger.old) {
        lst.add(paa.opportunity__c);
     }
    }
  
    list<Opportunity> opplst = new list<Opportunity>();
  
    if(trigger.isInsert) {
  opplst=[SELECT Primary_Application_Allocation_Set__c FROM Opportunity where Id in :lst];
        for(Primary_Application_Allocation__c paa : trigger.new) {
            for(Opportunity o : opplst) {
                if(paa.Opportunity__c == o.Id) {
                    o.Primary_Application_Allocation_Set__c = true;
                }
            }
        }
        update opplst;
    }
  
    if(trigger.isDelete) {

opplst=[SELECT Primary_Application_Allocation_Set__c,(select id from Primary_Application_Allocation__r) FROM Opportunity where Id in :lst];
    
            for(Opportunity o : opplst)
   {
       list<Primary_Application_Allocation__c> listPrimaApp= o.Primary_Application_Allocation__r;
                if(listPrimaApp.size()== 0)
    {
                    o.Primary_Application_Allocation_Set__c = False;
                }
            }
       
        update opplst;
    }
          
}


Good Luck