0

Need some help in getting the field Types without calling Apex

4

You can use getObjectInfo to fetch the data types. You should use data.fields.fieldName.dataType.

Sample code:

import { LightningElement, wire, api, track } from 'lwc';
import OPPORTUNITY_OBJECT from '@salesforce/schema/Opportunity';
import { getObjectInfo } from 'lightning/uiObjectInfoApi';

export default class Poc extends LightningElement {
    @wire(getObjectInfo, { objectApiName: OPPORTUNITY_OBJECT })
    oppInfo({ data, error }) {
        if (data) {
            this.dataTypes = Object.values(data.fields).map((fld) => {
                let { apiName, dataType } = fld;
                return { apiName, dataType };
            });
            console.log('this.dataTypes => ', JSON.stringify(this.dataTypes));
        }
    }
}

Output:

[
  {
    "apiName": "AccountId",
    "dataType": "Reference"
  },
  {
    "apiName": "Amount",
    "dataType": "Currency"
  },
  {
    "apiName": "CampaignId",
    "dataType": "Reference"
  },
  {
    "apiName": "CloneSourceId",
    "dataType": "Reference"
  },
  {
    "apiName": "CloseDate",
    "dataType": "Date"
  },
  {
    "apiName": "ContactId",
    "dataType": "Reference"
  },
  {
    "apiName": "Convert_RecordType__c",
    "dataType": "Picklist"
  },
  {
    "apiName": "CreatedById",
    "dataType": "Reference"
  },
  {
    "apiName": "CreatedDate",
    "dataType": "DateTime"
  },
  {
    "apiName": "CurrentGenerators__c",
    "dataType": "String"
  },
  {
    "apiName": "DeliveryInstallationStatus__c",
    "dataType": "Picklist"
  },
  {
    "apiName": "Description",
    "dataType": "TextArea"
  },
  {
    "apiName": "ExpectedRevenue",
    "dataType": "Currency"
  },
  {
    "apiName": "Fiscal",
    "dataType": "String"
  },
  {
    "apiName": "FiscalQuarter",
    "dataType": "Int"
  },
  {
    "apiName": "FiscalYear",
    "dataType": "Int"
  },
  {
    "apiName": "ForecastCategory",
    "dataType": "Picklist"
  },
  {
    "apiName": "ForecastCategoryName",
    "dataType": "Picklist"
  },
  {
    "apiName": "HasOpenActivity",
    "dataType": "Boolean"
  },
  {
    "apiName": "HasOpportunityLineItem",
    "dataType": "Boolean"
  },
  {
    "apiName": "HasOverdueTask",
    "dataType": "Boolean"
  },
  {
    "apiName": "Id",
    "dataType": "String"
  },
  {
    "apiName": "IsClosed",
    "dataType": "Boolean"
  },
  {
    "apiName": "IsDeleted",
    "dataType": "Boolean"
  },
  {
    "apiName": "IsPrivate",
    "dataType": "Boolean"
  },
  {
    "apiName": "IsWon",
    "dataType": "Boolean"
  },
  {
    "apiName": "LastActivityDate",
    "dataType": "Date"
  },
  {
    "apiName": "LastModifiedById",
    "dataType": "Reference"
  },
  {
    "apiName": "LastModifiedDate",
    "dataType": "DateTime"
  },
  {
    "apiName": "LastReferencedDate",
    "dataType": "DateTime"
  },
  {
    "apiName": "LastViewedDate",
    "dataType": "DateTime"
  },
  {
    "apiName": "LeadSource",
    "dataType": "Picklist"
  },
  {
    "apiName": "MainCompetitors__c",
    "dataType": "String"
  },
  {
    "apiName": "Name",
    "dataType": "String"
  },
  {
    "apiName": "NextStep",
    "dataType": "String"
  },
  {
    "apiName": "OrderNumber__c",
    "dataType": "String"
  },
  {
    "apiName": "OwnerId",
    "dataType": "Reference"
  },
  {
    "apiName": "Pricebook2Id",
    "dataType": "Reference"
  },
  {
    "apiName": "Probability",
    "dataType": "Percent"
  },
  {
    "apiName": "RecordTypeId",
    "dataType": "Reference"
  },
  {
    "apiName": "Snapshot_Opp__c",
    "dataType": "Url"
  },
  {
    "apiName": "StageName",
    "dataType": "Picklist"
  },
  {
    "apiName": "SystemModstamp",
    "dataType": "DateTime"
  },
  {
    "apiName": "Test_Field__c",
    "dataType": "String"
  },
  {
    "apiName": "TotalOpportunityQuantity",
    "dataType": "Double"
  },
  {
    "apiName": "TrackingNumber__c",
    "dataType": "String"
  },
  {
    "apiName": "Type",
    "dataType": "Picklist"
  }
]

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.