Is it possible for a script to reference a line on a new record? - field

This relates to some older questions on SuiteScript 1.0. I've got a 2.0 script that has to be 2.0, so I can't use the old nlapiGetLineItemValue - it has to be Record.getSublistValue(options). But I need it to get the line value on a new, unsaved record BeforeSubmit.
It keeps returning "getSublistValue" is not defined in object, and checking NetSuite Field Explorer confirms that the unsaved record has no defined lines.
The same applies to AfterSubmit.
So is there any work around, or is it even possible, to reference the line item value when the record is being created?
ADDING SOME CODE, WHERE THE QUESTION APPLIES:
var recNew = context.newRecord
var ItemID = recNew.getSublistValue({
sublistId: 'items',
fieldId: 'itemid',
});
var listIDs = ["6646", "17745", "17945", "21349"];
var a_filters = [];
a_filters.push(new nlobjSearchFilter(ItemID, null, 'anyof', listIDs));
{
// an action
}

getSublistValue requires you to pass the line number.
var recNew = context.newRecord
for (var x =0; x< recNew.getLineCount({sublistId:'item'}); x++) {
var ItemID = recNew.getSublistValue({ sublistId: 'item', fieldId: 'item', line: x });
// DO STUFF
}

try using
if(scriptContext.type == 'edit'){
var recNew = scriptContext.newRecord
recNew.getSublistValue(options)
}

Related

Error while adding multiple items to an entity

for (int i = 0; i < skus.Count; i++)
{
sku item = new sku();
item = skus[i];
sku sku = CompanyDbContext.skus.Where(s => s.item_no == item.item_no).FirstOrDefault();
if (sku == null) // ok to insert [no duplicate item numbers]
{
CompanyDbContext.skus.Add(item);
}
}
CompanyDbContext.SaveChanges();
I'm getting
collection was modified enumeration operation may not execute
error. How can I fix this ?
As mentioned in the comments, this happens because you are modifying the collection which you are looping through as you're performing your work.
One option you have is to create a temporary collection and add your sku items to that, and finally add the contents of the temporary List<sku> to your CompanyDbContext
// Create a new temporary list
List<sku> tempSkus = new List<sku>();
for (int i = 0; i < skus.Count; i++)
{
// Let's assign item to skus[i] immediately, we don't need a new instance here when we're later re-pointing to an existing instance
sku item = skus[i];
// Use LINQ Any function to determine whether there are any existing SKU's already
bool existingSku = CompanyDbContext.skus.Any(s => s.item_no == item.item_no);
// There are no duplicates, let's add this sku item to our temporary List
if(!existingSku)
{
tempSkus.Add(item);
}
}
// Add the Range of tempSkus List to the CompanyDbContext
CompanyDbContext.skus.AddRange(tempSkus);
CompanyDbContext.SaveChanges();
Or if you prefer LINQ
// Create a new temporary list
List<sku> tempSkus = skus.Where(p => CompanyDbContext.skus.Any(s => s.item_no != p.item_no)).ToList();
// Add the Range of tempSkus List to the CompanyDbContext
CompanyDbContext.skus.AddRange(tempSkus);
CompanyDbContext.SaveChanges();
The problem is that you are modify the same thing that you are iterating. As best practice you should update your method something like this:
//get search predicat from List<sku> skus
var item_nos = skus.Select(s=>s.item_no).ToList();
//items already in repo
var addedItems = CompanyDbContext.skus.Where(s => item_nos.Contains(s.item_no)).ToList();
var newItems = skus.Except(addedItems).ToList();
foreach(var sku in newItems){
CompanyDbContext.skus.Add(item);
}
CompanyDbContext.SaveChanges();

dapper querymultiple spliton error

I'm using: ASP.NET MVC, MySql, Dapper.NET micro-orm
I made a stored procedure with 3 SELECTs, two of which returns lists and the third one returns an integer.
Here is my code:
using (var conn = new MySqlConnection(GetConnectionString()))
{
var readDb = conn.QueryMultiple(storedProcedure, parameters, commandType: CommandType.StoredProcedure);
var result = new someView
{
TopicsList = readDb.Read<ITopic>().ToList(),
TopTopicsList = readDb.Read<IMessage>().ToList(),
TopicsCount = readDb.Read<int>().Single()
};
return result;
}
In ITopic I have TopicId, in IMessage I have MessageId.
And here's the error:
When using the multi-mapping APIs ensure you set the splitOn param if you have keys other than Id Parameter name: splitOn
I tried adding splitOn on both QueryMultiple and Read, and nigher accepted it.
Though I dont understand why I need splitOn? can't dapper see that I have three separate SELECTs? When using conn.Read(storedProcedure,parameters) on each of the selects separately (instead of MultipleQuery on all of the together) dapper has no problem mapping it to a given object.
What am I doing wrong?
1) Problem solved when I used the real models names instead of their interfaces names:
TopicView instead of ITopic, TopTopicsView instead of IMessage;
2) Once that was fixed and there was no longer "no splitOn" error, started another problem with the < int > casting in line:
TopicsCount = readDb.Read<int>().Single()
probably mysql doesnt return numbers back as ints?
I tried using decimal, object, dynamic, etc.. with no luck. Eventually fixed it by creating another Model with int property inside that has the same name as the database int parameter and now it works.
3) Here's the final working code:
using (var conn = new MySqlConnection(GetConnectionString()))
{
var parameters = context.MapEntity(query);
var multi = conn.QueryMultiple(storedProcedure, parameters, commandType: System.Data.CommandType.StoredProcedure);
var TopicsList = multi.Read<TopicView>().ToList();
var TopTopicsList = multi.Read<TopTopicsView>().ToList();
var result = multi.Read<HomeView>().Single();
result.TopicsList = TopicsList;
result.TopTopicsList = TopTopicsList;
return result;
}

breezeJS custom predicates

I want to write a slightly complex query, so I wonder if there is a way to provide a custom function as the predicate of a where clause.
For example, it would be nice if we could do something like:
var myArray = [1, 2, 3];
var filter = function (person) {
return elementExists(person.id, myArray);
};
EntityQuery.from('persons').toType('Person')
.where(filter);
Looking at the source code I realized that such capability is not present in the latest version of BreezeJS (i might be wrong).
I just wonder if breeze supports anything similar to that.
On the client side you should be able to find out if the element exists by checking the length of the return.
var getItems = function (runId, tankId, topicId) {
var localquery = EntityQuery.from("Items")
.using(manager);
var p1 = new breeze.Predicate("runId", "eq", runId);
var p2 = breeze.Predicate("tankId", "eq", window.app.vm.tanks.activetank());
var p3 = breeze.Predicate("topicId", "eq", topicId);
var p4 = breeze.Predicate("topicId", "eq", app.Topics.Growth_Topic);
var pred;
var runId = p1._value;
var tankId = p2._value;
// If the third parameter exists, add it to your complex predicate
// adding a specific Topic to the predicate
// Otherwise only add the General topic
if (p4)
pred = breeze.Predicate.or([p3, p4]);
else
pred = breeze.Predicate.or(p3);
var newpred = breeze.Predicate.and([p1, p2, pred]);
// newpred is now querying for a specific runId and tankId and (p3 or p4) if p4 exists
// otherwise it is querying for runId and tankId or (p3)
// So look in the local metadataStore first
var queryb = localquery.where(newpred);
var results = manager.executeQueryLocally(queryb);
// If we do have it locally use it
if (results.length) {
window.app.vm.Items.Items(results);
}
// otherwise get it from the database
else {
var query = EntityQuery
.from("Items")
.where(newpred);
// return the promise from breeze
return manager.executeQuery(query)
.then(function (data) {
// check to see if the call to the webapi controller returned any data
if (data.length > 0) {
// stick it into the viewmodel it to your viewmodel
window.app.vm.Items.Items(data.results);
return "element exists";
} else {
return "element does not exist";
}
})
.fail(queryFailed);
}
};
This example is quite a bit more complicated that you requested, so cut out the parts you do not want to simplify your query. I am showing you here how to get both an "and" and an "or" into your query. I am also showing how to check the local metadataStore, to see if the item is there before going to the server.
If the item does not exist and you want to create it, be careful about your thread timing and wrap the object creation in a promise before you do something like navigating to another page to show the new item. The navigation may be faster than the creation function and the new item may not be bound into your viewmodel yet by knockout, so it might show up blank on the new page. That will drive you crazy for quite a while.

linq to entities - updating tables with new values not working

The code below is updating the correct values into the OBJECT_TYPES table, but the OBJECT_ITEMS table is being overwritten but I am not sure why. Can anyone help?
var templateId = Request["id"].AsInt();
var dbcontext = new STDEntities1();
var query = dbcontext.OBJECT_TYPES.Where(o => o.ID == templateId);
var template = query.FirstOrDefault();
var newItem = new OBJECT_TYPES
{
CATEGORY_ID = template.CATEGORY_ID,
COMPANY_ID = template.COMPANY_ID,
OBJECT_NAME = "** Select A Name **",
HEIGHT = template.HEIGHT,
WIDTH = template.WIDTH,
TEMPLATE = template.ID
};
foreach (var field in template.OBJECT_ITEMS)
{
newItem.OBJECT_ITEMS.Add(field);
}
dbcontext.OBJECT_TYPES.Add(newItem);
dbcontext.SaveChanges();
this is happening because you are adding field which actually is an object that is being tracked by the dataContext/dbContext and even has an id. So the values are being overwritten.
Try creating the a new field OR try detaching the field from the context and then put the Id/Primary key to 0 and try inserting it again.

Client Side Query to filter by extended properties

I have a need where i want to get the count of deleted items and get change set/collection for items that are not deleted.
How can i achieve that ?
i am able to do it using the LINQJS, but was wondering if there is more proper way of doing it.
var countryType = manager.metadataStore.getEntityType("Country");
var countries = manager.getEntities(countryType);
var deletedCount = Enumerable.From(countries)
.Where(function (x) {
return x.entityAspect.entityState == "Deleted"
}).Count();
self.totalRows(self.totalServerRows() - deletedCount);
var queryResult = Enumerable.From(countries)
.OrderBy(function(x){ x.Country_Code })
.Where(function (x) {
return x.entityAspect.entityState != "Deleted"
})
.Skip(self.pageIndex() * self.pageSize())
.Take(self.pageSize())
.ToArray();
self.list(queryResult);
The EntityManager.getEntities call is overloaded (see: http://www.breezejs.com/sites/all/apidocs/classes/EntityManager.html#method_getEntities ) so that you can simply ask for those entities within the entityManager that are of specific types or in specified states. So for 'deleted, try this:
var deletedEntities = em.getEntities(null, [EntityState.Deleted]);
or if you wanted just the Added or Modified entities
var deletedEntities = em.getEntities(null, [EntityState.Added, EntityState.Modified]);
I hope this helps.

Resources