function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Alejandro Garcia LopezAlejandro Garcia Lopez 

Hello, help whit this : System.QueryException: List has no rows for assignment to SObject

Hello, i have a problem with this trigger, i need to update a field from account (Pendientes_Realizados__c) but the query doesn't return something (c.Oferta__r.gerente_operativo__c) is an id from account that query returns the id in workbench but nothing in the trigger 
trigger gerente on Documento__c (after update) {
for (Documento__c c:Trigger.new)
{
    Account l=[select Pendientes_Realizados__c from Account where id=:c.Oferta__r.gerente_operativo__c];

        l.Pendientes_Realizados__c=10;
        update l;
  }
    
}

 
Best Answer chosen by Alejandro Garcia Lopez
Alejandro Garcia LopezAlejandro Garcia Lopez
i solved it creating other variable and comparing r.gerente_operativo__c with the id of the account and it works!
oferta__c r=[select id,etapa__c, gerente_operativo__c from oferta__c where id=:c.oferta__c];
    Account l=[select Pendientes_Realizados__c from Account where id=:r.gerente_operativo__c];

All Answers

karthikeyan perumalkarthikeyan perumal
Hello, 

The exception is because of you are not getting any record in your SOQL query..
which meet the where criteria so it's give error.

in workbench  it will return id coz its checking the full object. in trigger its  checking for the current record of the trigger. so there is no id for the currenct record.  

Documento__c c:Trigger.new   ---- means you are retriving record based on current record instatce( c)  c.Oferta__r.gerente_operativo__c. 

to avoid this error always do null check before retrie the records. 

kinldy use below modified code.
 
trigger gerente on Documento__c (after update) {
for (Documento__c c:Trigger.new)
{
    Account l=[select Pendientes_Realizados__c from Account where id=:c.Oferta__r.gerente_operativo__c];
       if(l != null)
       { 
        l.Pendientes_Realizados__c=10;
        update l;
       }
  }
    
}

hope this will work for you. 
make it solved if its works for you 

Thanks
karthik
 
Alejandro Garcia LopezAlejandro Garcia Lopez
i solved it creating other variable and comparing r.gerente_operativo__c with the id of the account and it works!
oferta__c r=[select id,etapa__c, gerente_operativo__c from oferta__c where id=:c.oferta__c];
    Account l=[select Pendientes_Realizados__c from Account where id=:r.gerente_operativo__c];
This was selected as the best answer