- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-25-2017 10:25 PM
Client want to auto populate Deliver to address for the user. Currently it is empty in Service Portal.
This is working from navpage , Isssue only with Service Portal.
I tried to modify SC Shopping Cart Widget, it is working when we change requested for BUT on load it is not filling Deliver To field
Pls help
When i tried with the below script it is NOT working
c.deliverTo = {
displayValue: c.data.cart.requestor_location_display_name,
value: c.data.cart.requestor_location,
name: 'requestor_location'
};
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-12-2017 10:18 AM
I've been working on this today as it's a requirement of ours too.
This is my working code. It loads the logged in user's delivery address when they enter the cart. If the 'Requested for' field changes, the new user's delivery address is pulled through.
SC Shopping Cart
Client Script:
// Insert below code after line 106 - c.requestedFor = {...};
c.deliverTo = c.data.deliverTo;
$scope.$on("field.change", function(evt, parms) {
if (parms.field.name == 'requested_for') {
c.data.cart.requested_for = parms.newValue;
/* This is new code */
c.server.get({
action: "update_deliver_to",
name: parms.newValue
}).then(function(response) {
c.deliverTo = response.data.deliverTo;
});
/* End of new code */
}
});
Server Script:
// Insert below code after line 7 - var cart = new SPCart(input.cartName, userID);
data.deliverTo = getDeliveryAddress(userID);
if (input && input.action === "update_deliver_to") {
data.deliverTo = getDeliveryAddress(input.name);
}
// Insert below code after line 62 - data.cartItems = cart.getItems();
function getDeliveryAddress(requestedFor) {
var deliveryAddress = '';
var userObject = gs.getUser().getUserByID(requestedFor);
var location = userObject.getLocation();
var cmnLocationGR = new GlideRecord('cmn_location');
cmnLocationGR.addQuery('sys_id', location);
cmnLocationGR.query();
if (cmnLocationGR.next()) {
locationAddress = {
name: cmnLocationGR.getValue('name'),
street: cmnLocationGR.getValue('street'),
city: cmnLocationGR.getValue('city'),
postcode: cmnLocationGR.getValue('zip')
};
if (locationAddress.name && locationAddress.name != '')
deliveryAddress = locationAddress.name + '\n';
if (locationAddress.street && locationAddress.street != '')
deliveryAddress += locationAddress.street + '\n';
if (locationAddress.city && locationAddress.city != '')
deliveryAddress += locationAddress.city + '\n';
if (locationAddress.postcode && locationAddress.postcode != '')
deliveryAddress += locationAddress.postcode;
return deliveryAddress;
}
}
Create a new Angular ng-template by duplicating large_shopping_cart-html.
custom_large_shopping_cart.html:
<!-- Amend line 64 so it reads: -->
<sn-record-picker field="c.requestedFor" table="'sys_user'" display-field="'name'" value-field="'sys_id'" search-fields="'name'" page-size="100" ng-change="c.updateRequestedFor(sys_id)" ></sn-record-picker>
Make the new template the default:
Service Portal Designer > Shopping Cart > Edit 'SC Shopping Cart' widget > Menu > Open in platform > Additional options, JSON format:
{
"cartTemplate": "custom_large_shopping_cart.html"
}
And unless I'm forgetting something, that's it!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-08-2019 12:10 PM
Would any one be able to provide me some feedback on how I can also display the floor field from the user table using the modified script below? It displays undefined for the floor as shown in the screen shot below. I have tried a couple different combinations only ending with the Any help or direction would be greatly appreciated.
Server Script:
function getDeliveryAddress(requestedFor) {
var deliveryAddress = '';
var userObject = gs.getUser().getUserByID(requestedFor);
var location = userObject.getLocation();
var cmnLocationGR = new GlideRecord('cmn_location');
cmnLocationGR.addQuery('sys_id', location);
cmnLocationGR.query();
if (cmnLocationGR.next()) {
locationAddress = {
name: cmnLocationGR.getValue('name'),
street: cmnLocationGR.getValue('street'),
city: cmnLocationGR.getValue('city'),
postcode: cmnLocationGR.getValue('zip'),
//floor: userObject.getValue('u_location_floor')
floor: userObject.u_location_floor.getValue('floor')
};
if (locationAddress.name && locationAddress.name != '' )
deliveryAddress += locationAddress.name + '\n';
if (locationAddress.street && locationAddress.street || locationAddress.city && locationAddress.city || locationAddress.postcode && locationAddress.postcode != '')
deliveryAddress += locationAddress.street + ', ' + locationAddress.city + ' ' + locationAddress.postcode + ' Floor: ' + locationAddress.floor +'\n';
return deliveryAddress;
}
}
})();

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-09-2019 04:58 AM
If your custom 'u_location_floor' field is stored in the User [sys_user] table, you should be able to use:
gs.getUser().getUserByID(requestedFor).getRecord().getValue('u_location_floor');
or rather use the 'userObject' variable from earlier in your code:
userObject.getRecord().getValue('u_location_floor');
A nifty cheat sheet well worth printing or bookmarking is https://www.servicenowguru.com/scripting/user-object-cheat-sheet/.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-09-2019 05:12 AM
Rosstowle - this worked out perfectly for me, I really appreciate your help along with the cheat sheet.
Thank you,
Paul