0

I want to create a record of the payment schedule when the opportunity stage is booked, I want to map the field from Cost_Sheet__c object to payment schedule object which has the lookup with opp. oppo is the parent object for payment schedule obj and cost sheet obj. there is a field like cost1, name 1, minstone 1 on cost sheet object so I want to map these fields to create a record for the payment schedule field

trigger PaymentScheduleRecords on Opportunity (After insert, After Update){  
        
       List<Payment_Schedule__c> paymentSchedule = new List <Payment_Schedule__c>();     

       List<Cost_Sheet__c> costSheet = new list<Cost_Sheet__c>( [Select Id,Name from Cost_Sheet__c]); 
    
            For( Opportunity opp : Trigger.new){   
            
                 system.debug('opportunity'+ opp);    
        
                 if(opp.StageName =='Booking' && (opp.Apartment__c != Null)){                                      
                    Payment_Schedule__c pay = new Payment_Schedule__c();       
                    Cost_Sheet__c costsheet = new Cost_Sheet__c();                     
                    pay.Name = costsheet.Name_1__c;           
                    pay.Payment_Percent__c = costsheet.Milestone_1__c;   
                    pay.Amount_Receivable__c = costsheet.Cost_1__c;                   
                    paymentSchedule.add(pay);    
            
                 }   
    
           }
    
     }

Is anyone help me to do that?

Thanks.

3
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
    – Community Bot
    Dec 1, 2021 at 12:18
  • @Tejaswini, What if the opportunity has multiple cost sheet records. Is opportunity parent or child for Cost Sheet. Can you edit the question and provide all the required details. Dec 1, 2021 at 12:33
  • @SaiPraveen Kakkirala I update the problem Dec 1, 2021 at 17:00

1 Answer 1

0

Can you please try the below code. The Query which you used above is not queried based on the opportunity id. Add if any additional fields or filters required in If condition or in the query.

trigger PaymentScheduleRecords on Opportunity (after update) {
 List<Payment_Schedule__c> paymentSchedule = new List <Payment_Schedule__c>();     
map<id,opportunity> mapoppy= new map<id,opportunity>();
        
    set<id> oppyid= new set<id>();
    
            For( Opportunity opp : Trigger.new){   
            if(opp.StageName =='Booking')
            oppyid.add(opp.id);       
                  
    
           }
    List<Cost_Sheet__c> costSheet = new list<Cost_Sheet__c>( [Select Id,Name,Name_1__c,Milestone_1__c,Cost_1__c,Opportunity__c  from Cost_Sheet__c where Opportunity__c in :oppyid ]);
    
    for(Cost_Sheet__c c:costSheet )
   
    {                                      
                    Payment_Schedule__c pay = new Payment_Schedule__c();       
                    pay.Name = c.Name_1__c;           
                    pay.Payment_Percent__c = c.Milestone_1__c;   
                    pay.Amount_Receivable__c = c.Cost_1__c;   
                    pay.Opportunity__c=c.Opportunity__c;
                    paymentSchedule.add(pay);    
            
                 } 
    insert paymentSchedule;
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .