0

I have a field named Statut__c , it is a picklist ( Active / Inactive ) , i want to get a static ressource named Inactif if the value is Inactive and Actif is the value is Active .

i DID get any result , the static ressource don't appear ,like if it don't exist

My code is below :

JS

import STATUT_FIELD from '@salesforce/schema/Account.Station_status__c';
import Actif from '@salesforce/resourceUrl/Actif'; 
import Inactif from '@salesforce/resourceUrl/Inactif';
import AccountStatut  from '@salesforce/apex/MyController.AccountStatut';
// i added this line based on the proposition 
import { getFieldValue } from 'lightning/uiRecordApi';


export default class Station extends LightningElement {
  Actif = Actif ;
  Inactif = Inactif ;
  @track icon ;
    connectedCallback(){
      AccountStatut().then(data=>
          {
            switch(getFieldValue(data, STATUT_FIELD)) {
case 'Inactive': 
  this.icon = Inactif
  break
case 'Active':
  this.icon = Actif
  break
}
          })

Html

  <div>
  <img src={statut_icon}> </div>

Apex

public with sharing class MyController {
    @AuraEnabled(cacheable=true)
    public static Account AccountStatut(Id AccountId){
        return [SELECT Id,  Station_status__c, FROM Account 
         WHERE Id=:AccountId LIMIT 1];
    }

}
4
  • 2
    Welcome to Salesforce Stack Exchange (SFSE)! Please edit your question to add information as to what you mean by "not visible": Are you getting an error? (If so, add exact error text.) Are you gettting "weird" results? (If so, don't say "weird", but explain exactly what you are seeing.) Etc.
    – Moonpie
    Feb 1, 2022 at 14:52
  • @Moonpie thank you , i just modified my question
    – mary snh
    Feb 1, 2022 at 14:56
  • I think the answer to the following question will help you: salesforce.stackexchange.com/q/337115/81648
    – Moonpie
    Feb 1, 2022 at 15:39
  • 1
    Please do not change the code that you initially posted to what was suggested in an answer. That will confuse potential future readers who are trying to follow the progression of your original question on through to the answer. You can accept an answer (which you did - thanks!), and/or you can comment on an answer. If you do want to update your original question to indicate that you tried something given in a comment or an answer, instead of changing your original code, please add it at the bottom of your question. Many people put UPDATE at the bottom and then what they tried below that.
    – Moonpie
    Feb 1, 2022 at 22:13

1 Answer 1

0

You're accessing the field incorrectly. You should use getFieldValue.

import { getFieldValue } from 'lightning/uiRecordApi'

...

switch(getFieldValue(data, STATUT_FIELD)) {
  case 'Inactive': 
    this.icon = Inactif
    break
  case 'Active':
    this.icon = Actif
    break
}

You could also use the value directly:

switch(data[STATUT_FIELD.fieldApiName]) {

The [] operator accesses a property on an object dynamically.

3
  • thank you for your help , but in my code i already use ` @wire(AccountStatut,{ AccountId: '$recordId'}) account; get Statut__c (){ return this.account.data ? getSObjectValue(this.account.data, STATUT_FIELD ) : ''; }` How can i use your code proposition
    – mary snh
    Feb 1, 2022 at 17:41
  • @marysnh this code: if(data.STATUT_FIELD== 'Inactive'){ is incorrect, you need to use getFieldValue there instead.
    – sfdcfox
    Feb 1, 2022 at 20:34
  • i have tried your solution , but i don't now what i have wrong because the image is not visible , i will modified in my first code , if you can help me , i will be very thankful
    – mary snh
    Feb 1, 2022 at 21:55

You must log in to answer this question.

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