Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get correct compact layout for sObject #53

Merged
merged 9 commits into from
Nov 15, 2023

Conversation

sfdctaka
Copy link
Contributor

@sfdctaka sfdctaka commented Nov 10, 2023

To get a compact layout for a sObject I am using 2-pass process.

If sObject has compact layout assigned it will have defaultCompactLayoutId assigned. Otherwise, null is assigned and in that case the compact layout is defaults to SYSTEM compact layout.

After determining which compact layout is actually needed only after that a second network request need to be made in order to get the actual layout content.

@sfdctaka sfdctaka changed the title Get compact layout. [DON'T REVIEW YET] Get compact layout. Nov 10, 2023
@sfdctaka sfdctaka changed the title [DON'T REVIEW YET] Get compact layout. Get correct compact layout for sObject Nov 13, 2023
@sfdctaka sfdctaka marked this pull request as ready for review November 13, 2023 05:25
@sfdctaka sfdctaka requested a review from a team as a code owner November 13, 2023 05:25
Comment on lines +22 to +26
export interface CompactLayoutField {
editableForNew: boolean;
editableForUpdate: boolean;
label: string;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dbreese We don't have to use this interface. I added so that I can cast to this before returning to the caller so LWC generating code can have defined fields in the elements of returned array of sObject's fields. Otherwise, you might need to do lot of casting as I do in getCompactLayoutFieldsForSObject.

Comment on lines 75 to 115
public static async getAllCompactLayoutsForSObject(
sObjectName: string
): Promise<any> {
const org = await Org.create();
const conn = org.getConnection();

const result = await conn.request(
`/services/data/v59.0/sobjects/${sObjectName}/describe/compactLayouts`
);

return Promise.resolve(result);
}

public static async getCompactLayoutForSObject(
sObjectName: string,
recordTypeId: string
): Promise<any> {
const org = await Org.create();
const conn = org.getConnection();

const result = await conn.request(
`/services/data/v59.0/sobjects/${sObjectName}/describe/compactLayouts/${recordTypeId}`
);

return Promise.resolve(result);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wrapping conn.request because request method of Connection returns StreamResult. StreamResult is a type from @salesforce/core API and I couldn't find a way to create it or use it. Worse, Typescript was very strict when I tried to create stubs using sinon. So I wrapped them in methods and this way I can wrap the wrapping methods in the unit tests.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like it returns a StreamPromise from some version of the jsforce package—which is just an extension of Promise that has some stream-specific functions added.

But I think it's fine you're doing it this way, in any case. As I mentioned below, I would change the type to Promise<Object>, and coerce that type as necessary.

If we wanted to get fancy at some point, the request method takes a template type, so we could be more definitive in specifying what we want to return, for example:

const result: Promise<OurStronglyTypedCompactLayoutResultType> =
    await conn.request<OurStronglyTypedCompactLayoutResultType>(
            `/services/data/v59.0/sobjects/${sObjectName}/describe/compactLayouts/${recordTypeId}`
    );
return Promise.resolve(result);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm using Promise<Object> for now.

src/utils/orgUtils.ts Outdated Show resolved Hide resolved
src/utils/orgUtils.ts Outdated Show resolved Hide resolved
src/utils/orgUtils.ts Outdated Show resolved Hide resolved
src/utils/orgUtils.ts Outdated Show resolved Hide resolved
src/utils/orgUtils.ts Outdated Show resolved Hide resolved
Copy link
Collaborator

@khawkins khawkins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @sfdctaka ! This is looking like a great first pass! 👍

@sfdctaka sfdctaka merged commit 7632f66 into salesforce:main Nov 15, 2023
8 checks passed
@sfdctaka sfdctaka deleted the compactLayout branch November 15, 2023 00:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants