3

System.FinalException: SObject row does not allow errors

When I attempt to test my error code. Other documentation I've read states that this would only occur if the error is not set to the record type of that which fires the trigger.

trigger IO_Billing_Entity_Update on Insertion_Order__c (after insert){

    //We now need to get gather all Billing Entities for the Brand related to the Insertion Order via the Initiative.Brand.Id
    //This list will contain all the Partner records

    list<Insertion_Order__c> iol0 = [SELECT Initiative__r.Brand__r.ParentId, Brand__c, Billing_Entity__c FROM Insertion_Order__c WHERE Id IN :trigger.new];
    system.debug(iol0);

    set<Id> relatedIds = new Set<Id>();
    for(Insertion_Order__c rec : iol0){
        relatedIds.add(rec.Initiative__r.Brand__r.ParentId);
    }
    system.debug('List of Corp IDs from IO List: '+relatedIds);

    List<Partner> be = new List<Partner>();
    list<Partner> bethis = new list<Partner>();

    //Query Partner Records - AccountTo will be our Corporation Id
    be = [SELECT Id, AccountFromId,AccountToId,Role FROM Partner WHERE AccountFromId IN :relatedIds AND Role = 'Billing Agency'];

    system.debug('Number of Billing Entities found:' + be.size());
    system.debug(be);

    //For each IO in the list...
    //First Check to see if there are Partners listed at all, if not, send Error
    for(Insertion_Order__c io : iol0){
        if(be.size() == 0 && io.Billing_Entity__c == null){
            io.addError('No Billing Agency assigned to Corporation. Please assign a Billing Agency to Corporation "Partners" list, or manually assign a Billing Entity here.');
        }

        //Now let's check each IO and see if how many BE's are related to it....
        else if(be.size() > 0 && io.Billing_Entity__c == null){
            for(Partner b0 : be){
                if(b0.AccountFromId == io.Initiative__r.Brand__r.ParentId){
                        bethis.add(b0);
                } 
            }
            system.debug('List of Billing Entities related to IO: ' + bethis);
            //We should only be automatically updating if there is only 1 Billing Entity, so let's filter out those with more than 1
            if(bethis.size() > 1){
                io.addError('Corporation contains more than 1 Billing Entity, please manually select the Billing Entity');
            }

etc etc etc - what am i doing wrong?

5

You can only use addError on variables which are in trigger context. That means you can only addError in variable you get from Trigger.New or Trigger.newMap

Here I can see, you are using Insertion_Order__c in SOQL and then adding error in it iol0 is not in triggger context , it fails in addError.

Thus your code will be

trigger IO_Billing_Entity_Update on Insertion_Order__c (after insert){

    //We now need to get gather all Billing Entities for the Brand related to the Insertion Order via the Initiative.Brand.Id
    //This list will contain all the Partner records

    list<Insertion_Order__c> iol0 = [SELECT Initiative__r.Brand__r.ParentId, Brand__c, Billing_Entity__c FROM Insertion_Order__c WHERE Id IN :trigger.new];
    system.debug(iol0);

    set<Id> relatedIds = new Set<Id>();
    for(Insertion_Order__c rec : iol0){
        relatedIds.add(rec.Initiative__r.Brand__r.ParentId);
    }
    system.debug('List of Corp IDs from IO List: '+relatedIds);

    List<Partner> be = new List<Partner>();
    list<Partner> bethis = new list<Partner>();

    //Query Partner Records - AccountTo will be our Corporation Id
    be = [SELECT Id, AccountFromId,AccountToId,Role FROM Partner WHERE AccountFromId IN :relatedIds AND Role = 'Billing Agency'];

    system.debug('Number of Billing Entities found:' + be.size());
    system.debug(be);

    //For each IO in the list...
    //First Check to see if there are Partners listed at all, if not, send Error
    for(Insertion_Order__c io : iol0){
        if(be.size() == 0 && io.Billing_Entity__c == null){
            Trigger.newMap.get(io.Id).addError('No Billing Agency assigned to Corporation. Please assign a Billing Agency to Corporation "Partners" list, or manually assign a Billing Entity here.');

        }

        //Now let's check each IO and see if how many BE's are related to it....
        else if(be.size() > 0 && io.Billing_Entity__c == null){
            for(Partner b0 : be){
                if(b0.AccountFromId == io.Initiative__r.Brand__r.ParentId){
                        bethis.add(b0);
                } 
            }
            system.debug('List of Billing Entities related to IO: ' + bethis);
            //We should only be automatically updating if there is only 1 Billing Entity, so let's filter out those with more than 1
            if(bethis.size() > 1){
                Trigger.newMap.get(io.Id).addError('Corporation contains more than 1 Billing Entity, please manually select the Billing Entity');
            }
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.