You need to sign in to do that
Don't have an account?
Akis Athanasiadis
Records not created by apex
Hello,
I have created a trigger which is supposed to create multiple records.
When i check the box renew in contracts, records of contract renewal which are its children are created. However, when i do that no records are created. I check the contract renewals object just in case records arecreated without being assigned to the contracts. any ideas of what might be missing here?
when i
I have created a trigger which is supposed to create multiple records.
trigger CreateMultipleContacts on Contract (after insert, after update) { List<Contract_Renewal__c> contractFinalListToInsert = New List<Contract_Renewal__c>(); if(Trigger.isInsert || Trigger.isUpdate){ for(Contract c : Trigger.New) { if(c.Renew__c == true) { Integer fetchingAlreadyExistedRecords = [SELECT count() FROM Contract_Renewal__c WHERE Contract__c=:c.Id and Not_Renewed__c=:false and Multi_Year__c=:true]; if(fetchingAlreadyExistedRecords!=null) { // We are only creating a records when there at least one Contract record exists. for(Integer i=0; i<fetchingAlreadyExistedRecords; i++) { Contract_Renewal__c con = new Contract_Renewal__c(); con.Contract__c = c.Id; contractFinalListToInsert.add(con); } } } try{ if(!contractFinalListToInsert.IsEmpty()){ INSERT contractFinalListToInsert; } } catch(Exception e){ System.debug('The thrown exception for CreatingAutoRecords is:: ' + e.getMessage()); } } } }I have the Contracts object and the Contract Renewals which is related to Contracts(Contracts master object)
When i check the box renew in contracts, records of contract renewal which are its children are created. However, when i do that no records are created. I check the contract renewals object just in case records arecreated without being assigned to the contracts. any ideas of what might be missing here?
when i
Not_Renewed__c=:false and Multi_Year__c=:true
Should be
Not_Renewed__c=false and Multi_Year__c=true
Also, take your fetchingAlreadyExistedRecords SOQL and the entire try and catch out of the loop and put it outside the loop. Placing SQOL or DML (crud operations) inside loops is not good practice as it can cause you to hit the SQOL limit.
All Answers
Not_Renewed__c=:false and Multi_Year__c=:true
Should be
Not_Renewed__c=false and Multi_Year__c=true
Also, take your fetchingAlreadyExistedRecords SOQL and the entire try and catch out of the loop and put it outside the loop. Placing SQOL or DML (crud operations) inside loops is not good practice as it can cause you to hit the SQOL limit.
Add a bunch of System.debug into your code to see what values are coming back and also to find out where your code is going.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_system.htm
Example code
Hi Akis,
I am happy to let you know that the trigger you wrote is working perfectly. I have tested the same trigger in my Org.
Please go through the below practice:
1) Use Contract Standard Object
2) Use Contract Renewal Custom Object
3) Create Contract Record without checking Renew Checkbox
4) Create One Contract Renewal Record, select the Contract record that you created earlier in Contract Look-up and check Multi-Year checkbox and Save the record.
5) Confirm that the trigger on Contract that you wrote should be Active.
6) Now go to Contract record and click on Edit and check Renew button.
7) Once you save the Contract record, you check in the related list, there you will be finding one new Contract Renewal record associated with the same Contract record.
Please let me know if you face any difficulty.
Thanks.
I confirm you again that records got created.
Thanks.