Share your saved bundles with other users via portal

MKelly83
Giga Expert

Hi Guys 

Has anyone built functionality were you can share your saved bundles with multiple users via the service portal or can someone point me in the right direction.

We can do this via the native ui by changing the user on the record in the sc_cart table but looking to see if its possible via the portal as well. 

Many Thanks 

Michelle 

 

5 REPLIES 5

Joshua DeSouza
Tera Contributor

Hi Michelle,

 

Did you ever get solution for this? I am looking for something similar. 

 

Thanks,

 

Josh

hi josh, 

yes we cloned the OOTB SC Save Bundle widget and added two new reference fields one for the user (sys_user table) and one for groups (sys_user_group)

find_real_file.png

<form>
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
${Create New Bundle}
</h4>
</div>
<div class="panel-body">
<div ng-if="c.data.error"class="alert alert-danger">
<span>{{c.data.error}}</span>
</div>
<p>${Here, you can save the current contents of your cart as a bundle, which can be restored at any time.}</p>
<p>${To create a new bundle, provide a name for your bundle, select the items you would like to include in the bundle, and press save.}</p>
<div class='form-group'>
<label for="bundlename">
<strong>${Bundle Name}</strong>
</label>
<input id="bundlename" placeholder="New Bundle Name" type='text' class='form-control' placeholder='' autofocus="true" ng-model='c.data.newCartName' maxlength="40">
<label>
<strong>${Share to Users:}</strong>
</label>
<sn-record-picker field="share_to_users"
table="'sys_user'"
display-field="'u_formatted_display_name'"
value-field="'sys_id'"
search-fields="'u_formatted_display_name,first_name,last_name,user_name'"
page-size="15"
default-query="'active=true^u_formatted_display_nameISNOTEMPTY'"
multiple="true"></sn-record-picker>
<label>
<strong>${Share to Groups:}</strong>
</label>
<sn-record-picker field="share_to_groups"
table="'sys_user_group'"
display-field="'name'"
value-field="'sys_id'"
search-fields="'name'"
page-size="15"
default-query="'nameISNOTEMPTY^active=true'"
multiple="true"></sn-record-picker>
</div>

<table id="cart" class="table table-hover table-condensed">
<tbody>
<tr class="sr-only">
<th>${Include Item}</th>
<th>${Product}</th>
<th>${Price}</th>
<th>${Quantity}</th>
<th>${Subtotal}</th>
</tr>
<tr ng-repeat="item in c.data.cartItems track by item.sys_id | orderBy: 'order'">
<td>
<input type="checkbox" ng-model="item.selected" aria-label="${Include} {{item.name}} ${in bundle}">
</td>
<td>
<div class="row">
<div class="col-sm-2 hidden-sm hidden-xs"><img ng-show="item.picture" ng-src="{{item.picture}}" alt="{{item.name}}" class="img-responsive item-image"/></div>
<div class="col-sm-10 item">
<h4 class="nomargin item_name">{{item.name}}</h4>
<p class="hidden-sm hidden-xs">{{item.short_description}}</p>
</div>
</div>
</td>
<td>
<strong>{{item.display_price}}</strong>
<div ng-show="item.recurring_price > 0">+ {{item.display_recurring_price}} {{item.recurring_frequency_display}}</div>
</td>
<td>
<span aria-label="${Quantity} {{item.quantity}}" ng-if="item.show_quantity">{{item.quantity}}</span>
<span aria-label="${Quantity not applicable}" ng-if="!item.show_quantity">--</span>
</td>
<td>
<strong aria-label="${Subtotal price} {{item.subtotal_price}}">{{item.subtotal_price}}</strong>
<div aria-label="${Recurring subtotal price} {{item.recurring_subtotal_price}}" ng-show="item.recurring_subtotal > 0">+ {{item.recurring_subtotal_price}} {{item.recurring_frequency_display}}</div>
</td>
</tr>
</tbody>
</table>
</div>
<div class="panel-footer">
<button type='submit'
class='btn btn-primary pull-right'
ng-click='c.saveCart()'
ng-disabled='!c.enableSave()'
tabindex="0">${Save}</button>
<i class="clearfix"></i>
</div>
</div>
</form>
 
.item_name {
margin-top: 0px;
margin-bottom: 0px;
}
.item {
padding-left:20px;
}
 
(function() {
data.bundleSaveSuccessMsg = gs.getMessage("Bundle has been successfully created");

data.share_to_users = {
names: '',
sys_ids: ''
};

data.share_to_groups = {
names: '',
sys_ids: ''
};

var userID = gs.getUser().getID();
var cart = new SPCart("DEFAULT", userID);
data.cartItems = cart.getItems();
data.cartItems.map(function(item) {
item.selected = true;
});

if (input) {
//Check to make sure no carts with this name already exist
var cartGR = new GlideRecord("sc_cart");
cartGR.addQuery("user", userID);
cartGR.addQuery("name", input.newCartName);
cartGR.query();
if (cartGR.next()) {
data.error = gs.getMessage("A bundle of that name already exists - please select another.");
data.cartItems = input.cartItems;
return;
}

var includedItemsArr = input.cartItems.filter(function(item) {
return item.selected;
}).map(function(item) {
return item.sys_id;
});

// Modification for sharing to users
// Orginal code for current user
var newCart = new SPCart(input.newCartName, userID);
newCart.setHidden(false);
newCart.loadCart(cart.getCartID(), includedItemsArr);

// Additional groups
var usersFromGrp = [];
if (input.share_to_groups.sys_ids) {
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('group', input.share_to_groups.sys_ids);
gr.query();
while (gr.next())
usersFromGrp.push(gr.user + '');
}
// Additional users
var usersArr = input.share_to_users.sys_ids.split(',');
var allUsers = usersFromGrp.concat(usersArr);
// ge only unique users
var uniqueUsers = allUsers.filter(function(value, index, self) {
return (self.indexOf(value) === index) && (value !== gs.getUserID());
});

if (uniqueUsers.length)
for (var i =0; i < uniqueUsers.length; i++)
if (uniqueUsers[i] && uniqueUsers[i] !== gs.getUserID()) {
var qnewCart = new SPCart(input.newCartName + ' [Shared by ' + gs.getUserDisplayName() + ']', uniqueUsers[i]);
qnewCart.loadCart(cart.getCartID(), includedItemsArr);
qnewCart.setHidden(false);
}
// share bundles for unique users
return;
}

})();
 
 
function(spAriaUtil, $rootScope, $scope) {
/* widget controller */
var c = this;

c.saveCart = function() {
c.data.error = null;
c.server.update().then(function() {
if (!c.data.error) {
$rootScope.$broadcast("$sp.service_catalog.cart.save_cart");
spAriaUtil.sendLiveMessage(c.data.bundleSaveSuccessMsg);
$rootScope.hasSavedCarts = true;
}
});
}

c.enableSave = function() {
return c.data.newCartName &&
c.data.newCartName.length > 0 &&
c.data.cartItems.filter(function(item){
return item.selected;
}).length > 0;
}

// Share to Users field
$scope.share_to_users = {
displayValue: c.data.share_to_users.names,
value: c.data.share_to_users.sys_ids,
name: 'share_to_users'
};

$scope.$on("field.change", function(evt, parms) {
if (parms.field.name == 'share_to_users') {
c.data.share_to_users.names = parms.displayValue;
c.data.share_to_users.sys_ids = parms.newValue;
}

//c.server.update().then(function(response) {
//spUtil.update($scope);
//})
});

$scope.share_to_groups = {
displayValue: c.data.share_to_groups.names,
value: c.data.share_to_groups.sys_ids,
name: 'share_to_groups'
};

$scope.$on("field.change", function(evt, parms) {
if (parms.field.name == 'share_to_groups') {
c.data.share_to_groups.names = parms.displayValue;
c.data.share_to_groups.sys_ids = parms.newValue;
}
});
}
 
Let me know if you need any additional help. 
 
 
 
 

Hello ,

I have same requirement . I want to use save bundle for multiple users. Can you elaborate more on this.

pvn
Tera Contributor

Hi @samsam1 
Were you able to achieve this requirement?