How to retrieve field from custom master object? - salesforce

I am trying to write a custom object and have to retrieve one custom related field from custom object Course Master(master) to detail object Training deals. This code is showing error
List<List<String>> strList = new List<List<String>>();
List<Training_deal__c> td = [select name, Course_master__r.course__c from Training_deal__c];
for(Training_deal__c t : td){
List<String> tempList = new List<String>();
tempList.add('Training Deals');
tempList.add(t.name);
tempList.add(t.course__c);
strList.add(tempList);
}

Try This
tempList.add(t.Course_master__r.course__c);

I tried to like this and it is working properly
List<List<String>> strList = new List<List<String>>();
List<Training_deal__c> td = [select name, Course_master__r.course__c from Training_deal__c];
for(Training_deal__c t : td){
List<String> tempList = new List<String>();
tempList.add('Training Deals');
tempList.add(t.name);
tempList.add(t.Course_master__r.course__c);
strList.add(tempList);
}

Related

.NET WPF set combobox selected item

I am setting items in a combobox with this:
Dictionary<int, string> myItems = await resp.Content.ReadAsAsync<Dictionary<int, string>>();
myCmb.Items.Clear();
myCmb.ItemsSource = myItems;
myCmb.SelectedValuePath = "Key";
myCmb.DisplayMemberPath = "Value";
Then I want to select item by Key (for example, select item with Key=5) in another part of the code. How can this be done?
myCmb.SelectedItem = myItems.FirstOrDefault(n => n.Key == desiredKey);
You can do it this way but you will need to have myItems accessible from the another part of code.

Map and List values from APEX SALESFORCE

I have question on my Salesforce WebService and Apex Code.
In a relationship, we have one Notice and multiple attachments in my salesforce. But I don't know how to fix below requirements:
when "GET" webservices incoming thru specific URL API, it supposed to return with JSON format
JSON Format should {Notice1 : attach1{link},attach2{link} , etc }
#RestResource(urlMapping='/API/V1/notice/*')
global with sharing class API_Notice {
#HttpGet(UrlMapping='/API/V1/notice/all')
global static List<String> getNotice(){
Set<Id> NoticeIds = new Set<Id>();
Set<Id> VersionIds = new Set<Id>();
String compares;
List<String> returnJSON = new List<String>();
List<Notice__c> reConts = [select Id, ClosingDate__c ,Name, Contents__c from notice__c];
Map<Id,Notice__c> addMap = new Map <Id,Notice__c>();
Map<Map<Id,Notice__c>,Map<Id,contentdistribution>> addsMap = new Map<Map<Id,Notice__c>,Map<Id,contentdistribution>>();
//SET NOTICE ID
if(!reConts.isEmpty()){
for(Notice__c nc : reConts){
NoticeIds.add(nc.Id);
addMap.put(nc.id,nc);
}
}
//GET public Image URL
if(!NoticeIds.isEmpty()){
Map<Id,ContentDocumentLink> contentMap = new Map<Id,ContentDocumentLink>([
select contentDocumentid,LinkedEntityId from ContentDocumentLink where LinkedEntityId IN:NoticeIds
]);
for(ContentDocumentLink Key : contentMap.values()){
VersionIds.add(Key.ContentDocumentId);
}
if(!VersionIds.isEmpty()){
Map<Id, contentdistribution> cdb = new Map <Id, contentdistribution> ([
select DistributionPublicUrl from contentdistribution where contentDocumentid IN:VersionIds
]);
addsMap.put(addMap,cdb);
}
}
return null;
}
}

Salesforce updating metadata record using apex

'I am trying to update a salesforce metadata record via Apex. here is my code:
Metadata.DeployContainer mdContainer = new Metadata.DeployContainer();
for(Omni_Routing_Skillset__mdt md: omnimetadata) {
Metadata.CustomMetadata customMetadata = new Metadata.CustomMetadata();
customMetadata.fullName = md.DeveloperName; //custom metadata name
customMetadata.Label = md.MasterLabel;
Metadata.CustomMetadataValue customField1 = new Metadata.CustomMetadataValue(); //the values you're changing/updating
customField1.field = 'Omni_Routing_Skillset__mdt.Skill_Level__c'; //the custom field API Name that you're wanting to insert/update a value of
customField1.value = skillsAndValues.get(md.MasterLabel);
customMetadata.values.add(customField1);//add the changes to list of changes to be deployed
mdContainer.addMetadata(customMetadata);
System.debug('customMetadata: ' + customMetadata);
}
//MetadataDeploy callback = new MetadataDeploy();
Id jobId = Metadata.Operations.enqueueDeployment(mdContainer, null);
This kicks off the deployment but i get the following error:
**Must specify the name in the CustomMetadataType.CustomMetadata format, Current Name: Team_2_SEP, Required Delimiter: .**
I am not sure how to fix this, can anyone please advise?
Thanks!
The name would be Omni_Routing_Skillset__mdt.Team_2_SEP you're only passing in Team_2_SEP.
You can reference this method:
public static void updateExisitingMetadatRecord() {
List<Metadata_Creditional__mdt> metadatList = [SELECT Id, DeveloperName,
MasterLabel FROM Metadata_Creditional__mdt WHERE DeveloperName='MyTest'];
Metadata.CustomMetadata metadataRec = new Metadata.CustomMetadata();
metadataRec.fullName =
'Metadata_Creditional__mdt.'+metadatList[0].DeveloperName;
metadataRec.label = metadatList[0].MasterLabel;
//provide the value for the fields and add it to custom metadata instance
Metadata.CustomMetadataValue totalPurchasetoUpdate = new
Metadata.CustomMetadataValue();
totalPurchasetoUpdate.field = 'Secret_Key__c';
totalPurchasetoUpdate.value = 'Change Secreet Value';
metadataRec.values.add(totalPurchasetoUpdate);
Metadata.DeployContainer mdContainer = new Metadata.DeployContainer();
mdContainer.addMetadata(metadataRec);
Metadata.Operations.enqueueDeployment(mdContainer, null);
}

AS3 Separating Arrays for different items

I have a function that creates a new value inside an associative array.
var array:Array = new Array(new Array());
var i : int = 0;
function personVelgLand(evt:MouseEvent)
{
for(i = 0; i < personListe.dataProvider.length; i++)
{
if(txtPersonVelg.text == personListe.dataProvider.getItemAt(i).label)
{
personListe.dataProvider.getItemAt(i).reise = array;
personListe.dataProvider.getItemAt(i).reise.push(landListe.selectedItem.land);
}
}
}
What happens is that the 'array' array which becomes 'personListe.dataProvider.getItemAt(i).reise' applies to every item in the list. I want it so that each time the function runs that the .reise only applies to the item chosen and not all of them.
EDIT:
I did this:
personListe.dataProvider.getItemAt(i).reise = new Array(array);
And now they are not the same but now each item in the list can not have multiple .reise values...
EDIT 2: dataProvider is nothing it would work just as fine without it. .reise is created in the function I originally posted it creates .reise in the object getItemAt(i).
personListe is a list which the users add their own items to by the use of a input textfield. It is given by this function:
function regPerson(evt:MouseEvent)
{
regPersoner.push(txtRegPerson.text);
personListe.addItem({label:regPersoner});
regPersoner = new Array();
txtRegPerson.text = "";
}
EDIT 3 : The user can register names which turn in to labels in a list. There is also list with 3 countries, Spain, Portugal and England. The user can then register a country to a person they select. Thats when I want to create the .reise inside the "name" items in the first list which contains the countries they have selected. I want every name to be able to select multiple countries which then will be created in the element .reise inside the item that holds their name. This would be easy if I could use strings. But later I plan to have the user type in a country and then something that would show every user that have selected that country. That is why the countries need to be stored as arrays inside the "name" items..
You should first create a class for the User data that you are modelling. You already know all the properties.
The user can register names
The user can then register a country to a person they select.
able to select multiple countries
Such a class could look like this:
package
{
public class User
{
private var _name:String;
private var _countries:Array;
public function User(name:String)
{
_name = name;
_countries = [];
}
public function get name():String
{
return _name;
}
public function get countries():Array
{
return _countries;
}
public function set countries(value:Array):void
{
_countries = value;
}
}
}
Now create a DataProvider, fill it with objects of that class and use it for the list as described here:
import fl.controls.List;
import fl.data.DataProvider;
var users:List = new List();
users.dataProvider = new DataProvider([
new User("David"),
new User("Colleen"),
new User("Sharon"),
new User("Ronnie"),
new User("James")]);
addChild(users);
users.move(150, 150);
In order to get a label from a User object, define a labelFunction
import fl.controls.List;
import fl.data.DataProvider;
var users:List = new List();
users.labelFunction = userLabelFunction;
function userLabelFunction(item:Object):String
{
return item.name;
}
users.dataProvider = new DataProvider([
new User("David"),
new User("Colleen"),
new User("Sharon"),
new User("Ronnie"),
new User("James")]);
addChild(users);
users.move(150,150);
This way you do not have to add a label property that you don't want in your class.
Selecting a name means selecting a user. The list of countries associated to the name should show up in a second List.
The DataProvider of that List remains constant, a list of all the available countries.
import fl.controls.List;
import fl.data.DataProvider;
// list of users
var users:List = new List();
addChild(users);
users.move(150,150);
users.labelFunction = userLabelFunction;
function userLabelFunction(item:Object):String
{
return item.name;
}
users.dataProvider = new DataProvider([
new User("David"),
new User("Colleen"),
new User("Sharon"),
new User("Ronnie"),
new User("James")]);
// lsit of countries
var countries:List = new List();
addChild(countries);
countries.move(550,150); // adjut position as desired
countries.dataProvider = new DataProvider([
{label:"a"},
{label:"b"},
{label:"c"},
{label:"d"},
{label:"e"},
{label:"f"}]);
Now all you have to do is to wire it all up. If a user is selected, select his countries in the countries list. If a country is selected, add that to the currently selected users list of countries. That could look somethign like this:
users.addEventLsitener(Event.CHANGE, onUserSelected);
function onUserSelected(e:Event):void
{
countries.selectedItems = users.selectedItem.countries;
}
countries.addEventLsitener(Event.CHANGE, onCountrySelected);
function onCountrySelected(e:Event):void
{
users.selectedItem.countries = countries.selectedItems;
}
The full code could look like this. I did not test this, but you get the idea.
// list of users
var users:List = new List();
addChild(users);
users.move(150,150);
users.labelFunction = userLabelFunction;
function userLabelFunction(item:Object):String
{
return item.name;
}
users.dataProvider = new DataProvider([
new User("David"),
new User("Colleen"),
new User("Sharon"),
new User("Ronnie"),
new User("James")]);
// list of countries
var countries:List = new List();
addChild(countries);
countries.move(550,150); // adjut position as desired
countries.dataProvider = new DataProvider([
{label:"a"},
{label:"b"},
{label:"c"},
{label:"d"},
{label:"e"},
{label:"f"}]);
// events
users.addEventLsitener(Event.CHANGE, onUserSelected);
function onUserSelected(e:Event):void
{
countries.selectedItems = users.selectedItem.countries;
}
countries.addEventLsitener(Event.CHANGE, onCountrySelected);
function onCountrySelected(e:Event):void
{
users.selectedItem.countries = countries.selectedItems;
}
From what I understand this seems to work except for the fact that the names are already provided when the program starts. What I want is that the user adds the name themselves while the program is running.
You can add new items with the methods provided by the DataProvider class, like addItem() for example. Just add new User objects.

