You need to sign in to do that
Don't have an account?

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 {} } }
As per salesforce best practices use Try/Catch block
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh