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

Fixed deploying objects and fields to orgs with namespace #654

Merged
merged 1 commit into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const CreateNewObjectModal: FunctionComponent<CreateNewObjectModalProps>
const modalBodyRef = useRef<HTMLDivElement>(null);
const tabsRef = useRef<TabsRef>();

const apiName = useRecoilValue(fromCreateObjectState.apiNameState);
const apiNameWithoutNamespace = useRecoilValue(fromCreateObjectState.apiNameState);
const createTab = useRecoilValue(fromCreateObjectState.createTabState);
const selectedTabIcon = useRecoilValue(fromCreateObjectState.selectedTabIconState);
const objectPermissions = useRecoilValue(fromCreateObjectState.objectPermissionsState);
Expand All @@ -40,6 +40,8 @@ export const CreateNewObjectModal: FunctionComponent<CreateNewObjectModalProps>
const payload = useRecoilValue(fromCreateObjectState.payloadSelector);
const isValid = useRecoilValue(fromCreateObjectState.isFormValid);

const apiName = `${selectedOrg.orgNamespacePrefix ? `${selectedOrg.orgNamespacePrefix}__` : ''}${apiNameWithoutNamespace}`;

const { results, deployMetadata, status, errorMessage, hasError, loading, permissionRecordResults } = useCreateObject({
apiVersion: defaultApiVersion,
serverUrl,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export const payloadSelector = selector<CreateObjectPayload>({
allowInChatterGroups: get(allowInChatterGroupsState),
compactLayoutAssignment: 'SYSTEM',
deploymentStatus: 'Deployed',
description: get(descriptionState),
enableActivities: get(allowActivitiesState),
enableBulkApi: get(allowSharingBulkStreamingState),
enableEnhancedLookup: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface CreateObjectPayload {
allowInChatterGroups: boolean;
compactLayoutAssignment: 'SYSTEM';
deploymentStatus: 'Deployed';
description?: string;
enableActivities: boolean;
enableBulkApi: boolean;
enableEnhancedLookup: false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { queryAll, sobjectOperation } from '@jetstream/shared/data';
import { REGEX, splitArrayToMaxSize } from '@jetstream/shared/utils';
import { ObjectPermissionRecordInsert, RecordResult, SalesforceOrgUi, TabPermissionRecordInsert } from '@jetstream/types';
import { Maybe, ObjectPermissionRecordInsert, RecordResult, SalesforceOrgUi, TabPermissionRecordInsert } from '@jetstream/types';
import JSZip from 'jszip';
import partition from 'lodash/partition';
import { composeQuery, getField } from 'soql-parser-js';
Expand All @@ -24,7 +24,11 @@ export function generateApiNameFromLabel(value: string) {
return apiNameLabel ? `${apiNameLabel}__c` : '';
}

export function getObjectAndTabPermissionRecords({ apiName, createTab, profiles, permissionSets, objectPermissions }: CreateFieldParams) {
export function getObjectAndTabPermissionRecords(
{ apiName: apiNameWithoutNamespace, createTab, profiles, permissionSets, objectPermissions }: CreateFieldParams,
orgNamespacePrefix?: Maybe<string>
) {
const apiName = orgNamespacePrefix ? `${orgNamespacePrefix}__${apiNameWithoutNamespace}` : apiNameWithoutNamespace;
const _objectPermissions: ObjectPermissionRecordInsert[] = [];
const tabPermissions: TabPermissionRecordInsert[] = [];

Expand Down Expand Up @@ -162,6 +166,7 @@ export function getObjectXml(payload: CreateObjectPayload) {
<allowInChatterGroups>${payload.allowInChatterGroups}</allowInChatterGroups>
<compactLayoutAssignment>${payload.compactLayoutAssignment}</compactLayoutAssignment>
<deploymentStatus>${payload.deploymentStatus}</deploymentStatus>
<description>${payload.description || ''}</description>
<enableActivities>${payload.enableActivities}</enableActivities>
<enableBulkApi>${payload.enableBulkApi}</enableBulkApi>
<enableEnhancedLookup>${payload.enableEnhancedLookup}</enableEnhancedLookup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ export default function useCreateObject({ apiVersion, serverUrl, selectedOrg }:
async (data: CreateFieldParams) => {
try {
const { apiName } = data;
const { orgNamespacePrefix } = selectedOrg;
logger.info('[deployMetadata]', data);
setStatus('LOADING_METADATA');
setPermissionRecordResults(null);
const permissionRecords = getObjectAndTabPermissionRecords(data);
const permissionRecords = getObjectAndTabPermissionRecords(data, orgNamespacePrefix);
const file = await getMetadataPackage(apiVersion, data);

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ import { css } from '@emotion/react';
import { logger } from '@jetstream/shared/client-logger';
import { useNonInitialEffect } from '@jetstream/shared/ui-utils';
import { unSanitizeXml } from '@jetstream/shared/utils';
import { SplitWrapper as Split } from '@jetstream/splitjs';
import { FileExtAllTypes, ListMetadataResult, MapOf, SalesforceOrgUi } from '@jetstream/types';
import { AutoFullHeightContainer, FileDownloadModal, Modal, Spinner, TreeItems } from '@jetstream/ui';
import Editor, { DiffEditor } from '@monaco-editor/react';
import type { editor } from 'monaco-editor';
import { Fragment, FunctionComponent, useCallback, useEffect, useRef, useState } from 'react';
import { SplitWrapper as Split } from '@jetstream/splitjs';
import { useRecoilState } from 'recoil';
import { applicationCookieState } from '../../../app-state';
import * as fromJetstreamEvents from '../../core/jetstream-events';
import { useViewOrCompareMetadata } from './useViewOrCompareMetadata';
import ViewOrCompareMetadataEditorSummary from './ViewOrCompareMetadataEditorSummary';
import ViewOrCompareMetadataModalFooter from './ViewOrCompareMetadataModalFooter';
import ViewOrCompareMetadataSidebar from './ViewOrCompareMetadataSidebar';
import { useViewOrCompareMetadata } from './useViewOrCompareMetadata';
import { EditorType, FileItemMetadata, OrgType } from './viewOrCompareMetadataTypes';
import { generateExport, getEditorLanguage } from './viewOrCompareMetadataUtils';

Expand Down Expand Up @@ -157,6 +157,11 @@ export const ViewOrCompareMetadataModal: FunctionComponent<ViewOrCompareMetadata
fetchMetadata(org, 'TARGET');
}

function handleReload() {
sourceOrg && fetchMetadata(sourceOrg, 'SOURCE');
targetOrg && fetchMetadata(targetOrg, 'TARGET');
}

async function handleDownload(which: OrgType) {
const fileNameParts = ['metadata-package'];
if (which === 'SOURCE' && sourceResults) {
Expand Down Expand Up @@ -234,6 +239,7 @@ export const ViewOrCompareMetadataModal: FunctionComponent<ViewOrCompareMetadata
sourceLastChecked={sourceLastChecked}
targetLoading={targetLoading}
targetLastChecked={targetLastChecked}
reloadMetadata={handleReload}
onDownloadPackage={handleDownload}
onExportSummary={handleExport}
onClose={onClose}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface ViewOrCompareMetadataModalFooterProps {
sourceLastChecked: Date | null;
targetLoading: boolean;
targetLastChecked: Date | null;
reloadMetadata?: () => void;
onDownloadPackage: (which: OrgType) => void;
onExportSummary: () => void;
onClose: () => void;
Expand All @@ -23,6 +24,7 @@ export const ViewOrCompareMetadataModalFooter: FunctionComponent<ViewOrCompareMe
sourceLastChecked,
targetLoading,
targetLastChecked,
reloadMetadata,
onDownloadPackage,
onExportSummary,
onClose,
Expand All @@ -31,6 +33,11 @@ export const ViewOrCompareMetadataModalFooter: FunctionComponent<ViewOrCompareMe
return (
<Grid align="spread" verticalAlign="center">
<div>
{reloadMetadata && !sourceLoading && !targetLoading && (
<button className="slds-button slds-button_neutral" onClick={() => reloadMetadata()}>
Reload Metadata
</button>
)}
{sourceLoading && (
<div className="slds-truncate" title={`last checked at ${sourceLastChecked?.toLocaleTimeString() || '<waiting>'}`}>
Loading metadata from Salesforce {sourceLastChecked && <span>- last checked at {sourceLastChecked.toLocaleTimeString()}</span>}
Expand Down
4 changes: 3 additions & 1 deletion apps/jetstream/src/app/components/orgs/AddOrg.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ export const AddOrg: FunctionComponent<AddOrgProps> = ({ className, label = 'Add
className="slds-input"
placeholder="org-domain"
value={customUrl}
onChange={(event) => setCustomUrl(event.target.value)}
onChange={(event) =>
setCustomUrl((prevValue) => (event.target.value || '').replaceAll(/(https:\/\/)|(\.my\.salesforce\.com)/g, ''))
}
/>
</Input>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { logger } from '@jetstream/shared/client-logger';
import { describeGlobal, genericRequest, queryAllFromList, queryWithCache } from '@jetstream/shared/data';
import { REGEX, ensureBoolean, splitArrayToMaxSize } from '@jetstream/shared/utils';
import { CompositeResponse, GlobalValueSetRequest, MapOf, SalesforceOrgUi, ToolingApiResponse } from '@jetstream/types';
import { CompositeResponse, GlobalValueSetRequest, MapOf, Maybe, SalesforceOrgUi, ToolingApiResponse } from '@jetstream/types';
import type { DescribeGlobalSObjectResult } from 'jsforce';
import isBoolean from 'lodash/isBoolean';
import isNil from 'lodash/isNil';
Expand Down Expand Up @@ -800,11 +800,11 @@ export function isFieldValues(input: FieldValues | FieldDefinitionMetadata): inp
return !isNil((input as any)?._key);
}

export function preparePayload(sobjects: string[], rows: FieldValues[]): FieldDefinitionMetadata[] {
return rows.flatMap((row) => sobjects.map((sobject) => prepareFieldPayload(sobject, row)));
export function preparePayload(sobjects: string[], rows: FieldValues[], orgNamespace?: Maybe<string>): FieldDefinitionMetadata[] {
return rows.flatMap((row) => sobjects.map((sobject) => prepareFieldPayload(sobject, row, orgNamespace)));
}

function prepareFieldPayload(sobject: string, fieldValues: FieldValues): FieldDefinitionMetadata {
function prepareFieldPayload(sobject: string, fieldValues: FieldValues, orgNamespace?: Maybe<string>): FieldDefinitionMetadata {
const fieldMetadata: FieldDefinitionMetadata = [
...baseFields,
...fieldTypeDependencies[fieldValues.type.value as FieldDefinitionType],
Expand All @@ -816,7 +816,8 @@ function prepareFieldPayload(sobject: string, fieldValues: FieldValues): FieldDe
return output;
}, {});
// prefix with object
fieldMetadata.fullName = `${sobject}.${fieldMetadata.fullName}__c`;
const fieldApiName = orgNamespace ? `${orgNamespace}__${fieldMetadata.fullName}__c` : `${fieldMetadata.fullName}__c`;
fieldMetadata.fullName = `${sobject}.${fieldApiName}`;

if (fieldValues.type.value === 'Formula') {
fieldMetadata.type = fieldValues.secondaryType.value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export default function useCreateFields({
(rows: FieldValues[]) => {
if (rows?.length && sObjects?.length) {
try {
const payload: CreateFieldsResults[] = preparePayload(sObjects, rows).map(
const payload: CreateFieldsResults[] = preparePayload(sObjects, rows, selectedOrg.orgNamespacePrefix).map(
(field): CreateFieldsResults => ({
key: `${(field.fullName as string).replace('.', '_').replace(REGEX.CONSECUTIVE_UNDERSCORES, '_')}`,
label: field.fullName,
Expand All @@ -127,7 +127,7 @@ export default function useCreateFields({
}
return false;
},
[rollbar, sObjects]
[rollbar, sObjects, selectedOrg.orgNamespacePrefix]
);

/**
Expand Down