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
sree prasadsree prasad 

Case Closed Trigger

trigger PaymentscheduleCases on Payment_Schedule__c (after update) 
{
    set<string> oppid=new set<string>();
    set<string> psidD=new set<string>();
    Map<id,opportunity> opp=new map<id,opportunity>();
    List<case> cse=new List<case>(); 
    List<Payment_Schedule__c> psListDemand=new List<Payment_Schedule__c>();
    Map<id,Payment_Schedule__c> lstPsD=new Map<id,Payment_Schedule__c>();
    
    if(checkRecursive.runDemand==True)
    {
        //checkRecursive.runDemand=false;
        if(trigger.oldMap.get(p.id).Demand_Note_Raised__c!=p.Demand_Note_Raised__c&& p.Demand_Note_Raised__c==true)
            {
                psidD.add(p.id);
                oppid.add(p.Opportunity__c);
            }
      }
        psListDemand=[SELECT Id,Name,Demand_Note_Raised__c,Opportunity__c FROM Payment_Schedule__c WHERE id=:psidD ];
         if(psidD.size()>0)
        {
            for(Opportunity o:[SELECT id,accountid,ownerid,Unit_Number1__c,CRM__c,account.PersonContactId,Project1__r.Minimum_Amount__c,(SELECT id,name FROM Payment_Schedules__r) FROM opportunity WHERE id=:oppid])
            {
                opp.put(o.id,o);
                 for(Payment_Schedule__c p1: psListDemand)
                {
                    lstPsD.put(p1.id,p1);
                }
            }
       for(Payment_Schedule__c p1: trigger.new)
            {
                if(trigger.oldMap.get(p1.id).Demand_Note_Raised__c!=p1.Demand_Note_Raised__c && p1.Demand_Note_Raised__c==true)
                {
                    Case cs= new Case();
                     if(opp.get(p1.Opportunity__c).Project1__r.Minimum_Amount__c != Null)
                    {
                       if(opp.get(p1.Opportunity__c).Project1__r.Minimum_Amount__c < p1.Amount_Pending__c)
                        {
                            cs.Payment_Schedule__c=p1.id;
                            if(opp.containskey(p1.Opportunity__c))
                            {
                                cs.AccountId=opp.get(p1.Opportunity__c).accountid;
                                cs.ContactId=opp.get(p1.Opportunity__c).account.PersonContactId;
                               if(opp.get(p1.Opportunity__c).CRM__c !=null)
                                {
                                    cs.ownerid=opp.get(p1.Opportunity__c).CRM__c;
                                }
                                else
                                {
                                    cs.ownerid=opp.get(p1.Opportunity__c).ownerid;
                                }
                             system.debug('++++++++++'+opp.get(p1.Opportunity__c).ownerid);
                            }
                             
          /*  if(P1.Status__c == 'Completed' && p1.Amount_Pending__c ==0){
            system.debug('----------------P1.Status__c-------'+P1.Status__c);
            cs.Status = 'Closed';
            system.debug('----------------P1.Status__c-------'+ cs.Status);
            cs.Origin = 'clsoed';
            system.debug('----------------P1.Status__c-------'+ cs.Origin);
            update cs;                                
                       
          }*/

                            cs.Subject= 'Unit Number('+opp.get(p1.Opportunity__c).Unit_Number1__c+') Amount Pending('+p1.Amount_Pending__c+') Due Date('+ system.today().day()+'/'+system.today().month()+'/'+system.today().year()+')';
                            cs.Opportunity__c=p1.Opportunity__c;
                            cs.Type='Payment Schedule';
                            cse.add(cs);
                        } 
                    }
                }
            }
        }
        if(cse.size()>0)
        {
          checkRecursive.runDemand=False;
            insert cse;
        }
    }
}

Now In Payment_Schedule__c  object when Status Picklist field is Completed and amount Pending Field is  '0' i want to close that case


How can i Achive this Trigger.

Please Reply Fast 
 
sree prasadsree prasad
when recipt amount is entered perticuler amount of Payment_Schedule__c that time only status__c filed would be changed to Completed.(only perticuler Payment_Schedule__c case only i need to close