How to add fields to the shopping cart Service Portal Helsinki/Istanbul

tommyjensen
Giga Expert

I am trying to add new fields to the shopping cart checkout screen.

Shopping cart.png

I have been able to add a normal text field by modifying the server script in the widget SC Shopping Cart. I set the field directly on the generated request and then do an update.

However this approach has the problem that some of the fields I need to add are mandatory so the request cannot be created without them filled in.

So the bottomline is that I need to modify the following code the the script include "cart"

  placeOrder: function() {

              var req = new GlideappRequestNew();

              var rc = req.copyCart(this.cartName, this.userID);

              this.clearCart();

              return rc;

      }

More specifically the function copyCart. But how do I do that, there is not scriptinclude named GlideappRequestNew.

ps I have searched and found some old threads but all point to a solution from 2010 so do not take the new Service Portal / widgets design into account.

1 ACCEPTED SOLUTION

If you want some code as inspiration here is what I have



Client script checkout button:


c.checkout = function() {


        //Don't checkout if cart is empty (prevent multiple click on submit button)


        if (c.data.cart.items.length == 0)


        return;



        //Run check on required fields, just to be sure


        c.data.mandatoryMessage = requiredFields();


        if (c.data.mandatoryMessage > 0)


        return;



        c.data.checkedOut = "true";



        showLoading();


        c.data.checkout = true;



        c.server.update().then(function(data) {


                  c.data.checkout = undefined;


                  var parms = {};


                  parms.id = c.data.success_page;


                  parms.table = "sc_request";


                  parms.sys_id = c.data.req;


                  $location.search(parms);


        })


}


Server side part


// Checkout


if (input && input.checkout && input.cart.items.length > 0) {


//Save cart values


saveCartValues(cart);



var req = cart.checkout();


data.req = req.getUniqueValue();


}



function saveCartValues(cart) { //Transfer PC, Asset ect. to request


var cartValues = "<root>";


cartValues += "<u_wait_for_ritm_generation>"+ cart.cartGR.u_wait_for_ritm_generation + "</u_wait_for_ritm_generation>";



if (input.validate.pc_visible)


cartValues += "<u_user_pc>"+ input.cart.user_pc + "</u_user_pc>";




if (input.showIncident && input.cart.incident)


cartValues += "<u_created_for_incident>" + input.cart.incident + "</u_created_for_incident>";




if (input.mandatoryReason)


cartValues += "<u_reason>"+ input.cart.reason + "</u_reason>";




if (input.showDelivery) {


var str = "Leveringsmetode: " + input.cart.delivery_method;


if (input.cart.delivery_information)


str+= "\nLeveringsinformation:\n" + input.cart.delivery_information


cartValues += "<delivery_address>" + str + "</delivery_address>";


}




if (input.showAccessBypass && input.cart.change_request) {


cartValues += "<u_created_for_change>"+ input.cart.change_request + "</u_created_for_change>";


if (input.cart.bypassApproval != undefined)


cartValues += "<u_bypass_approvals>"+ input.cart.bypassApproval + "</u_bypass_approvals>";


if (input.cart.bypassNotification != undefined)


cartValues += "<u_bypass_notifications>"+ input.cart.bypassNotification + "</u_bypass_notifications>";


}




cartValues += "</root>";



var hints = "<hints><entry key='sysparm_processing_hint' value='setfield:request.u_cart_values="+escape(cartValues)+"'/></hints>";



var cart_item = new GlideRecord('sc_cart_item');


cart_item.addQuery('active',true);


cart_item.addQuery('cart', cart.getCartID());


cart_item.query();


if(cart_item.next()) {


cart_item.hints = hints;


cart_item.update();


}


}


A business rule on the request will split the values from the xml into the actual fields


//Translates the XML in the cart values field, which are inserted when checking out the cart. The values are inserted into the appropriate fields


readCartValues();




function readCartValues(){


      //Get the values to populate and split into name/value pairs




        var xmlString = unescape(current.u_cart_values);



        var helper = new XMLHelper(xmlString);        


        var obj = helper.toObject();


     


        for(var label in obj){


                  if(JSUtil.notNil(obj[label])){


                            var textContent = obj[label];


                            current.setValue(label, textContent);


                  }


        }


}


View solution in original post

10 REPLIES 10

larstange
Mega Sage

Hi



The requiredFields function is defined in the client script. I have my logic here to check the fields in the form if they have to be mandatory and if so, if they have been filled out.


Can you provide the client script requiredFields();   I am trying to imitate the same functionality


Here it is - not sure if it will make sense to you, as you dont the the rest of the widget code




function requiredFields() {


var hasRequired = false;


var answer = [];


var message = {};


//if (!firstLoad) { //don't validate the page when it is loading


message.text = '${Fill in: Requested for}';


message.type = c.data.cart.requested_for ? "success" : "danger";


answer.push(JSON.parse(JSON.stringify(message))); //We cant insert the 'message' object directly as it will be referenced


if (c.data.validate.pc_visible) {


message.text = '${Fill in: Select the computer...}';


message.type = c.data.cart.user_pc ? 'success' : 'danger';


answer.push(JSON.parse(JSON.stringify(message)));


}


if (c.data.mandatoryReason) {


message.text = '${Fill in: Describe the reason why you need this...}';


message.type = c.data.cart.reason ? 'success' : 'danger';


answer.push(JSON.parse(JSON.stringify(message)));


}


if (c.data.showDelivery) {


message.text = '${Choose: Delivey method/place}';


message.type = c.data.cart.delivery_method ? 'success' : 'danger';


answer.push(JSON.parse(JSON.stringify(message)));



if (c.data.cart.delivery_method == "Post Danmark") {


message.text = '${Fill in: Delivery information}';


message.type = !c.data.cart.delivery_information ? 'danger' : 'success';


answer.push(JSON.parse(JSON.stringify(message)));


}


}


if (c.data.showAccessBypass && (c.data.cart.change_request || c.data.cart.bypassApproval || c.data.cart.bypassNotification)) {


message.text = 'Vælg mindst en af de to bypass muligheder';


message.type = c.data.cart.change_request && !c.data.cart.bypassApproval && !c.data.cart.bypassNotification ? "danger" : "success";


answer.push(JSON.parse(JSON.stringify(message)));



message.text = 'Angiv den change som ligger til grund for de valgte tilsidesættelser';


message.type = !c.data.cart.change_request && (c.data.cart.bypassApproval || c.data.cart.bypassNotification) ? "danger" : "success";


answer.push(JSON.parse(JSON.stringify(message)));


}


//}


//Check if any of the checks found a required field not filled out


if (search('danger','type',answer))


hasRequired = true;



c.data.hasRequired = hasRequired;


return answer;


}


Community Alums
Not applicable

HI Lars,


I have few fields set mandatory on sc_request (like request type, short description). How can i pass those values while submitting catalog item? i checked your sample code but confused with SC Shopping Cart widget, placeOrder function in cart script include. Could you please help on this?


Harish KM
Kilo Patron
Kilo Patron

Hi @larstange Could you please explain me the approach you had implemented. I did the same way not able to push the values.


Regards
Harish