Global API
Code Examples

Closing a Sales Order from a Button

5min
here is a use case example, where it's desired to add a button allowing the user to close a sales order in netsuite right from the sales order in salesforce this can be done by using a flow button which kicks off a flow and simply passes the source sales order record to an invokable apex action which does all the work let's do this and also set up to receive back the api message from netsuite to see if the process was successful this is not necessary, but shown in case it's useful since we'll be getting a netsuite response back, we will not use the future method callout first things first, set up the apex class to close sales orders input parameter (in request section) has the description 'netsuite sales order' which will come up on the flow parameters list the output parameter (in response section) has the description 'netsuiteresponse' and we will be able to take the value and display it to screen public with sharing class closesalesorder invokable { public closesalesorder invokable() { } @invocablemethod( label='close netsuite sales order' description='close netsuite sales order' category='breadwinner' ) public static list\<response> closesalesorder(list\<requests> requestlist) { response response = new response(); id salesorderid = requestlist\[0] salesorder; system debug('so id ' + salesorderid); map\<id, breadwinner ns bw sales order c> salesordersbyid = new map\<id, breadwinner ns bw sales order c>( \[select id, breadwinner ns internalid c from breadwinner ns bw sales order c where id = \ salesorderid] ); system debug('so by id ' + salesordersbyid); map\<id, breadwinner ns bw line item c> lineitemsbyid = new map\<id, breadwinner ns bw line item c>( \[select id, breadwinner ns iteminternalid c, breadwinner ns line c, breadwinner ns isclosed c from breadwinner ns bw line item c where breadwinner ns sales order c = \ salesorderid] ); system debug('li by id ' + lineitemsbyid); //create salesorder map for so values query map\<string, object> nssalesorder = new map\<string, object>(); for(breadwinner ns bw sales order c salesordersbyidvalue salesordersbyid values()){ nssalesorder put('internalid', salesordersbyidvalue breadwinner ns internalid c); } //initialize sales order line items list\<object> lineitemlist = new list\<object>(); for(breadwinner ns bw line item c lineitemvalue lineitemsbyid values()){ map\<string, object> lineitem = new map\<string, object>(); lineitem put('item', new map\<string, object>{'internalid'=>lineitemvalue breadwinner ns iteminternalid c}); lineitem put('line', lineitemvalue breadwinner ns line c); lineitem put('isclosed', true); lineitemlist add(lineitem); } system debug('line item updates ' + lineitemlist); // adding the line items to sales order map\<string, object> solineitemlist = new map\<string, object>(); solineitemlist put('item',lineitemlist); solineitemlist put('replaceall', false); nssalesorder put('itemlist',solineitemlist); // initialize the requestjson data to be passed map\<string, object> requestjsonmap = new map\<string, object>(); requestjsonmap put('salesorders', new list\<object>{nssalesorder}); string reqjson = json serialize(requestjsonmap); map\<string, object> reqobj = new map\<string, object>(); reqobj put('version', '1 0'); reqobj put('action', 'updatesalesorder'); reqobj put('requestjson', reqjson); system debug('request ' + reqobj); // place a request to breadwinner global api map\<string, object> respmap = breadwinner ns breadwinnernetsuiteapi call(reqobj); system debug('response ' + respmap); //take the netsuite response map and convert to single string string nsresponsestring = ''; for(string key respmap keyset()){ if(nsresponsestring length() !=0){ nsresponsestring += ' '; } nsresponsestring += key + '=' + respmap get(key); } response netsuiteresponse = nsresponsestring; list\<response> responsewrapper= new list\<response>(); responsewrapper add(response); return responsewrapper; } public class requests { @invocablevariable(label='netsuite sales order' description='ns sales order (sf id)') public id salesorder; //lookup } public class response { @invocablevariable(label='error' description='error') public string error; @invocablevariable(label='netsuiteresponse' description='api response from netsuite') public string netsuiteresponse; } } with the apex class now loaded to salesforce, let's build the flow keeping it simple we will just read the current record we're on (user will be interacting from the netsuite sales order) and pass the id to the class to close it creating a new variable called recordid and setting it's default value to {!$flow\ currentrecord} is used to easily refer to the source record (netsuite sales order) the user is looking at after creating recordid text variable, create another text variable called netsuiteresponse that will get the reply from the netsuite api so it can be displayed for debugging and finally add the apex action from the code that was added to salesforce the action we want can be found in the breadwinner category (set in the description and category section of apex class) fill out the parameters, and don't forget to open up the advanced section for reply values great! now the apex invokable class is installed, and we've got a screen flow built to run it let's debug to make sure it works click debug on the flow and feed it the record id from a test netsuite sales order you want to close click next or finish and the apex will fire off and close that order the response might be a bit messy looking, but there's alot of good debug info in there in case something doesn't go as planned with the flow having been run, check out the sales order that you processed and there we go! we've closed a sales order using a flow in salesforce from here you can deploy the flow to salesforce by creating a new apex action button and add it to your page layout