I am new to Apex development and trying to compile this code but failing - salesforce

public class InstanceTriggerHandler {
public static void createInstanceHistoryRecord(Map<ID,Instance__c> newMap, Map<ID,Instance__c> oldMap) {
List<Instance_History__c > listOfIntHistoryToCreate = new List<Instance_History__c >();
List<Schema.FieldSetMember> trackedFields = SObjectType.Instance__c.FieldSets.Case_View.getFields();
if (trackedFields.isEmpty()) return;
for(Instance__c ins:newMap.values()) {
for (Schema.FieldSetMember fst : trackedFields) {
String fieldName = fst.getFieldPath();
String fieldLabel = fst.getLabel();
Instance__c oldInstance = (oldMap != null) ? oldMap.get(ins.id) : new Instance__c();
if (ins.get(fieldName) != oldInstance.get(fieldName)) {
String newValue = String.valueOf(ins.get(fieldName));
String oldValue = String.valueOf(oldInstance.get(fieldName));
if (oldValue != null && oldValue.length()>255) {
oldValue = oldValue.substring(0,255);
}
if (newValue != null && newValue.length()>255) {
newValue = newValue.substring(0,255);
}
final Instance_History__c instanceHistory = new Instance_History__c();
instanceHistory.Field__c = fieldLabel;
instanceHistory.Instance__c = ins.Id;
instanceHistory.Field_API__c = fieldName;
instanceHistory.Original_Value__c = oldValue;
instanceHistory.New_Value__c = newValue;
listOfIntHistoryToCreate.add(instanceHistory);
}
}
}
if(!listOfIntHistoryToCreate.isEmpty()) {
insert listOfIntHistoryToCreate;
}
}
}
Apex is giving me an error.
"Invalid type: Schema.Instance_History".
I already checked my custom object and it indeed has double underscores in code everywhere it is referenced. Can someone help?

Field history objects for custom objects need 2 underscores, try Instance__History. Probably easiest for you is to use http://workbench.developerforce.com/ to see all objects? You ever used "describe" operations?
https://help.salesforce.com/s/articleView?id=sf.tracking_field_history.htm&type=5
Salesforce stores an object’s tracked field history in an associated
object called StandardObjectNameHistory or CustomObjectName__History.
For example, AccountHistory represents the history of changes to the
values of an Account record’s fields. Similarly,
MyCustomObject__History tracks field history for the MyCustomObject__c
custom object.

Schema.SObjectType.Instance__c.FieldSets.Case_View.getFields();

Related

Flutter : How to get the value of the other properties from a List?

All of my Json data is already inside in my model. And I have also a list of data inside "selectedBranches[]". What do I want is to get the other value of the other properties which is the BranchCode and BranchId based on the value from the selectedBranches list and put it to the another list.
My selectedBranches List is consist of list of branchFullName properties from my json data.
But dart says The argument type 'List' can't be assigned to the parameter type 'Pattern'.
#Code Snippet#
selectedBranches = [ex1,ex2,ex3,ex4];
selectedAssignBranches = [];
assignBranch() {
branchModel1 = GetBranchList.fromJson(jsonData1);
var newArr = branchModel1.data;
for (var u in newArr) {
AssignBranch assignModel = new AssignBranch();
if (u.branchFullName.contains(selectedBranches/*error*/)) {
assignModel.BranchCode = u.branchCode;
assignModel.BranchID = u.brID;
selectedAssignBranches.add(assignModel);
}
}
}
#Model#
class Datum {
String branchCode;
String branchName;
String stat;
String brID;
String branchFullName;
I already solve the problem. You just need to have a nested for loop. Thats it!
assignBranch() {
branchModel1 = GetBranchList.fromJson(jsonData1);
var newArr = branchModel1.data;
for (var i in selectedBranches) {
for (var u in newArr) {
if (u.branchFullName == (i)) {
AssignBranch assignModel = new AssignBranch();
assignModel.BranchCode = u.branchCode;
assignModel.BranchID = u.brID;
selectedAssignBranches.add(assignModel);
}
}
}
}

Sitecore doesn't remove records from custom solr index

I have strange issue when I am trying to remove some records from custom solr index.
I created code like this
public void DeleteRecordsFromIndex(string indexName,IEnumerable<IIndexableUniqueId> uniqueIds)
{
if (uniqueIds == null || string.IsNullOrEmpty(indexName) || !uniqueIds.Any())
{
return;
}
using (IProviderDeleteContext deleteContext = ContentSearchManager.GetIndex(indexName).CreateDeleteContext())
{
foreach (var indexId in uniqueIds)
{
deleteContext.Delete(indexId);
}
deleteContext.Commit();
}
}
Search Item property when I need to get UniqueId
[IndexField("_uniqueid")]
public IIndexableUniqueId UniqueId
{
get
{
return new `enter code here`IndexableUniqueId<string>(this.Uri.ToString());
}
}
Based on debug info IIndexableUniqueId contains correct values
like:"sitecore://web/{66d75448-72a5-4d94-9788-61c6c64b9251}?lang=en-au&ver=1"
what is equal to _uniqueid field in solr index.
I had 4 records in my custom solr index.
After first run one records was removed from index, But 3 records are steel there.
I have ran code couple of times, but 3 records always inside of index.
What could be wrong with my code ?
I found solution:
insted of using IIndexableUniqueId it is better to use IIndexableId
public IIndexableId GetIndexableId(ID itemId)
{
//map to _group value
var id = itemId.ToString().ToLower().Replace("{", "").Replace("}", "").Replace("-", "");
return new IndexableId<string>(id);
}
public int DeleteRecordsFromIndex(string indexName, IIndexableId uniqueIds)
{
if (uniqueIds == null || string.IsNullOrEmpty(indexName))
{
return -1;
}
using (var deleteContext = ContentSearchManager.GetIndex(indexName).CreateDeleteContext())
{
var resuls = deleteContext.Delete(uniqueIds);//this method use _group fields for remove records. All languages will be removed.
deleteContext.Commit();
return resuls;
}
}
regarding ILSpy, sitecore remove item by _group
// Sitecore.ContentSearch.SolrProvider.SolrUpdateContext
public void Delete(IIndexableId id)
{
Assert.ArgumentNotNull(id, "id");
object obj = this.index.Configuration.IndexFieldStorageValueFormatter.FormatValueForIndexStorage(id);
SolrQueryByField solrQueryByField = new SolrQueryByField("_group", obj.ToString());
SolrQueryByField solrQueryByField2 = new SolrQueryByField("_indexname", this.index.Name);
this.solr.Delete(solrQueryByField && solrQueryByField2);
this.CommitPolicyExecutor.IndexModified(this, id, IndexOperation.Delete);
}

Using variable A to control variable B

I have a function called FarmClick, and I want it to check the current MovieClip's array. For example:
var farmSpot1:("id",int,int);
var farmSpot2...
I need the clickEvent to capture the MovieClips name and check it's array for the 0th element to see whether it's empty, or not...
My code so far:
public function FarmClick(event: MouseEvent): void {
var CurrentSlot = event.currentTarget.name
if ([event.currentTarget.name[0]] = "empty") {
stage.addChild(menu);
menu.x = 400;
menu.y = 90;
this.menu.buyCornBtn2.addEventListener(MouseEvent.CLICK,buyCorn2);
} else if (farmEmpty == true && itemSelected != "none") {
selected();
} else if (farmEmpty == false) {
farmHarvest();
}
}
It took me a minute to figure out just exactly what it was that you're asking.
So basically, your movie clip's name would be farmSpot[someNumber] and you have an array with the same name?
in that case, to reference the array variable, you need to use the this keyword.
you can add any array subscripts after you've referenced it like so:
private var farmSpot1:Array = ["id", int, int];
public function FarmClick(event:MouseEvent): void
{
if (this[event.currentTarget.name][0] == "") // wasnt sure if you meant the string "empty" or just an empty string, so i just put an empty string
{
// do whatevs
}
}

Is it possible to specify schema.data in a Kendo.Mvc.UI.Fluent.MultiSelectBuilder

Using server-side filtering, I would like to respond to each request from a kendo MultiSelect widget with an object comprised of multiple properties one of which is the data items to be enumerated in the drop down list.
It looks like I need to specify schema.data. But I haven't figured out how to specify schema.data using the MVC wrappers. Is this possible or is there another way to do this?
Here's my custom MultiSelectBuilder code:
MultiSelectBuilder BuildSelector(HubAssemblyFieldEnums.FilterFields field, string placeHolder, string dataValueField, string dataTextField = default(string), string itemTemplate = default(string), string tagTemplate = default(string)) {
var name = System.Enum.GetName(typeof(HubAssemblyFieldEnums.FilterFields), field);
var multiSelect = Html.Kendo().MultiSelect()
.Name(name)
.Placeholder(placeHolder)
.HighlightFirst(true)
.Filter(FilterType.Contains)
.AutoBind(false)
.DataSource(_ => _.Read(read => {
read.Action("getFilterValues", "api/hubassembly")
.Type(HttpVerbs.Post)
.Data("function(e) { return getFilters(e, filterEnum." + name + "); }");
})
.ServerFiltering(true));
if (dataValueField != default(string)) { multiSelect.DataValueField(dataValueField); }
if (dataTextField != default(string)) { multiSelect.DataTextField(dataTextField); }
if (itemTemplate != default(string)) { multiSelect.ItemTemplate(itemTemplate); }
if (tagTemplate != default(string)) { multiSelect.TagTemplate(tagTemplate); }
return multiSelect;
}
thanks!
From Telerik tech support,
Generally speaking, this is not supported by the MultiSelect MVC
wrapper and its dataSource always expects a collection, but can be
achieved by overriding the reader.data function e.g.
multiselect.dataSource.reader.data = function (data) { return data.values};

ASP.NET Entity Framework Update SQL set int value to 0

I have a SQL column as an int data type.
I'm trying to update en entry using Entity Framework, setting it's value to 0 but it does not get updated. I also don't get an error when Submitting Changes... Odd. Well I changed the Tag Name and set the count to 0, the name got updated but the count was not modified
Any ideas?
Thanks in advance
Code Snipet:
Add Tag - up the tag count:
Tag tag = _tagsRepository.GetTagByName(TagName);
if (tag == null)
{
tag = new Tag();
tag.CreateDate = DateTime.Now;
tag.Name = TagName;
tag.Count = 1;
}
else
{
tag.Count += 1;
}
tag = _tagsRepository.SaveTag(tag);
Remove tag from an item, update the tag count
Tag tag = _tagsRepository.GetTagByName(TagName);
if (tag != null)
{
tag.Count -= 1;
}
tag = _tagsRepository.SaveTag(tag);
GetTagByName Method
public Tag GetTagByName(string Name)
{
Tag result = null;
using (ISADataContext dc = conn.GetContext())
{
result = dc.Tags.Where(t => t.Name == Name).FirstOrDefault();
}
return result;
}
SaveTag Method
public Tag SaveTag(Tag tag)
{
using (ISADataContext dc = conn.GetContext())
{
if (tag.TagID > 0)
{
dc.Tags.Attach(new Tag { TagID = tag.TagID });
dc.Tags.ApplyCurrentValues(tag);
}
else
{
dc.Tags.AddObject(tag);
}
dc.SaveChanges();
}
return tag;
}
Workaround:
using (ISADataContext dc = conn.GetContext())
{
if (tag.TagID > 0)
{
if (tag.Count == 0)
{
Tag t = dc.Tags.Where( tt=> tt.TagID == tag.TagID).First();
t.Count = 0;
}
else
{
dc.Tags.Attach(new Tag { TagID = tag.TagID });
dc.Tags.ApplyCurrentValues(tag);
}
}
else
{
dc.Tags.AddObject(tag);
}
dc.SaveChanges();
}
return tag;
You shouldn't wrap your DataContext in using statements in your scenario. Doing this means that you're retrieving the object in one context and trying to save the existing object in a new context. This means that when you're trying to save in the second context, it has no idea that the object already exists in the database.
I ran into the same problem as yours - every other value except 0 works fine. Can we get some solution to this?
EDIT: After some research, I found a way to fix this. You have to modify this line:
dc.Tags.Attach(new Tag { TagID = tag.TagID });
In the stub object, add the problematic property (in your case Count) and set its value to something that is never possible in the db, for example -2. In this way, the EF context will also compare the count value of the attached object and will always know there is a difference. So your line becomes:
dc.Tags.Attach(new Tag { TagID = tag.TagID, Count = -2 });
I am not sure if this will work every time, but for now it seems to do the job.

Resources