Create BandedGridView for DevExpress XtraGrid

I would like to know how the XtraGrid and the BandedGrid play togehter and are bound to the underlaying data. The documentation has some explanatory tl;dr-text but i am missing a full working example to set it up in code. So it took me about 2 hours to figure it out. Based on this blog entry
i would like to post my answer here.
If there is a better way to put the pieces together as in my answer below i would love to know about it.
First you have to know that you can bind a plain DataTable to the XtraGrid and that the creation of the banded grid is independent.
Below you can see a new instance of XtraGrid is created. It MainView is set to be a BandedGridView
private void LoadAndFillXtraGrid() // object sender, EventArgs e
{
grid = new DevExpress.XtraGrid.GridControl();
grid.Dock = DockStyle.Fill;
// set the MainView to be the BandedGrid you are creating
grid.MainView = GetBandedGridView();
// set the Datasource to a DataTable
grid.DataSource = GetDataTable();
// add the grid to the form
this.Controls.Add(grid);
grid.BringToFront();
}
Above the line grid.MainView = GetBandedGridView(); set a BandedGridView as the MainView of the Xtragrid. Below you see how to create this BandedGridView
//Create a Banded Grid View including the grindBands and the columns
private BandedGridView GetBandedGridView()
{
BandedGridView bandedView = new BandedGridView();
// Set Customer Band
SetGridBand(bandedView, "Customer",
new string[3] { "CustomerId", "LastName", "FirstName" });
SetGridBand(bandedView, "Address", new string[3] { "PLZ", "City", "Street" });
return bandedView;
}
To set up the GridBand you have to create a GridBand and attach it to the bandedGridView by calling bandedView.Columns.AddField for each column
private void SetGridBand(BandedGridView bandedView, string gridBandCaption
, string[] columnNames)
{
var gridBand = new GridBand();
gridBand.Caption = gridBandCaption;
int nrOfColumns = columnNames.Length;
BandedGridColumn[] bandedColumns = new BandedGridColumn[nrOfColumns];
for (int i = 0; i < nrOfColumns; i++)
{
bandedColumns[i] = (BandedGridColumn)bandedView.Columns.AddField(columnNames[i]);
bandedColumns[i].OwnerBand = gridBand;
bandedColumns[i].Visible = true;
}
}
The DataSource can be a plain DataTable that contains some columns. If the name of the column in the datatable matches the names of the BandedGridColumn the will be automatically mapped. As you can see i added a column NotMapped in the datatable which is not visible in the screenshot above:
private DataTable GetDataTable()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] {
new DataColumn("CustomerId", typeof(Int32)),
new DataColumn("NotMapped", typeof(Int32)),
new DataColumn("LastName", typeof(String)),
new DataColumn("FirstName", typeof(String)),
new DataColumn("PLZ", typeof(Int32)),
new DataColumn("City", typeof(String)),
new DataColumn("Street", typeof(String))
});
dt.Rows.Add(1, 0, "John", "Barista", 80245, "Manhatten", "Broadway");
dt.Rows.Add(2, 0, "Mike", "Handyman", 87032, "Brooklyn", "Martin Luther Drive");
dt.Rows.Add(3, 0, "Jane", "Teacher", 80245, "Manhatten", "Broadway 7");
dt.Rows.Add(4, 0, "Quentin", "Producer", 80245, "Manhatten", "Broadway 15");
return dt;
}
If someone has a more elegant way to put the pieces together i would love to know about it.

Resources