0

I'm fairly new to salesforce. I'm trying to create an Apex Trigger that creates a record of another object. I don't see any errors in the logs but I don't see any new records being created as well.

What I'd like to happen here is when a new cpCoupon_batch__c record is created, a new record of cpCoupon_single__c also gets created.

The if conditions checks first that cpCoupon_batch__c.quantity__c has a value greater than 0. If condition is true, it will create x number of records based on the value in quantity__c.

I hope someone here can help me out.

Here is my code:

trigger batchCreateCp on cpCoupon_batch__c (after insert) {
    List<cpCoupon_single__c> insert_list = new List <cpCoupon_single__c>();
    
    for(cpCoupon_batch__c cpCoupon_batch : Trigger.New) {
        
        if(cpCoupon_batch.quantity__c > 0) {
            
            for (Integer i=0; i>=cpCoupon_batch.quantity__c; i++) {
                cpCoupon_single__c cp = new cpCoupon_single__c(status__c='VALID');
                insert_list.add(cp);
            }
        }
    }
    
    try {
        insert insert_list; 
    } catch (system.Dmlexception e) {
        system.debug (e);
    }
}

I've updated the code based on feedback. Unfortunately, I still don't see any new records created. I also just tried to remove the quantity check for now, to see if that is blocking the creation. Unfortunately no.

trigger batchCreateCp on cpCoupon_batch__c (after insert) {
    List<cpCoupon_single__c> insert_list = new List <cpCoupon_single__c>();
    
    for(cpCoupon_batch__c cpCoupon_batch : Trigger.New) {
        for (Integer i=0; i<=5; i++) {
            cpCoupon_single__c cp = new cpCoupon_single__c(status__c='VALID');
            insert_list.add(cp);
        }
        
        /**if(cpCoupon_batch.quantity__c > 0) {
            
            for (Integer i=0; i<=cpCoupon_batch.quantity__c; i++) {
                cpCoupon_single__c cp = new cpCoupon_single__c(status__c='VALID');
                insert_list.add(cp);
            }
        }**/
    }
    
    try {
        insert insert_list; 
    } catch (system.Dmlexception e) {
        system.debug (e);
    }
}

Final Update, I've verified the code to be working. The problem is that I can't see the records created by my Apex code, it's probably a Salesforce configuration that I need to fix to see them. I am able to keep track of the changes using the unique ID of my records.

Here is the final version of the code I am using. Sorry if I haven't changed the system.debug at my try-catch. I'll fix it later.

trigger batchCreateCp on cpCoupon_batch__c (after insert) {
    System.debug('Inside Trigger');
    List<cpCoupon_single__c> insert_list = new List <cpCoupon_single__c>();
    
    for(cpCoupon_batch__c cpCoupon_batch : Trigger.New) {
        if(cpCoupon_batch.quantity__c > 0) {
            System.debug('Passed quantity check');
            for (Integer i=0; i<cpCoupon_batch.quantity__c; i++) {
                cpCoupon_single__c cp = new cpCoupon_single__c(status__c='VALID');
                System.debug('Passed create coupon single');
                insert_list.add(cp);
            }
        }
    }
    
    try {
        insert insert_list;
         System.debug('list inserted, size: '+insert_list.size());
    } catch (system.Dmlexception e) {
        system.debug (e);
    }
}

1 Answer 1

1

I can see few errors in your code. Your for loop looks incorrect to me

for (Integer i=0; i<=cpCoupon_batch.quantity__c; i++) //instead of > it should be <

Second in try catch you are just adding a debug log, instead of debug you should use addError or send email to notify user for error.

Third you are not linking cpCoupon_single__c records with any any other record. So this trigger will only create n number of cpCoupon_single__c records. And I am not sure how will they help you if they don't have any relationship with anyone, check that as well and add null check as well for quantity.

2
  • Thanks for the feedback, especially on the for loop, I didn't catch that :D Unfortunately the code still doesn't work. No new records are still being created. I'm at a lost to be honest. I am not sure what's happening.
    – tamboy
    Commented Aug 27, 2020 at 0:26
  • Hey, I just want to say, thanks for the help. I've verified that the code is working. I just couldn't see the record for some reason. I am able to track the changes using the Coupon Ids, I see the numbers going up. What I need to do now is check how to configure salesforce to show records created by my Apex code.
    – tamboy
    Commented Aug 27, 2020 at 2:31

You must log in to answer this question.

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