1

I have the following code

    import { LightningElement, wire, track } from "lwc";
    import { getObjectInfo, getPicklistValues } from "lightning/uiObjectInfoApi";

import CAMPAIGN_OBJECT from "@salesforce/schema/Campaign";
import STATUS_FIELD from "@salesforce/schema/Campaign.Status";

export default class CreateAdventure extends LightningElement {

    @track errorSt;
    @track statusOptions;


    @wire(getObjectInfo, { objectApiName: CAMPAIGN_OBJECT })
    objectInfo;

    @wire(getPicklistValues, { recordTypeId: "$recordTypeId", fieldApiName: STATUS_FIELD })
    statusOptions;

    get recordTypeId() {
        let rtId = '';
        if (this.objectInfo.data) {
            const rtis = this.objectInfo.data.recordTypeInfos;
            rtId = Object.keys(rtis).find(rti => rtis[rti].name === 'Adventure');
        } else if (this.objectInfo.error) {
            rtId = JSON.stringify(this.objectInfo.error);
        }
        return rtId;
    }


    get statusOptionsStr() {
        const sojson = JSON.stringify(this.statusOptions);        
        return sojson;
    }

I added the last function to see what the statusOptions was returning. And even though the recordTypeId is displaying correctly, the statusOptions is an empty object. Can someone help me with this? I have tried many different combinations but I am still not getting the response I am expecting.

2
  • Does the status field have options configured for that specific record type that you are dealing with? – Jayant Das Apr 23 '19 at 12:53
  • At this time all record types in Campaign have the same options. Is this the reason I am getting an empty object? Should I jut try getting the picklist with the default record type? I was trying to code it for future changes (although there is no plan for changes). – Martha Vance Apr 23 '19 at 15:39
0

The values returned from the getPicklistValues() wire adapter are returned in the data or error property as applicable. And that seems to be your issue here as why you are not able to view the values.

If a property is decorated with @wire, the results are returned to the property’s data property or error property.

So as long as your Campaign Record Type has valid Status values, and that you have the correct recordTypeId value being passed in the getPicklsitValues() wire adapter, you will get the Status values without any issue. What you will need to instead change in your existing function is to return the data property. This is how your function should look like:

get statusOptionsStr() {
    const sojson = JSON.stringify(this.statusOptions.data); // notice it now returns data        
    return sojson;
}
8
  • I added the data property, although I thought that the full object would be stringified with data or error. Now it says that sojson is undefined. – Martha Vance Apr 23 '19 at 23:59
  • Are you sure you are passing the correct record type id? Have you tried with the default record type id? Check this answer to see details as how you can pass the default record type id. – Jayant Das Apr 24 '19 at 0:03
  • I am retrieving the recordTypeId through the getRecordTypeInfos wire, and it sends back the correct id. I just tried the getPicklistVaues with a hardcoded id and it works fine. -- i got the id from the console.log(recordTypeId). Why is it not working with '$recordTypeId' but it works if I hardcode it? does my recordTypeId need to have a value parameter? – Martha Vance Apr 24 '19 at 3:03
  • I tried setting a VALUE parameter on recordTypeId, and it doesn't work either. – Martha Vance Apr 24 '19 at 3:25
  • There must be something else going around then. I can confirm that the approach above works perfectly fine as long as the request arguments to the adapter are fine. – Jayant Das Apr 24 '19 at 12:09

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.