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
Mahmoud Coudsi 1Mahmoud Coudsi 1 

Can I invoke an APEX class through both a Java button and an Apex Trigger? Depends on the criteria

Hi,

I have an APEX class that is built on the lead object. The class runs multiple methods (Trigger API calls) and if-statments to determine lead qualifcation. I'd like to invoke the class through both a Java button and an Apex Trigger:

1. Here is the code for the check eligibilty Java button:

// This part been implemented first and it worked fine.
 
{!REQUIRESCRIPT("/soap/ajax/30.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/30.0/apex.js")}
 
var msg = new sforce.SObject("Lead");
msg.Id = '{!Lead.Id}';
msg.Qualification_message__c = null;
 
var result = sforce.connection.update([msg]);
 
var patientId='{!Lead.Name}';
var result = sforce.apex.execute("Eligibility","PreEligiblityCheck",{MemberID: MemberId});
alert(result);
window.location.reload();
sforce.debug.trace=true;
 

2. Here is the code for the APEX trigger: (This is where the issues started coming up)

Trigger CheckEligiblityUponSelfSchedule on Lead (before update) {
  
  for(Lead l: Trigger.new) {                           
    if(l.self_scheduled__c == TRUE){
    Eligibility.runEligibilityCheck(l.Id);
    }
  }
}
 

3. Here is the error message that I'm getting upon checking the box for self-schedule and saving the lead record:

Error:Apex trigger CheckEligiblityUponSelfSchedule caused an unexpected exception, contact your administrator: CheckEligiblityUponSelfSchedule: execution of BeforeUpdate caused by: System.QueryException: List has no rows for assignment to SObject: Class.Eligibility.buildEligibilityRequest: line 175, column 1

Snippet from runEligiblityCheck method code: 
Public static String runEligibilityCheck(String MemberID) {
       
      // confidential code to make an API call to a third-party tool to determine lead qualification based on a set logic we have set up internally.  
         
    }
Snippet from buildEligibilityRequest method

private static String buildEligibilityRequest(String MemberID) {
        // Find the patient in a query using the name passed in PatientID
// This is line 175 where the code is breaking //
       Lead l = [select Id, Name, Leads_DOB__c, FirstName, LastName, self_scheduled__c
                              From Lead
                              Where Name = :MemberID];
 

Any ideas about this specfic issue that I'm having? Also any ideas if it's possible to execture the same class through a Java-script button and trigger depends on a specific criteria?

SandhyaSandhya (Salesforce Developers) 
Hi,

You will get the above error if the query is not resulting any rows

Refer below salesforce help article for more explaination.

https://help.salesforce.com/articleView?id=000159853&type=1
 
Please mark it as solved if my reply was helpful, it will make it available
for others as a proper solution.

Best Regards,
​Sandhya
 
Raj VakatiRaj Vakati
private static String buildEligibilityRequest(String MemberID) {
        // Find the patient in a query using the name passed in PatientID
// This is line 175 where the code is breaking //

            Lead l =new Lead(); 
       ;= [select Id, Name, Leads_DOB__c, FirstName, LastName, self_scheduled__c
                              From Lead
                              Where Name = :MemberID];
if(l.Name!=null){
return l.Name;
}else{
return '':
}
try somethink like above
 
Raj VakatiRaj Vakati
private static String buildEligibilityRequest(String MemberID) {
        // Find the patient in a query using the name passed in PatientID
// This is line 175 where the code is breaking //

            Lead l =new Lead(); 
       l= [select Id, Name, Leads_DOB__c, FirstName, LastName, self_scheduled__c
                              From Lead
                              Where Name = :MemberID];
if(l.Name!=null){
return l.Name;
}else{
return '':
}