3

Hello and thanks in advanced.

-Background-

I have a custom object for files, one of the files recorded in this object needs to be the primary file, but only one.

-Approach-

Create a trigger that checks before the new insert if there is another record already mark as primary, if so show a warning, otherwise insert the record.

-Problem-

I'm new to Apex code, to triggers and everything. So, not sure why this is not working. When I'm creating a new record and check the primary file checkbox it doesn't creates the record. So I'm unable to see if the trigger worked.

-Code-

trigger trg_Custom_Object on Custom_Object__c (before insert) {

    Set <Id> filesId = new Set <Id>();

    List <Custom_Object__c> filesToSave = new List <Custom_Object__c>();

    for (Custom_Object__c files : Trigger.new) {
        //Check if trigger.new Primary_file__c is TRUE
        if (files.Primary_file__c){
            //Add the Id to a list of ids
            filesId.add(files.Id);
            filesToSave.add(files);
        }

    }
    //Query the ids from object where Primary_file__c is TRUE and is old
    List <Custom_Object__c> filesList = new List <Custom_Object__c> ([select id from Custom_Object__c where Primary_file__c = true and id not in :filesID] );

    for (Custom_Object__c files : Trigger.new) {
        //For each new record to be inserted check if its Primary_file__c is set to TRUE
        //and if there is already a file with Primary_file__c set TRUE
        if (files.Primary_file__c && filesList.size() > 0){
            //Trhows error
            files.Id.AddError('You cannot have more than one primary file');
        } else {
            //Insert new record
            //insert filesToSave; (Commented by @dschach because unnecessary.)
        }
    }
}
9
  • 1
    You have another use case on update and delete. What happens if the primary file is deleted or someone tries to update a file to primary. You probably should cover this logic in your trigger as well.
    – greenstork
    Commented Mar 20, 2014 at 16:49
  • That's correct, in the update case the idea will be to uncheck the previous file and mark the new. In the delete case doesn't matter, It's not necessary to have a record mark as primary always.
    – Jose
    Commented Mar 20, 2014 at 16:55
  • 1
    Sure, just updated. (Update: When I'm creating a new record and check the primary file checkbox it doesn't creates the record. So I'm unable to see if the trigger worked.)
    – Jose
    Commented Mar 20, 2014 at 17:53
  • 2
    I think that your shoudn't call insertwithin an insert trigger of the same object. BTW: avoid insert in a loop Commented Mar 20, 2014 at 18:12
  • 2
    One of the problems that I can see is that you are trying to use the Id of the new record in a Before Insert trigger. At this point in the execution, Salesforce has not generated an Id for the record. Commented Mar 20, 2014 at 18:18

1 Answer 1

1

So, I'll be answering my own question thanks to the comments above I was able to complete it.

-Here is the code.-

trigger trg_Custom_Object on Custom_Object__c (before insert, before update) {

    Set <Id> filesId = new Set <Id> ();
    set <Id> objectsID = new Set <Id> ();

   for (Custom_Object__c files : Trigger.new) {
       //Check if trigger.new Primary_file__c is TRUE
       System.debug('files.Primary_file__c: '+files.Primary_file__c);
       if (files.Primary_file__c){
           //Add the Id to a list of ids
           filesId.add(files.Id);
           objectsID.add(files.Object__c);
       }     
   }
    System.debug('Object: ' + objectsID);
    //Query the ids from object where Primary_file__c is TRUE and is not new
    List <Custom_Object__c> filesList = new List <Custom_Object__c> (
        [select id, Primary_file__c from Custom_Object__c where Primary_file__c = true and     id not in :filesID and Object__c in :objectsID] );
    //System.debug('filesList: '+ filesList);
    System.debug('filesList.size(): ' + filesList.size());

    for (Custom_Object__c files : Trigger.new) {
        if (filesList.size() > 0){
            files.Id.AddError(Label.Primary_file_already_created);
        } 
    }
}

You must log in to answer this question.

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