Deliver to (shipping address) in Service portal cart

venkatkk
Tera Contributor

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'

};

find_real_file.png

1 ACCEPTED SOLUTION

Roffy
Tera Contributor

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!


View solution in original post

7 REPLIES 7

paulfiegl
Tera Contributor

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.

 

find_real_file.png

 

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;

   }

}

})();

 

 

Roffy
Tera Contributor

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/.

paulfiegl
Tera Contributor

Rosstowle - this worked out perfectly for me, I really appreciate your help along with the cheat sheet.

Thank you,

Paul