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
linda blinda b 

Create Case on parent object in for loop

I would like to write a scheduled Apex class which checks for certain conditions on a custom object, and if they are true, create a case on the master-detail parent object, here is my code so far:

global class CaseGenerator implements Schedulable {
	global void execute(SchedulableContext ctx) {
        createInvoiceCase();
	}
    public void createInvoiceCase() {
     List<Billing_Unit__c> invoiceCases = new List<Billing_Unit__c>();
     invoiceCases = [SELECT Id, Contract__r.Id
                                  FROM Billing_Unit__c
                                  WHERE Invoicing_Date__c = TODAY AND
                                  Unlimited__c = TRUE];
    for(Billing_Unit__c bgut : invoiceCases) {
        // create case related to Contract__r.id here
       Case cseobj = new Case (subject= 'Rechnung erstellen'; Contract__c=Contract__r) (???)
    }
    update invoiceCases;
    }
}
I don't know how to create the case on the parent object in the for loop, any help/tips would be greatly appreciated! :)
Best Answer chosen by linda b
Niraj Kr SinghNiraj Kr Singh
Hi IInda,

Try this code and let me know if it works for you.
global class CaseGenerator implements Schedulable {
	global void execute(SchedulableContext ctx) {
        createInvoiceCase();
	}
    public void createInvoiceCase() {
		List<Case> newCaseList = new List<Case>();
		List<Billing_Unit__c> invoiceCases = [SELECT Id, Contract__r.Id
											  FROM Billing_Unit__c
											  WHERE Invoicing_Date__c = TODAY AND
											  Unlimited__c = TRUE];
		if(invoiceCases != null) {
			for(Billing_Unit__c bgut : invoiceCases) {
				// create case related to Contract__r.id here
			   Case cseobj = new Case (subject= 'Rechnung erstellen', Contract__c=Contract__r, Status='New'); //As per ur requirements give other fields
			   newCaseList.add(cseobj);
			}
			
			//Insert cases in DB.
			insert newCaseList;
			
		}
	}
}
Thanks
Niraj


 

All Answers

Niraj Kr SinghNiraj Kr Singh
Hi IInda,

Try this code and let me know if it works for you.
global class CaseGenerator implements Schedulable {
	global void execute(SchedulableContext ctx) {
        createInvoiceCase();
	}
    public void createInvoiceCase() {
		List<Case> newCaseList = new List<Case>();
		List<Billing_Unit__c> invoiceCases = [SELECT Id, Contract__r.Id
											  FROM Billing_Unit__c
											  WHERE Invoicing_Date__c = TODAY AND
											  Unlimited__c = TRUE];
		if(invoiceCases != null) {
			for(Billing_Unit__c bgut : invoiceCases) {
				// create case related to Contract__r.id here
			   Case cseobj = new Case (subject= 'Rechnung erstellen', Contract__c=Contract__r, Status='New'); //As per ur requirements give other fields
			   newCaseList.add(cseobj);
			}
			
			//Insert cases in DB.
			insert newCaseList;
			
		}
	}
}
Thanks
Niraj


 
This was selected as the best answer
linda blinda b
Thank you, it worked!