+ Start a Discussion
Eric Boller 14Eric Boller 14 

Apex/OnClick JavaScript Button Error

We have the below custom button on opportunities:

{!REQUIRESCRIPT("/soap/ajax/19.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/19.0/apex.js")} 
var eQuoteEndpoint = sforce.apex.execute("UAUpdate","GenerateEQ", 
{user_id:"{!Opportunity.OwnerId}",opp_id:"{!Opportunity.Id}"});
//var eQuoteEndpoint =  //'URL REDACTED' + "token=//{!User.Quote_Token__c}" + "&opportunityId={!Opportunity.Id}";
console.log(String(eQuoteEndpoint)); 
window.open(String(eQuoteEndpoint));

Every so often it gives the below error:

A problem with the OnClick JavaScript for this button or link was encountered:

{faultcode:'soapenv:Client',
faultstring:'System.QueryException: LIst has no rows for assignment to SObject

Class.UAUpdate.GenerateEQ: line 9, column 1', }
 

As far as I can tell, it is referencing the below Apex class (only pasted until line 9 out of 184).

global class UAUpdate{
    public static boolean isApexTest = false;
    
    WebService static String GenerateEQ(String user_id, String opp_id){
      // Perform SOQLs to obtain relevant information.
      User user = [SELECT Id, Quote_Token__c FROM User WHERE Id = :user_id LIMIT 1];
      Opportunity opp = [SELECT Id, AccountId FROM Opportunity WHERE Id = :opp_id LIMIT 1];
      Account account = [SELECT Id, Name FROM Account WHERE Id = :opp.AccountId LIMIT 1];
      Contact contact = [SELECT Id, Email, FirstName, LastName, MailingCity, MailingPostalCode, MailingState, MailingStreet, Phone FROM Contact WHERE AccountId = :opp.AccountId LIMIT 1];

Is the class trying to reference a missing record/object? I can't tell what exactly it would be and how to fix it. Would appreciate any help.

 

Amit Chaudhary 8Amit Chaudhary 8
try to debug below lines:-

global class UAUpdate{
    public static boolean isApexTest = false;
    
    WebService static String GenerateEQ(String user_id, String opp_id){
     System.debug('---------user_id------->'+user_id);
     System.debug('-----------opp_id----->'+opp_id);

      // Perform SOQLs to obtain relevant information.
      User user = [SELECT Id, Quote_Token__c FROM User WHERE Id = :user_id LIMIT 1];
      Opportunity opp = [SELECT Id, AccountId FROM Opportunity WHERE Id = :opp_id LIMIT 1];
     System.debug('-----------opp ----->'+opp );

      Account account = [SELECT Id, Name FROM Account WHERE Id = :opp.AccountId LIMIT 1];
     System.debug('-----------account ----->'+account );
      Contact contact = [SELECT Id, Email, FirstName, LastName, MailingCity, MailingPostalCode, MailingState, MailingStreet, Phone FROM Contact WHERE AccountId = :opp.AccountId LIMIT 1];
     System.debug('-----------contact ----->'+contact );

And share your log
Aditya312Aditya312
Please check which query is written at line 9 in your apex code. It is not returning any records as per the filter you have set in the query. Try to check the filter in the query at line 9.
Eric Boller 14Eric Boller 14
Contact contact = [SELECT Id, Email, FirstName, LastName, MailingCity, MailingPostalCode, MailingState, MailingStreet, Phone FROM Contact WHERE AccountId = :opp.AccountId LIMIT 1]; This appears to be line 9 in the query. I’ll try running the debug when I get a chance.
Aditya312Aditya312
Ok you can manually check that whether there is any contact record associated to the account of that opportunity on which you are hitting the button.
Eric Boller 14Eric Boller 14
Yep, that's one of the first things I tried but for some reason wasn't able to find the account. After finding a few duplicates, making some merges, and ensuring the association relationships were in place the tool worked normally again. Thank you for your help!