- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2020 04:49 AM
Hi,
Need working example of
html//
<sp-editable-field editable-by-user="data.isLoggedInUsersProfile" table="sys_user" table-id="data.sysUserID" field-model="data.sysUserModel.department"
on-change="onChangeCompany" on-submit="onSubmitCompany"></sp-editable-field>
//cleint controller
var c = this;
var gformDepartment, gformLocation, scopeDepartment, scopeLocation;
$scope.$on("spEditableField.gForm.initialized", function (e, gFormInstance, shadowModel) {
if (shadowModel.name === "department") {
gformDepartment = gFormInstance;
scopeDepartment = e.targetScope;
}
});
$scope.onChangeCompany = function (gform, shadowModel, oldValue, newValue) {
if (oldValue !== newValue) {
$scope.data.sysUserModel.department.value = "";
$scope.data.sysUserModel.department.displayValue = "";
scopeDepartment.shadowModel.value = "";
scopeLocation.shadowModel.value = "";
}
};
$scope.onSubmitCompany = function (gform, shadowModel, oldValue, newValue) {
scopeDepartment.saveForm();
scopeLocation.saveForm();
};
it didnt work ,Could anyone share working code for this ootb tag
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2020 05:54 AM
Hello,
I have changed your code, There is no need to write onChange and on submit methods for this, sp-editable-fields saves the form withouth those methods, just you need to add few lines of code in the server script, please find below code and copy it as it is in your widget and let me know if that works,
HTML -
<div>
<sp-editable-field editable-by-user="data.isLoggedInUsersProfile" table="sys_user" table-id="data.sysUserID" field-model="data.sysUserModel.company"></sp-editable-field>
</div>
Server Script -
(function() {
/* populate the 'data' object */
/* e.g., data.table = $sp.getValue('table'); */
var loggedInSysUserID = gs.getUser().getID();
data.sysUserID = $sp.getParameter("sys_id") || gs.getUserID();
data.isLoggedInUsersProfile = loggedInSysUserID.equals(data.sysUserID);
var sysUserForm = $sp.getForm("sys_user", data.sysUserID);
data.sysUserView = sysUserForm._view;
data.sysUserModel = sysUserForm._fields;
})();
Mark correct if it helps.
Regards
Omkar Mone
ServiceNow Community MVP 2020
ServiceNow Community Leader 2019
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2020 04:58 AM
Hi,
The User Profile widget in OOB instances can help as an example.
<sp-editable-field ng-if="::displayField('sys_user', 'title', true)" editable-by-user="data.isLoggedInUsersProfile" table="sys_user" table-id="data.sysUserID" field-model="data.sysUserModel.title" on-change="titleOnChange" on-submit="titleOnSubmit"></sp-editable-field>
<span ng-if="::(displayField('sys_user', 'company', true) && displayField('sys_user', 'title', true))">at</span>
<span ng-if="::(displayField('sys_user', 'company', true) && !displayField('sys_user', 'title', true))">Works at</span>
<sp-editable-field ng-if="::displayField('sys_user', 'company', true)" editable-by-user="data.isLoggedInUsersProfile" table="sys_user" table-id="data.sysUserID" field-model="data.sysUserModel.company"></sp-editable-field>
</h4>
You can refer to these community links for more examples and helpful links:
Please mark as correct or helpful if this solves your query.
Thanks
DR
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2020 05:05 AM
Hi Manish,
In addition to Debopriya also check the below link.
Mark helpful or correct based on impact.
Regards,
Priyanka A.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2020 05:54 AM
Hello,
I have changed your code, There is no need to write onChange and on submit methods for this, sp-editable-fields saves the form withouth those methods, just you need to add few lines of code in the server script, please find below code and copy it as it is in your widget and let me know if that works,
HTML -
<div>
<sp-editable-field editable-by-user="data.isLoggedInUsersProfile" table="sys_user" table-id="data.sysUserID" field-model="data.sysUserModel.company"></sp-editable-field>
</div>
Server Script -
(function() {
/* populate the 'data' object */
/* e.g., data.table = $sp.getValue('table'); */
var loggedInSysUserID = gs.getUser().getID();
data.sysUserID = $sp.getParameter("sys_id") || gs.getUserID();
data.isLoggedInUsersProfile = loggedInSysUserID.equals(data.sysUserID);
var sysUserForm = $sp.getForm("sys_user", data.sysUserID);
data.sysUserView = sysUserForm._view;
data.sysUserModel = sysUserForm._fields;
})();
Mark correct if it helps.
Regards
Omkar Mone
ServiceNow Community MVP 2020
ServiceNow Community Leader 2019