1

I am receiving the following sync error in Pardot:

System.QueryException: execution of BeforeUpdate caused by List has no rows for assignment to SObject ()

I think it is this trigger that is causing the issue, but don't know how to amend it to cater for zero rows:

    trigger trLeadSetRegion on Lead (before update) {
Lead l= trigger.new[0];
if (l.Region__c == null) {
    User o = [select Region__c from User where Id = :L.OwnerId];    
    l.Region__c = o.Region__c;       
   }
 }

If anyone can point me in the right direction it would be much appreciated.

  • Are you sure that it is owned by User and not by Queue – Vijay Ganji Mar 14 at 16:15
1

The record is owned by a Queue, and so the query fails, as there are no rows retrieved. This trigger needs to be bulkified anyways. A proper version of this trigger would look like this:

trigger trLeadSetRegion on Lead (before update) {
    Map<Id, String> userRegions = new Map<Id, String>();
    for(Lead record: Trigger.new) {
        userRegions.put(record.OwnerId, null);
    }
    for(User record: [SELECT Region__c FROM User WHERE Id = :userRegions.keySet()]) {
        userRegions.put(record.Id, record.Region__c);
    }
    for(Lead record: Trigger.new) {
        record.Region__c = userRegions.get(record.OwnerId);
    }
}
  • Hi @sfdcfox - many thanks for your answer. I'm getting an error: "Compile Error: Unexpected token 'Map'. at line 1 column 1" when I try to save the trigger. Am I missing something? – Brumps Mar 14 at 16:25
  • @Brumps I didn't include the first or last line of code: trigger trLeadSetRegion on Lead (before update) { ... } – sfdcfox Mar 14 at 16:30
  • Sorry! Being a bit thick. I see now. Thank you – Brumps Mar 14 at 16:31
  • @Brumps No worries, we all have moments like that. Glad I could help. – sfdcfox Mar 14 at 16:33
0

You are getting this error because Lead can be owned by User or can be owned by Queue. In your case, it might have been owned by Queue and hence your SOQL didn't return any rows which leads to the error which you are getting.

Hence, you should handle the scenario where your lead is not assigned to User.

Your trigger code should be something like below

trigger trLeadSetRegion on Lead (before update) {
    Set<Id> setOwnerIds = new Set<Id>();
    for(Lead l : Trigger.New){
        setOwnerIds.add(l.OwnerId);
    }
    Map<Id, User> mapUser = new Map<Id, User>([SELECT Id, Region__c from User where Id IN: setOwnerIds]);    
    for(Lead l: Trigger.New){            
        l.Region__c = mapUser.containsKey(l.OwnerId) ? mapUser.get(l.OwnerId).Region__c : null;            
    }
}
  • This wouldn't work if the region field was already populated and the owner was changed. – sfdcfox Mar 14 at 16:31
  • Yup but as per OP's existing logic, he is trying to update the region only when it is null. Ideally, it should be updated every time owner is updated, hence I left the business logic on him. Anyhow, updated the code :) – Vijay Ganji Mar 14 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.