```ModelsInit.context.setSessionObject('variableSe...
# suitecommerce
l
Copy code
ModelsInit.context.setSessionObject('variableSet', true);
var session = ModelsInit.context.getSessionObject('variableSet');
m
Getting.
Copy code
Uncaught Error: Module "SC.Models.Init" on "<http://localhost:7777/SC.Models.Init.js>" not found
Can you not load it into a View?
l
Actually, it's a Backend model, If you wan to set session in SuiteScript file you can load the Models.Init and use setSessionObject.
this won't work in Front end code.
1
m
Is
SC.Models.Init
only available for suitescript 1.0 backends? I seem to be very confused on where I can load it.
l
@Marvin , Could you please let me know where you would to set the session? in SCA? or in Backend SuiteScript file? What you have done so far? if you would like to set the session in SuiteScript file there is N/runtime module available. But if you would like to set in in SCA then there is 'Models.Init' module available Models.Init is also called as commerce API.
This is how I would do in SCA SuiteScript file.
Copy code
define('SessionExample', ['Models.Init'], function ( ModelsInit){
	'use strict';
	return SCModel.extend({
		name: 'SessionExample',
		get: function () {
            ModelsInit.context.setSessionObject('SessionExample', true);
		}
	})
});
Copy code
Uncaught Error: Module "SC.Models.Init" on "<http://localhost:7777/SC.Models.Init.js>" not found
Regarding above error, Have you deployed your code/extension?
and yes, SC.Models.Init is only available in SuiteScript 1.0 as per my knowledge.
Below is the documentation for Commerce API migration
m
Wow...you have been really helpful. Yeah I hooked up a Suitescript 1.0 backend so that I could use
SC.Models.Init
to set a session which worked. But you pointing me to the fact that
N/runtime
sets sessions. Can't believe I never saw that before since I use that module all the time when building suitescripts. Can I ask how you determine request method from frontend requests? The following does a GET request to the suitescript backend service controller.
Copy code
const orderModel = new OrderModel();
orderModel.fetch({
	data: {
		order_id: uri_matches[2]
	}
})
	.done(result => {
	});
How would I do a POST request to a Suitescript v2 backend?
l
Copy code
////SS file

/**
* @NApiVersion 2.x
* @NModuleScope Public
* // Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* // Licensed under the Universal Permissive License v 1.0 as shown at <http://oss.oracle.com/licenses/upl>.
*/
define([
    './Demo.SS2Model.js',
    'N/runtime'
], function (
    DemoSS2Model,
    runtime
) {
    'use strict';

    function isLoggedIn () {
        var user = runtime.getCurrentUser();
        return user.id > 0 && user.role !== 17
    }

    function service (context) {
        var response = {};
        if (isLoggedIn()) {
            switch (context.request.method) {
                case 'GET':
                    response = DemoSS2Model.get(context)
                    break;
                case 'POST':
                    response = <http://DemoSS2Model.post|DemoSS2Model.post>(context)
                    break;
                default:
                    response = {
                        type: 'error',
                        message: 'Method not supported: ' + context.request.method
                    };
            }
        }
        else {
            response = {
                type: 'error',
                message: 'You must be logged in to use this service'
            }
        }
        context.response.write(JSON.stringify(response));
    }

    return {
        service: service
    }
})
Copy code
//Model file

/**
* @NApiVersion 2.x
* @NModuleScope Public
*/
define([
  'N/record','N/search','N/runtime','N/log','N/url','N/https'
], function(
  record,search,runtime,log,url,https
){

  'use strict';
  return {
    get: function(context){
      if(context){
        try{
          var user = runtime.getCurrentUser();
          //------- Your Code
          return {message: "your get result"};
        }
        catch(e){
          return e;
        }
      }
    },
    post: function(context){
      if(context){
        try{
          var user = runtime.getCurrentUser();
          //------- Your Code
          return {message: "your post result"};
        }
        catch(e){
          return e;
        }
      }
    }
  }
});
Copy code
//your js view file

this.model = new DemoSS2Model();
					this.model.fetch({
								type: "post",
								 data:{
									 internalid:1234,
									 email:'email'
								 }
							,	killerId: AjaxRequestsKiller.getKillerId()
						}).done(function(result) {
							if (result) {
								var message= result.message;
								self.render();
								self.showSuccess(type,message);
							}
						});
Can I ask how you determine request method from frontend requests? You need to pass the type of request, Do search on google about this.
@Marvin Let me know if you need more information..
I have learn from- https://developers.suitecommerce.com/events.html and obviously Steav
m
Thanks. I knew how to catch POST calls, but wasn't sure how to pass them using
.fetch
. Appreciate the help.