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
Akis AthanasiadisAkis Athanasiadis 

Records not created by apex

Hello,

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
Best Answer chosen by Akis Athanasiadis
Tad Aalgaard 3Tad Aalgaard 3
It is failing is because you have a colon in front of true and false in your SQOL. Only use colon when referencing a parameter.  True and false are not parameters.

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

Tad Aalgaard 3Tad Aalgaard 3
It is failing is because you have a colon in front of true and false in your SQOL. Only use colon when referencing a parameter.  True and false are not parameters.

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.

 
This was selected as the best answer
Akis AthanasiadisAkis Athanasiadis
still no records are created
Tad Aalgaard 3Tad Aalgaard 3
Have you tried your SOQL in developer console with a contract id to see if you are getting any values back?

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.
Akis AthanasiadisAkis Athanasiadis
you mean on debug mode?
Tad Aalgaard 3Tad Aalgaard 3
System.debug is code that you add to apex that will write to the log that gets generated when your code runs.  You can open that log and view what you logged.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_system.htm

Example code
String x = 'blah'; 
System.debug('x : ' + x); // this will write x : blah to the log

​​​​​​​
vineet kumarvineet kumar

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.


 
Akis AthanasiadisAkis Athanasiadis
hope.... it didn't create any record there
vineet kumarvineet kumar
Akis,

I confirm you again that records got created.

Thanks.