0

Hi All I declared objectName with value why I am getting objectName undefined, doinit of component

  // Public Property
  @api objectName = 'Account';
  @api columns;
  @api relatedFieldAPI;
  @api whereClause;
  @api limit = 10;
  // Private Property
  @track data = [];
  @track soql;
  @track offSet = 0;
  @track totalRows = 0;
  // Do init funtion
  connectedCallback() {
    this.buildSOQL();
    console.log("soql::", this.soql);
  }
  //Count number of rows
  @wire(countRecords,{soql : `SELECT count() FROM ${this.objectName}`}) rows({error,data}) {
    if(data){
      console.log(data)
    }
  }

enter image description here

  • 1
    It is the "this" variable that is undefined. Clearly the template literal evaluation isn't supported by the LWC compiler. On top of that, this is a very dangerous implementation - passing a whole SOQL query to a function leaves that function vulnerable to serious abuse. You would be far better to limit what this function can do, such as counting Account instances only when the user has read access to Account. Even just passing the object name dynamically to a "countRecords" function is risky, though the function can check permissions first. Do this using a "$objectName" reference. – Phil W Feb 25 '20 at 16:16
2

The "template" syntax is not "reactive." This means that this may not exist, and changing the objectName won't cause a refresh of the object. Instead, set the entire SOQL as a variable:

soql;

...

@wire(countRecords,{soql : '$soql' }) rows({error,data}) {
  if(data){
    console.log(data)
  }
}

...

And have your buildSOQL method set the entire query string to the attribute.

Alternatively, call the method imperatively:

connectedCallback() {
  this.buildSOQL();
  countRecords({soql : `SELECT count() FROM ${this.objectName}`})
    .then(result => { ... });
}
  • 1
    Upvoted though you have not added any warning about the risk of exposing an aura enabled function that can evaluate arbitrary SOQL. Yes, you have literally answered the question but the OP should be steered away from such insecure implementation. – Phil W Feb 25 '20 at 16:34
0

you can use something of this sort

objectName = 'Account';

query = `SELECT count() FROM ${this.objectName}`;

//Count number of rows
@wire(countRecords,{name : '$query'}) rows({error,data}) {
if(data){
  console.log(data)
}

the moment you mark an api, it would wait for the component to load or wait for the parent to pass the value... which would cause an error...

it is, however, in this case, recommended to implement using connectedCallback()

Hope this helps

0

so as to fix the error all you have to do is remove 'this' keyword. Once you remove that it becomes reactive.Once the value for objectName is defined it callout will happen. Also lets place an addition if check. This is the solution for your implementation, but it would be better if u do an imperative call back.

if(this.objectName){
 //Count number of rows
  @wire(countRecords,{soql : `SELECT count() FROM ${objectName}`}) rows({error,data}) {
    if(data){
      console.log(data)
    }
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.