This message was deleted.
# suitescript
s
This message was deleted.
b
at least use .toJSON like a sane person
Copy code
srJSON = searchResult.toJSON()
k
lol actually im not going to be using JSON in this script, it was just an example from a previous script
I just need to load the parent record (if there is one), grab its URL component and the current URL component and store that in a field.
{parentURI}/{currentURI}
b
what type of record are you working with
k
Categories
b
full name? there are lots of categories (contact category, customer category, expense category...)
k
Oh actually it is retrieving the category for the inventory/kit items.
There is a category field
{custitem_kotn_sample_category}
being set on each inventory item, however since it is a dropdown, that just gives me the id of the category. So I need to take the category ID from that field on the item record, and look up its URI, and build a string to save to a new field
{categoryUri}/{itemUri}
if that makes sense
b
sounds more and more like this is #C29HR0BFG related
k
Im using a map reduce script in a sitebuilder site
b
but if its in a custom field, you need to do your own search
on the source of the custom item field
k
I already have the search built
b
what is the search type?
k
Saved Item Search
b
do the search on the source of the item field
not the item itself
k
im not sure I follow?
Im using an item search to retrieve a list of items that have the category dropdown filled out. Then loading each item record to then grab the ID of the category that is selected in that field.
b
what does the custom item field look like
k
message has been deleted
b
interested in the page where the field is setup
specifically the type and list/record fields
k
message has been deleted
b
that is not going to go well for you
site category is not supported in suitescript
k
How do I go about this then?
b
i would try asking in #C29HR0BFG to see if there is a programatic way to get the parent site category
you might have to hardcode a lookup list
k
Sounds fun, I asked in the #C29HR0BFG channel, thanks for your help!
b
you might be lucky, turns out sitecategory is undocumented
k
Convenient
b
you can load sitecategory records using the type
sitecategory
so get the value of custitem_kotn_sample_category, load the matching site category, get its parent, load the parent and then get the url components from the 2 records
k
`
Copy code
var searchResult = JSON.parse(context.value);
var productId = searchResult.id;

var nsId = searchResult.id;

var itemRec = record.load({
   type: record.Type.INVENTORY_ITEM,
   id: productId
});
var catId = itemRec.getValue({
   fieldId: "custitem_kotn_sample_category"
});

log.debug("Category ID" + catId);
Proper way so far, correct? Or am i still using the JSON object wrong
How do I load the parent in SS?
b
you've lost the eval, so it looks much better
k
sweet
Now just need to load the parent record, but not sure how to go about that
b
Copy code
record.load({
   type: 'sitecategory',
   id: catId
});
thats the child category
from that record you should be able to get the
parentcategory
and
urlcomponent
and then repeat the loading for the value of the parentcategory
k
and those being the `fieldId`'s?
b
hopefully, you take your chances with undocumented things
k
Weird that I am getting
97370
for the Category ID from that field...
Thats not even a valid category ID...
nvm yes it is...
Ok i think i can get this to work
last question....
What is the best way to conditionally load an
inventoryitem
or
assemblyitem
based on type?
Getting some errors when trying to load the wrong type
Just a simple case / switch?
b
you shouldnt need to
your search results have a recordType key
k
Copy code
ERROR String: {"type":"error.SuiteScriptError","name":"SSS_RECORD_TYPE_MISMATCH","message":"The record you are attempting to load has a different type: assemblyitem from the type specified: inventoryitem.","stack":["createError(N/error)"],"cause":{"type":"internal error","code":"SSS_RECORD_TYPE_MISMATCH","details":"The record you are attempting to load has a different type: assemblyitem from the type specified: inventoryitem.","userEvent":null,"stackTrace":["loadRecord_impl(N/recordImpl)","map(/SuiteScripts/reviewCatUri.js:30)"],"notifyOff":false},"id":"","notifyOff":false,"userFacing":false}
Just seeing this error in the script log
`
Copy code
type: record.Type.INVENTORY_ITEM,
instead of that, can I just drop the
INVENTORY_ITEM
part?
b
get the recordType from the search result.
use that for the type
k
Copy code
var searchResult = JSON.parse(context.value);
			var recordType = searchResult.recordType;
			var nsId = searchResult.id;

			var itemRec = record.load({
				type: recordType
				id: nsId
			});
like so?
b
looks reasonable
you can probably avoid loading the item record altogether by adding the custitem_kotn_sample_category to the columns and getting it from the search result
k
Ahhh good point
Thanks again btw for your help, this is super helpful
How do i get the 4th column in a search again??
Copy code
var catId = searchResult.columns[3];
b
either use the debugger to see what searchResult looks like or use log.debug with the searchResult to see what it looks like
k
Can you load an item record and category record at the same time?
I still cannot get the column I want from the search directly
b
whats the output of logging the searchResult
k
It gets cut off so i cannot view the whole output
When trying this `
Copy code
var searchResult = JSON.parse(context.value);

log.debug(searchResult);
b
there are 2 parameters to logging, the title and the details
details have 3999 characters, title much less
lots of people (me included) are lazy and rely on legacy behavior, but you are supposed to log like:
Copy code
log.debug({
  title: "Battk says i should choose a title",
  details: searchResult
});
k
Cant I just put a custom label on a column and call it directly by name somehow?
b
nope, native serialization doesnt care
you can write your own code to do so, but i think its overkill
did you need help accessing the
custitem_kotn_sample_category
when you know its structure looks like: ``````
k
ya i think i can just get it from there
Copy code
var catId = searchResult.values.custitem_kotn_sample_category.value;
And indeed i can
sweet
But I am still going to need to load two records at once...
to resave the data to the itemRec
b
you can probably use record.submitFields instead if you dont want to go back
k
what you mean?
b
record.submitFields allows you to modify records without loading them by doing the programatic equivalent of an inline edit
k
Copy code
var searchResult = JSON.parse(context.value);

			log.debug("Search Result", searchResult);


			var itemId = searchResult.id;
			var recordType = searchResult.recordType;
			
			var catId = searchResult.values.custitem_kotn_sample_category.value;
			var itemUrl = searchResult.values.urlcomponent;
			
			log.debug("Category ID: " + catId);

			var catRec = record.load({
				type: 'sitecategory',
				id: catId
			});

			var parentCatUrl = catRec.getValue({
				fieldId: "urlcomponent"
			});

			log.debug("Parent Category URL: ", parentCatUrl);

			var fullUrl = parentCatUrl + "/" + itemUrl;

			log.debug("Full URL: " + fullUrl);
			
			var itemRec = record.load({
				type: recordType,
				id: nsId
			});
			
			itemRec.setValue({
				fieldId: 'custitem_sample_cat_uri',
				value: fullUrl
			});
			
			var recordId = itemRec.save({
				enableSourcing: false,
				ignoreMandatoryFields: true
			});

			context.write(itemId, fullUrl);
Thats what i have now
Copy code
var itemRec = record.submitFields({
				type: recordType,
				id: itemId,
				values: {
					'custitem_sample_cat_uri': fullUrl
				},
				options: {
					enableSourcing: false,
					ignoreMandatoryFields : true
				}
			});
So something like this?
b
looks reasonable, but itemRec wont be a record, should be the itemId
k
Copy code
var itemRec = record.submitFields({
   type: recordType,
   id: itemId,
   values: {
      'custitem_sample_cat_uri': fullUrl
   },
   options: {
      enableSourcing: false,
      ignoreMandatoryFields : true
   }
});
like so?
b
i think thats the same thing, keep in mind that itemRec is no longer a record
k
Im not using it again, so i think its fine, right?
I think it worked!
Yep indeed it did
Thank you SO much