Global API
...
Code Examples
Sales Order Flow

Class (Apex)

1min
// start apex class public with sharing class createnssalesorderrunasync { public createnssalesorderrunasync() { } @invocablemethod(label='create netsuite sales order' description='create netsuite sales order') public static list\<response> createnetsuitesalesorder(list\<requests> requestlist){ list\<response> restobereturnedlist = new list\<response>(); list\<object> netsuitesalesorderlist = new list\<object>(); for(requests req requestlist){ netsuitesalesorderlist add(generatenetsuitesalesordermap(req)); } // sending request to netsuite via exposed global api map\<string, object> reqjsonmap = new map\<string, object>(); reqjsonmap put('salesorders' , netsuitesalesorderlist); string reqjson = json serialize(reqjsonmap); map\<string, object> finalreqmap = new map\<string, object>(); finalreqmap put('version' , '1 0'); finalreqmap put('action' , 'createsalesorder'); finalreqmap put('requestjson' , reqjson); system debug('request ' + finalreqmap); map\<string, object> respmap = breadwinner ns breadwinnernetsuiteapi call(finalreqmap); system debug('response ' + respmap); map\<string,object> soresponsejsonmap = (map\<string,object>)json deserializeuntyped((string)respmap get('responsejson')); response responsereturn = new response(); if(soresponsejsonmap !=null && soresponsejsonmap containskey('status') && soresponsejsonmap get('status')!=null){ responsereturn statuscode = (string)soresponsejsonmap get('status'); } if(soresponsejsonmap !=null && soresponsejsonmap containskey('salesorders') && soresponsejsonmap get('salesorders')!=null){ list\<object> soobj = (list\<object>)soresponsejsonmap get('salesorders'); map\<string,object> savedso = (map\<string,object>)soobj\[0]; responsereturn recordid = (string)savedso get('salesforceid'); } if(soresponsejsonmap !=null && soresponsejsonmap containskey('errors') && soresponsejsonmap get('errors')!=null){ responsereturn error = json serialize((list\<object>)soresponsejsonmap get('errors')); } restobereturnedlist add(responsereturn); //sendreqfuture(json serialize(finalreqmap)); return restobereturnedlist; } / @future(callout = true) public static void sendreqfuture(string jsonstring){ map\<string, object> finalreqmap = (map\<string, object>)json deserializeuntyped(jsonstring); map\<string, object> respmap = breadwinner ns breadwinnernetsuiteapi call(finalreqmap); system debug('response ' + respmap); } / public class requests{ @invocablevariable(label='class' description='class') public string classification; //lookup @invocablevariable(label='created from' description='created from') public string createdfrom; //lookup @invocablevariable(label='currency' description='currency') public string currencyrecord; //lookup @invocablevariable(label='custom form' description='custom form') public string customform; //lookup @invocablevariable(label='department' description='department') public string department; //lookup @invocablevariable(label='location' description='location') public string location; //lookup @invocablevariable(label='discount item' description='discount item') public string discountitem; //lookup @invocablevariable(label='discount rate' description='discount rate') public string discountrate; @invocablevariable(label='discount total' description='discount total') public double discounttotal; @invocablevariable(label='entity' description='entity') public string entity;//lookup @invocablevariable(label='exchange rate' description='exchange rate') public double exchangerate; @invocablevariable(label='external id' description='external id') public string externalid; @invocablevariable(label='handling cost' description='handling cost') public double handlingcost; @invocablevariable(label='memo' description='memo') public string memo; @invocablevariable(label='other ref num id' description='other ref num id') public string otherrefnum; @invocablevariable(label='sales rep' description='sales rep') public string salesrep; @invocablevariable(label='shipping cost' description='shipping cost') public double shippingcost; @invocablevariable(label='status' description='status') public string status; @invocablevariable(label='sub total cost' description='sub total cost') public double subtotal; @invocablevariable(label='subsidiary' description='subsidiary') public string subsidiary; @invocablevariable(label='tax total' description='tax total') public double taxtotal; @invocablevariable(label='total' description='total') public double total; @invocablevariable(label='tran date' description='tran date') public datetime trandate; @invocablevariable(label='tran id' description='tran id') public string tranid; //sales order line items @invocablevariable(label='sales order line items' description='sales order line items') public list\<sobject> solines; @invocablevariable(label='line description' description='line description') public string linedescription; @invocablevariable(label='line unit amount' description='line unit amount') public string lineunitamount; @invocablevariable(label='line quantity' description='line quantity') public string linequantity; @invocablevariable(label='line department' description='line department') public string linedepartment; //lookup @invocablevariable(label='line gross amt' description='line gross amt') public string grossamt; @invocablevariable(label='line item' description='line item') public string items; @invocablevariable(label='line number' description='line number') public string line; @invocablevariable(label='line location' description='line location') public string linelocation; @invocablevariable(label='line price' description='line price') public string price; @invocablevariable(label='line rate' description='line rate') public string rate; @invocablevariable(label='line revrec enddate' description='line revrec enddate') public string revrecenddate; @invocablevariable(label='line revrec schedule' description='line revrec schedule') public string revrecschedule; @invocablevariable(label='line revrec startdate' description='line revrec startdate') public string revrecstartdate; @invocablevariable(label='line tax1 amt' description=' line tax1 amt') public string tax1amt; @invocablevariable(label='line tax code' description='line tax code') public string taxcode; @invocablevariable(label='line tax rate1' description='line tax rate1') public string taxrate1; } public class response{ @invocablevariable(label='status code' description='status of the request') public string statuscode; @invocablevariable(label='sf record id' description='sf record id') public id recordid; @invocablevariable(label='error' description='error') public string error; } public static map\<string, object> generatenetsuitesalesordermap(requests req){ map\<string, object> netsuitesalesordermap = new map\<string, object>(); // assign values from the request to netsuite sales order if(req classification != null) netsuitesalesordermap put('classification', new map\<string, object>{'internalid'=>req classification}); if(req currencyrecord != null) netsuitesalesordermap put('currencyrecord', new map\<string, object>{'internalid'=>req currencyrecord}); if(req customform != null) netsuitesalesordermap put('customform', new map\<string, object>{'internalid'=>req customform}); if(req createdfrom != null) netsuitesalesordermap put('createdfrom', new map\<string, object>{'internalid'=>req createdfrom}); if(req department != null) netsuitesalesordermap put('department', new map\<string, object>{'internalid'=>req department}); if(req location != null) netsuitesalesordermap put('location', new map\<string, object>{'internalid'=>req location}); if(req discountitem != null) netsuitesalesordermap put('discountitem', new map\<string, object>{'internalid'=>req discountitem}); if(req discountrate != null) netsuitesalesordermap put('discountrate',req discountrate); if(req discounttotal != null) netsuitesalesordermap put('discounttotal',req discounttotal); if(req entity != null) netsuitesalesordermap put('entity', new map\<string, object>{'internalid'=>req entity}); if(req exchangerate != null) netsuitesalesordermap put('exchangerate',req exchangerate); if(req externalid != null) netsuitesalesordermap put('externalid',req externalid); if(req handlingcost != null) netsuitesalesordermap put('handlingcost',req handlingcost); if(req memo != null) netsuitesalesordermap put('memo',req memo); if(req otherrefnum != null) netsuitesalesordermap put('otherrefnum',req otherrefnum); if(req salesrep != null) netsuitesalesordermap put('salesrep', new map\<string, object>{'internalid'=>req salesrep}); if(req shippingcost != null) netsuitesalesordermap put('shippingcost',req shippingcost); if(req status != null) netsuitesalesordermap put('status',req status); if(req subtotal != null) netsuitesalesordermap put('subtotal',req subtotal); if(req subsidiary != null) netsuitesalesordermap put('subsidiary', new map\<string, object>{'internalid'=>req subsidiary}); if(req taxtotal != null) netsuitesalesordermap put('taxtotal',req taxtotal); if(req total != null) netsuitesalesordermap put('total',req total); if(req trandate != null){ netsuitesalesordermap put('trandate',req trandate gettime()/1000); } if(req tranid != null) netsuitesalesordermap put('tranid',req tranid); if(!(req solines) isempty()){ // initializing the sales order line items list\<object> lineitemlist = new list\<object>(); for(sobject sobj req solines){ map\<string, object> lineitemmap = new map\<string, object>(); if(req linedescription != null){ lineitemmap put('description', sobj get(req linedescription)); } if(req lineunitamount != null){ lineitemmap put('amount', sobj get(req lineunitamount)); } if(req linequantity != null){ lineitemmap put('quantity', sobj get(req linequantity)); } if(req linedepartment != null) lineitemmap put('department' , new map\<string, object>{'internalid' => sobj get(req linedepartment)}); if(req grossamt != null) lineitemmap put('grossamt', sobj get(req grossamt)); if(req items != null) lineitemmap put('item' , new map\<string, object>{'internalid' => sobj get(req items)}); if(req line != null) lineitemmap put('line', sobj get(req line)); if(req linelocation != null) lineitemmap put('location' , new map\<string, object>{'internalid' => sobj get(req linelocation)}); if(req price != null) lineitemmap put('price' , new map\<string, object>{'internalid' => sobj get(req price)}); if(req rate != null) lineitemmap put('rate', string valueof(sobj get(req rate))); if(req revrecstartdate != null){ datetime getstartdate = datetime valueof(sobj get(req revrecstartdate)); lineitemmap put('revrecstartdate', getstartdate gettime()/1000); } if(req revrecenddate != null){ datetime getenddate = datetime valueof(sobj get(req revrecenddate)); lineitemmap put('revrecenddate', getenddate gettime()/1000); } if(req revrecschedule != null) lineitemmap put('revrecschedule', new map\<string, object>{'internalid' => sobj get(req revrecschedule)}); if(req tax1amt != null) lineitemmap put('tax1amt', sobj get(req tax1amt)); if(req taxcode != null) lineitemmap put('taxcode' , new map\<string, object>{'internalid' => sobj get(req taxcode)}); if(req taxrate1 != null) lineitemmap put('taxrate1', sobj get(req taxrate1)); lineitemlist add(lineitemmap); } // adding the line items to sales order map\<string, object> lineitemobjmap = new map\<string, object>(); lineitemobjmap put('item',lineitemlist); netsuitesalesordermap put('itemlist',lineitemobjmap ); } return netsuitesalesordermap; } } apex test // start apex test class @istest private class createnssalesorderrunasynctest{ @istest static void bw createsalesorder(){ list\<createnssalesorderrunasync requests> requestlist = new list\<createnssalesorderrunasync requests>(); createnssalesorderrunasync requests req = new createnssalesorderrunasync requests(); list\<sobject> solines = new list\<sobject>(); opportunitylineitem opp = new opportunitylineitem(); opp description = 'qualification'; opp unitprice = 1; opp quantity = 1; solines add(opp); req classification='class'; req currencyrecord='test'; req customform='type'; req createdfrom='form'; req department='name'; req items='name'; req taxrate1='name'; req taxcode='name'; req tax1amt='name'; req revrecschedule='name'; req rate='name'; req price='name'; req location='name'; req line='name'; req grossamt='name'; req discountitem='test'; req discountrate='10'; req discounttotal=100; req entity='entity'; req exchangerate=10; req externalid='id'; req handlingcost=15; req memo='memo'; req otherrefnum='other'; req salesrep='salesrep'; req shippingcost=100; req status='staus'; req subtotal=10; req subsidiary='sub'; req taxtotal=5; req total=100; req tranid='tranid'; req solines = solines; req linedescription='description'; req lineunitamount='unitprice'; req linequantity='quantity'; req linedepartment='name'; req linelocation='name'; requestlist add(req); createnssalesorderrunasync createnetsuitesalesorder(requestlist); } }