Skip to content

Instantly share code, notes, and snippets.

@dhaniksahni
Created December 5, 2019 19:52
Show Gist options
  • Save dhaniksahni/8c62db5527d05f98ea83fc5a151e8b76 to your computer and use it in GitHub Desktop.
Save dhaniksahni/8c62db5527d05f98ea83fc5a151e8b76 to your computer and use it in GitHub Desktop.
/* parent.css */
divcenter {
text-align: center;
}
.divSize
{
height: 400px;
}
<template>
<div>
<lightning-card class="divSize">
<div style="text-align: center;">SalesforceCodex Field's Access Explorer</div>
<lightning-layout multiple-rows>
<lightning-layout-item size="2" padding="around-small">
<lightning-combobox
name="Users"
label="Users"
value=""
placeholder="Select User"
options={users}
onchange={handleUserChange}
required
>
</lightning-combobox>
</lightning-layout-item>
<lightning-layout-item size="2" padding="around-small">
<lightning-combobox name="objectName"
label="Object"
value=""
placeholder="Select Object"
options={objects}
onchange={handleObjectChange}
required></lightning-combobox>
</lightning-layout-item>
<lightning-layout-item size="2" padding="around-small">
<lightning-combobox
name="Fields"
label="Fields"
value=""
placeholder="Select Field"
options={fields}
onchange={handleFieldChange}
></lightning-combobox>
</lightning-layout-item>
<lightning-layout-item size="4" padding="around-small">
<div class="slds-m-around--medium">
<lightning-button label="Get Users Access" title="Get Users Access" onclick={handleClick}>
</lightning-button>
</div>
</lightning-layout-item>
</lightning-layout>
</lightning-card>
<lightning-card title="Field Access">
<br />
<div style="width: auto;">
<lightning-datatable data={data}
columns={columns}
key-field="Id">
</lightning-datatable>
</div>
</lightning-card>
</div>
</template>
import { LightningElement, track, wire} from 'lwc';
import getObjects from '@salesforce/apex/FieldExplorerController.getObjects';
import getFields from '@salesforce/apex/FieldExplorerController.getFields';
import getObjectAccess from '@salesforce/apex/FieldExplorerController.getObjectAccess';
import getFieldsAccess from '@salesforce/apex/FieldExplorerController.getFieldsAccess';
import getOrgUsers from '@salesforce/apex/FieldExplorerController.getOrgUsers';
const columnsDefs = [
{
label: 'EntityDefinitionId',
fieldName: 'EntityDefinitionId',
type: 'text',
sortable: true
},
{
label: 'FieldDefinitionId',
fieldName: 'FieldDefinitionId',
type: 'text',
sortable: true
},
{
label: 'FieldName',
fieldName: 'FieldName',
type: 'text',
sortable: true
},
{
label: 'IsAccessible',
fieldName: 'IsAccessible',
type: 'boolean',
sortable: true
},
{
label: 'IsUpdatable',
fieldName: 'IsUpdatable',
type: 'boolean',
sortable: true
},
{
label: 'IsCreatable',
fieldName: 'IsCreatable',
type: 'boolean',
sortable: true
}
];
export default class FieldAccessExplorer extends LightningElement {
@track objects = [];
@track fields = [];
@track users = [];
@track field='';
@track objName='';
@track user='';
@track data;
@track columns = columnsDefs;
connectedCallback()
{
getOrgUsers()
.then(result => {
this.dataArray = result;
let tempArray = [];
this.dataArray.forEach(function (element) {
var option=
{
label:element.Name,
value:element.Id
};
tempArray.push(option);
});
this.users=tempArray;
console.log(this.users);
})
.catch(error => {
this.error = error;
});
}
@wire(getObjects)
wiredMethod({ error, data }) {
if (data) {
this.dataArray = data;
let tempArray = [];
this.dataArray.forEach(function (element) {
var option=
{
label:element,
value:element
};
tempArray.push(option);
});
this.objects=tempArray;
} else if (error) {
this.error = error;
}
}
handleObjectChange(event)
{
const selectedOption = event.detail.value;
this.objName=selectedOption;
getFields({ objectName: selectedOption})
.then(result => {
this.dataArray = result;
let tempArray = [];
let option=
{
label:'Please Select',
value:''
};
tempArray.push(option);
this.dataArray.forEach(function (element) {
option=
{
label:element.Label,
value:element.Name
};
tempArray.push(option);
});
this.fields=tempArray;
})
.catch(error => {
this.error = error;
});
}
handleFieldChange(event){
const selectedOption = event.detail.value;
this.field=selectedOption;
}
handleUserChange(event){
const selectedOption = event.detail.value;
this.user=selectedOption;
}
handleClick(){
if(this.field==='')
{
getObjectAccess({ objectName: this.objName,userId:this.user})
.then(result => {
this.data=result;
})
.catch(error => {
console.log('Error'+ JSON.stringify(error));
this.error = error;
});
}
else
{
getFieldsAccess({ objectName: this.objName,fieldName:this.field,userId:this.user})
.then(result => {
this.data=result;
})
.catch(error => {
console.log('Error'+ JSON.stringify(error));
this.error = error;
});
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata" fqn="fieldAccessExplorer">
<apiVersion>47.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightningCommunity__Page</target>
<target>lightningCommunity__Default</target>
<target>lightning__RecordPage</target>
<target>lightning__AppPage</target>
<target>lightning__HomePage</target>
</targets>
</LightningComponentBundle>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment