1

I have below trigger code where I get "List has No Rows for Assignment to SObject"

Apex trigger:

trigger CallVisibility on Compliance_Incident_Transaction_abv__c(before insert, before update) {

    // List<Compliance_Incident_Transaction_abv__c> olis = [SELECT Id, Call_Account_abv__c,Call_Load_abv__c, Account_abv__c,Ownerid FROM Compliance_Incident_Transaction_abv__c WHERE Id IN: Trigger.newMap.keySet()];

    for (Compliance_Incident_Transaction_abv__c o: Trigger.new) {

        //System.debug(o.Order_Phase__r.Order__Item__r.Order_Project__c);
        String Terr=[select territoryId from userterritory where userid=:o.Ownerid].territoryid;
        String TerrName=[select name from Territory where Id=:Terr].Name;
        if(o.Call_Account_abv__c!=null) {
            String ATL= [select Territory_vod__c from Account_Territory_Loader_vod__c where Account_vod__c=:o.Call_Account_abv__c limit 1].territory_vod__c; // This line throws error
            if(ATL.contains(TerrName))
                {o.Call_Load_abv__c=true;}

        }
    }
}
  • 1
    Pretty self explanatory - you don't have any Account_Territory_Loader_vod__c records with a value for Account_vod__c that matches o.Call_Account_abv__c. Try debugging the values for the two fields and you'll get insight into what's going wrong – Brian Mansfield Jul 21 '15 at 15:28
  • I noticed in your query you're selecting Territory_vod__c but then using Account_vod__c in your WHERE clause. Is that intentional? – Brian Mansfield Jul 21 '15 at 15:32
  • n.b. Some vbl name improvements are in order -- (1) Terr should be terrId, (2) ATL should be something like terrVod as you are returning fields, not records in the left hand side of your assignment statements – cropredy Jul 21 '15 at 16:36
  • you are also doing soql inside of a for loop -- not best practice at all. Review doc on how to bulkify triggers by using maps – cropredy Jul 21 '15 at 17:03
3

The root cause of your issue is that you're assigning the result of a list into a singleton, and when there's no values returned, a QueryException is thrown. See these alternatives:

1)

try {

  String ATL= [select Territory_vod__c from Account_Territory_Loader_vod__c where Account_vod__c=:o.Call_Account_abv__c limit 1].territory_vod__c;
}
catch (System.QueryException e) {
  System.debug('caught ya!');
}

2)

List<Account_Territory_Loader_vod__c> atlv = [select Territory_vod__c from Account_Territory_Loader_vod__c where Account_vod__c=:o.Call_Account_abv__c limit 1];
if (atlv.size() == 0) {
  System.debug('nothin!');
} else {
  String ATL = atlv.get(0).territory_vod__c;
}
  • 1
    Ray - while these alternatives are good, the OP's issue was on the third SOQL. – cropredy Jul 21 '15 at 16:39

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.