1

This is my Before insert Trigger. When i am trying to execute this am getting error as

SObject row does not allow errors

Trigger

Trigger Service_BeforeInsert on SRV_Service__c(before insert) {

    system.debug('## Inside Service_BeforeInsert ##');
    if (Rfleet_TriggerAdministration.canTrigger('Service.onBeforeInsert')) {

        if(trigger.isinsert && trigger.isbefore) {            

            Rfleet_ServiceCoachBuilderType_VFC.updateOrderCamper(trigger.new);

        }
    }
}

Class

public class Rfleet_ServiceCoachBuilderType_VFC {

    public static void updateOrderCamper(List<SRV_Service__c> serviceLists){

          String strCarSetID;
          String strServiceType;
          String strServiceID;
          String strServiceCode;
    List <SRV_Service__c> serviceRecSimpleProductCheck = new List<SRV_Service__c>();
    List <SRV_Service__c> serviceList=[Select id,CarSet__c,ServiceType__c,SimpleProduct__r.Productcode__c  from SRV_Service__c where id IN :serviceLists];     

          for(SRV_Service__c service : serviceList) {
               strCarSetID= service.CarSet__c;
               strServiceType=service.ServiceType__c;
               strServiceID=service.ID;
               strServiceCode=service.SimpleProduct__r.Productcode__c;

          }

          serviceRecSimpleProductCheck = [Select Id,CarSet__c,ServiceType__c,SimpleProduct__r.Productcode__c from SRV_Service__c where CarSet__c=:strCarSetID and ServiceType__c='Simple product' and ID!=:strServiceID];

            for(SRV_Service__c service : serviceList) {

                if (strServiceType!=null && strServiceType.equals('Simple product') && strCarSetID!=null) {

                for(SRV_Service__c serviceObj : serviceRecSimpleProductCheck ) {
                    string str= serviceObj .SimpleProduct__r.Productcode__c; 

                    if (str.equals(strServiceCode) ){

                        service .addError(Label.SRV_CoachBuilderDuplicate);
                        break;
                    }             

                } 
                }
            }   
    }
}
3
  • Error is at line serv.addError(Label.SRV_CoachBuilderDuplicate); . POst your whole trigger here
    – Samir
    Commented Feb 23, 2017 at 9:25
  • @Samir Updated the full trigger
    – uma451
    Commented Feb 23, 2017 at 9:29
  • Is your lineList <SRV_Service__c> serviceList=[Select id,CarSet__c,ServiceType__c,SimpleProduct__r.Productcode__c from SRV_Service__c where id IN :serviceLists]; returning any records? As you are passing trigger.new to this method and id is not available in before insert.Can you check?
    – Samir
    Commented Feb 23, 2017 at 9:39

2 Answers 2

4

addError is only supported by Trigger context variables.

You are iterating over serviceList, which is generated by a query.

What you should ideally do is iterate over list which represents trigger.new' variable.

Like:

for(SRV_Service__c service : serviceLists) {
    // serviceLists represents Trigger.new
    //based in your logic add error
    service.addError('Your message');
}
1
  • As you said if i change the second for loop as below also, for(SRV_Service__c service : serviceLists) {} serviceLists is nothing but trigger.new ...Am getting the same error
    – uma451
    Commented Feb 23, 2017 at 10:12
0

In the following loop you are not updating strServiceCode before comparing with String str.

for (SRV_Service__c service: serviceList) {
    if (strServiceType != null && strServiceType.equals('Simple product') && strCarSetID != null) {
        for (SRV_Service__c serviceObj: serviceRecSimpleProductCheck) {
            string str = serviceObj.SimpleProduct__r.Productcode__c;
            if (str.equals(strServiceCode)) {
                service.addError(Label.SRV_CoachBuilderDuplicate);
                break;
            }
        }
    }
}

I think you have to update the strServiceCode after first for loop.

for (SRV_Service__c service: serviceList) {
    strServiceCode=service.SimpleProduct__r.Productcode__c;
    if (strServiceType != null && strServiceType.equals('Simple product') && strCarSetID != null) {
        for (SRV_Service__c serviceObj: serviceRecSimpleProductCheck) {
            string str = serviceObj.SimpleProduct__r.Productcode__c;
            if (str.equals(strServiceCode)) {
                service.addError(Label.SRV_CoachBuilderDuplicate);
                break;
            }
        }
    }
}

You must log in to answer this question.

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