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
Peter Martensen 8Peter Martensen 8 

I need not updating an Opportunity if the Apex result is Null

I managed to modify some existing Apex, which should have been the hard part, but I can't get it to fail properly if the result = Null.  The Apex is used in a Flow and it performs a sosl query on a long text are field.  Then it updates the Opportunity.  Can someone help me terminate the Apex if the result = Null?
 
global class SearchDrugNameSynonyms {
   @InvocableMethod
   public static void getID(List<String> recordID) {
   Opportunity ThisOpportunity=[select Compound_Name__c from Opportunity where ID=:recordId[0] limit 1];
   
   String fieldValue = ThisOpportunity.Compound_Name__c;
String fieldName = 'Drug_Name_Synonyms__c';
String query = 'FIND {"'+ fieldValue + '"}  RETURNING ' +
                  ' Drug_Name__c (id, ' + fieldName + ' ) LIMIT 1';
 List<List<sObject>> result = System.Search.query(query);
system.debug('Search result: ' + result[0]);//Get search result

List<Drug_Name__c> ThisDrugName=new list<Drug_Name__c>();
    if(!result.isEmpty()){
    ThisDrugName=result[0];
       ThisOpportunity.Compound_Name2__c = ThisDrugName[0].id;
   Update ThisOpportunity;
    }
    else {}
     
   }
}

 
Abdul KhatriAbdul Khatri
Please try this
 
global class SearchDrugNameSynonyms 
{
   	@InvocableMethod
	public static void getID(List<String> recordID) 
   	{
   		Opportunity ThisOpportunity=[select Compound_Name__c from Opportunity where ID=:recordId[0] limit 1];
   
   		String fieldValue = ThisOpportunity.Compound_Name__c;
		String fieldName = 'Drug_Name_Synonyms__c';
		String query = 'FIND {"'+ fieldValue + '"}  RETURNING ' +
                  			' Drug_Name__c (id, ' + fieldName + ' ) LIMIT 1';
       
		List<List<sObject>> result = System.Search.query(query);
		system.debug('Search result: ' + result[0]);//Get search result

		List<Drug_Name__c> ThisDrugName=new list<Drug_Name__c>();
    	if(result != null && !result.isEmpty())
        {
    		ThisDrugName=result[0];
       		ThisOpportunity.Compound_Name2__c = ThisDrugName[0].id;
   			Update ThisOpportunity;
    	}
   	}
}

 
mukesh guptamukesh gupta
Hi Peter,


As per salesforce best practices use Try/Catch block 
global class SearchDrugNameSynonyms {
   @InvocableMethod
   public static void getID(List<String> recordID) {
   Opportunity ThisOpportunity=[select Compound_Name__c from Opportunity where ID=:recordId[0] limit 1];
   
    String fieldValue = ThisOpportunity.Compound_Name__c;
	String fieldName = 'Drug_Name_Synonyms__c';
	String query = 'FIND {"'+ fieldValue + '"}  RETURNING ' +
					  ' Drug_Name__c (id, ' + fieldName + ' ) LIMIT 1';
	 List<List<sObject>> result = System.Search.query(query);
	system.debug('Search result: ' + result[0]);//Get search result

	List<Drug_Name__c> ThisDrugName=new list<Drug_Name__c>();
	
	try{
	   if(result != null && !result.isEmpty()){
        ThisDrugName = result[0];
       ThisOpportunity.Compound_Name2__c = ThisDrugName[0].id;
       Update ThisOpportunity;
    }
	} catch(DmlException e) {
    // Process exception here
	  system.debut('ERROR IN CATCH BLOCK===> '+e.getMessage());
	}
    
    
   }
}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh