0

I am composing an LWC Component, that makes an imperative apex call to a method that takes ALL the fields of a record and return them to the client." I am using dynamic SOQL query, that expects a recordId from user and makes this call.

Apex Class:

  public static Broker_Application__c getDraftData(String recordId){
    
    


      Set<String> SobjectFields = Schema.getGlobalDescribe().get('Broker_Application__c').getDescribe().fields.getMap().keySet();     

   
   //Error in next line
  Broker_Application__c lstBroker = (Broker_Application__c) Database.query('SELECT ' + String.join(new List<String>(SobjectFields), ',') + ' FROM Broker_Application__c WHERE Id=:recordId LIMIT 1');
            
            return lstBroker;        
    }

The corresponding JS in apex interacts with this as:

 getDraftData({ recordId: this.recordId })
    .then((data) => {
      console.log('data received');

      console.log('draft data received from apex' + [...Object.entries(this.BrokerRecord)]);
      for (let [key, value] of Object.entries(data)) {
        if (value != undefined)
          this.BrokerRecord[key] = value;
      }

Now, the apex class seems to not return anything with an 'List has no rows for assignment to sObject' in the bold line---- any clues? Its an LWC component hosted as a site and presently holds all permissions for Create/Read granted.

1
  • 1
    add console.log for this.recordId. What do you mean by site?
    – ytiq
    Commented Mar 31, 2022 at 12:05

1 Answer 1

1

First of all, you could just use getRecord and skip the Apex entirely.

@wire(getRecord, { recordId: '$recordId', layoutTypes: 'Full' }) brokerApp;

If you want to do this imperatively, which is the only reason you'd want to use Apex, your Apex could be:

public static sObject getDraftData(Id recordId) {
    return Database.query(
        'SELECT '+String.join(new List<String>(sObjectType.Broker_Application__c.fields.getMap().keySet()),',')+
        ' FROM '+sObjectType.Broker_Application__c.Name+
        ' WHERE Id = :recordId'
    );
}

Note that this might also be a security issue. Make sure the user has at least read-only access to the record, and has at least read access on the object.

1
  • Hi, this is a great solution and I don't fnd any flaws in my solution as well. But it doesn't retrieve any records, where could the permissions be missing? The component lives in an experience site hosted as a public link. It has got a profile of its own which is the default profile for all visiting users(Guest). Object level permission has read/write access, FLS is granted as visible. Any holes? Commented Apr 2, 2022 at 12:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .