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

venkatkk
Tera Contributor

any help on this?


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!


venkatkk
Tera Contributor

That's great...Solves my issue. Thanks a ton.


SRJ
Tera Contributor

HI , could you please help me to the below requirement please

line manager cant be an approver(vice-versa). tried comparing the two values in client script but its not working 😞 .

[we have done : Line manager is auto populating based on the requested for's User profile data and user can manually change the line manager as well]