Here is the task in one of the tutorials of Trail head. But, after trying that out, the error "no viable alternative at character ' ' " has been occurring no matter what so ever changes were made.
here's the code :
trigger ClosedOpportunityTrigger on Opportunity (after insert, after update) {
List<Task> taskList= new List<Task>();
for(Opportunity opp : Trigger.new) {
//Only create Follow Up Task only once when Opp StageName is to 'Closed Won' on Create
if(Trigger.isInsert) {
if(Opp.StageName == 'Closed Won') {
taskList.add(new Task(Subject = 'Follow Up Test Task', WhatId = opp.Id));
}
}
//Only create Follow Up Task only once when Opp StageName changed to 'Closed Won' on Update
if(Trigger.isUpdate) {
if(Opp.StageName == 'Closed Won'
&& Opp.StageName != Trigger.oldMap.get(opp.Id).StageName) {
taskList.add(new Task(Subject = 'Follow Up Test Task', WhatId = opp.Id));
}
}
}
if(taskList.size()>0) {
insert taskList;
}
}
what can be the main cause for that error?
Thanks in advance :)
Related
I'd like to write and trigger using Salesforce best practise that will link all contacts from the related account that are flagged as key contacts to an opportunity on that account when it is created.
What I already did:
trigger OppCreatedKeyContactTrigger on Opportunity (after insert, after Update) {
for(Opportunity opp : Trigger.New){
if(Trigger.isInsert || Trigger.isUpdate){
Contact con = [SELECT Id,Name,Key_Contact__c FROM Contact WHERE AccountId =:opp.AccountId];
con.Key_Contact__c = true;
update con;
}
}
}
I also need a test class
Do you use OpportunityContactRole (standard related list under opportunities but might have to be enabled in setup) or do you have 1 or more custom lookups from Opportunity to Contact? What should happen "after update"?
Something like this should push you in right direction. I don't guarantee it'll compile, if you get stuck edit the question. It'll be just 1 query, not in a loop (bulk-safe). If you want to really go with best practices - next step would be to use some trigger handler framework.
trigger OpportunityTrigger on Opportunity (after insert) {
Set<Id> accountIds = new Set<Id>();
for(Opportunity o : trigger.new){
accountIds.add(o.AccountId);
}
accountIds.remove(null);
if(!accountIds.isEmpty()){
Map<Id, Account> accounts = new Map<Id, Account>([
SELECT Id,
(SELECT Id FROM Contacts WHERE Key_Contact__c = true)
FROM Account
WHERE Id IN :accountIds
]);
List<OpportunityContactRole> roles = new List<OpportunityContactRole>();
for(Opportunity o : trigger.new){
Account a = accounts.get(o.AccountId);
if(a != null && !a.Contacts.isEmpty()){
for(Contact c : a.Contacts){
roles.add(new OpportunityContactRole(
OpportunityId = o.Id,
ContactId = c.Id,
Role = 'Decision Maker'
));
}
}
}
insert roles;
}
}
trigger OppCreatedKeyContactTrigger on Opportunity (after insert, after Update) {
Set<Id> accountIds = new Set<Id>();
for(Opportunity o : trigger.new){
accountIds.add(o.AccountId);
}
accountIds.remove(null);
List<Contact> conList = [SELECT Id,Name,Key_Contact__c FROM Contact WHERE AccountId IN :accountIds];
List<contact> updateConList = new List<contact>();
if(Trigger.isInsert || Trigger.isUpdate){
for(Contact cons : conList){
updateConList.add(new contact(id=cons.id,Key_Contact__c=true));
}
}
if(updateConList.size()>0){
update updateConList;
}
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have a table like this. Id1 and Id2 are the reference of Id column
Id Name Id1 Id2
1 X 3 4
2 Y 2 1
3 Z 4 2
4 P 1 3
I need Output like this
Id Name Id1 Id2
1 X Z P
2 Y Y X
3 Z P Y
4 P X Z
Can some one help me to write a query using joins only
Try this:
SELECT * INTO #Tbl1
FROM (VALUES
(1 ,' X ',3 , 4),
(2 ,' Y ',2 , 1),
(3 ,' Z ',4 , 2),
(4 ,' P ',1 , 3))
x(Id ,Name,Id1 ,Id2)
SELECT t.id, t.Name, t1.Name, t2.Name
FROM #Tbl1 as t
INNER JOIN #Tbl1 as t1 ON t.Id1 = t1.Id
INNER JOIN #Tbl1 as t2 ON t.Id2 = t2.Id
I'm trying to create a Azure database using a master database as a template using C#. I have the database creation working but I cannot find a way to specify the resource group for the new database.
The below code snippet requires Windows Azure management class library.
If you have not installed it, run the below command in Package Manager Console
PM> Install-Package Microsoft.WindowsAzure.Management.Libraries -Pre
The code below shows how to get a subscription of cloud credentials using C#.
public static string subscriptionId = "{Your-Subscription-id}";
public static string base64EncodedCertificate = "Your-certificate-Base64-string";
static SubscriptionCloudCredentials getCredentials()
{
return new CertificateCloudCredentials(subscriptionId, new X509Certificate2(Convert.FromBase64String(base64EncodedCertificate)));
}
The code below shows how to create a SQL database using C# code.
static void Main(string[] args)
{
SqlManagementClient client = new SqlManagementClient(getCredentials());
//Server Name is your SQL Azure DataBase server name, for example:mysqlserver001
client.Databases.Create("{Server-Name}", new Microsoft.WindowsAzure.Management.Sql.Models.DatabaseCreateParameters()
{
Name = "SqlDBTest",
MaximumDatabaseSizeInGB = 1,
CollationName = "SQL_Latin1_General_CP1_CI_AS",
Edition="Web"
});
Console.ReadLine();
}
Looking to write a note application in WPF/C# MVVM, not because we need another one but because it'll help me go over a few things I'm trying to become more familiar with.
My question is regarding some guidance on how to handle multiple windows. For example there will be multiple notes not tied to a main window. I'd like to keep track of all open windows, for example if one received focus, I may want to bring the other note windows to the front, but not remove focus from the one I selected, not looking for anyone to give me the code, just some guidance on how to handle it.
Maybe this can help you in some way: http://kentb.blogspot.nl/2011/11/application-im-currently-working-on-top.html
The important parts from the article:
What I did instead was had the WindowItemsControl create WindowItemsControlItem instances as containers. These containers are really just surrogates for the Window they represent. When they're initialized, they display the Window. When they're destroyed, they close the Window. In addition, if a Window is closed ahead of time, the corresponding data item is removed from the underlying collection and thus too the surrogate from the visual tree.
public class WindowItemsControl : ItemsControl
{
public static readonly DependencyProperty ShowDialogProperty = DependencyProperty.Register(
"ShowDialog",
typeof(bool),
typeof(WindowItemsControl));
public static readonly DependencyProperty OwnerProperty = DependencyProperty.Register(
"Owner",
typeof(Window),
typeof(WindowItemsControl),
new FrameworkPropertyMetadata(OnOwnerChanged));
public static readonly DependencyProperty WindowStartupLocationProperty = DependencyProperty.Register(
"WindowStartupLocation",
typeof(WindowStartupLocation),
typeof(WindowItemsControl));
public static readonly DependencyProperty RemoveDataItemWhenWindowClosedProperty = DependencyProperty.Register(
"RemoveDataItemWhenWindowClosed",
typeof(bool),
typeof(WindowItemsControl),
new FrameworkPropertyMetadata(true));
static WindowItemsControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(WindowItemsControl), new FrameworkPropertyMetadata(typeof(WindowItemsControl)));
}
public bool ShowDialog
{
get { return (bool)this.GetValue(ShowDialogProperty); }
set { this.SetValue(ShowDialogProperty, value); }
}
public Window Owner
{
get { return this.GetValue(OwnerProperty) as Window; }
set { this.SetValue(OwnerProperty, value); }
}
public WindowStartupLocation WindowStartupLocation
{
get { return (WindowStartupLocation)this.GetValue(WindowStartupLocationProperty); }
set { this.SetValue(WindowStartupLocationProperty, value); }
}
public bool RemoveDataItemWhenWindowClosed
{
get { return (bool)this.GetValue(RemoveDataItemWhenWindowClosedProperty); }
set { this.SetValue(RemoveDataItemWhenWindowClosedProperty, value); }
}
protected override DependencyObject GetContainerForItemOverride()
{
return new WindowItemsControlItem(this);
}
protected override bool IsItemItsOwnContainerOverride(object item)
{
return item is WindowItemsControlItem;
}
protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
(element as WindowItemsControlItem).Window.Content = item;
}
protected override bool ShouldApplyItemContainerStyle(DependencyObject container, object item)
{
// the item container style will be applied to the windows, not to the containers (which are surrogates for the window)
return false;
}
private static void OnOwnerChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
var windowItemsControl = (WindowItemsControl)dependencyObject;
var owner = (Window)e.NewValue;
for (var i = 0; i < windowItemsControl.Items.Count; ++i)
{
var container = windowItemsControl.ItemContainerGenerator.ContainerFromIndex(i) as WindowItemsControlItem;
if (container == null)
{
continue;
}
container.Window.Owner = owner;
}
}
}
it declares some properties (ShowDialog, Owner, WindowStartupLocation) that assist it in the display of child Windows
it declares a RemoveDataItemWhenWindowClosed property that can be used to prevent the control from removing data items when a window is closed. This can be useful in shutdown or other situations where windows are being closed programmatically rather than by the user
I don't apply the ItemContainerStyle to the containers themselves, but instead hold out so that I can apply them to the Windows they represent
I also make sure that any change of Owner is applied to any existing Windows
the default style is overridden to remove unnecessary stuff like the Border, because the WindowItemsControl will never actually be visible on screen
public class WindowItemsControlItem : FrameworkElement
{
private readonly WindowItemsControl windowItemsControl;
private readonly Window window;
static WindowItemsControlItem()
{
// there is no need for these items to be visible as they are simply surrogates for the windows that they display
VisibilityProperty.OverrideMetadata(typeof(WindowItemsControlItem), new FrameworkPropertyMetadata(Visibility.Collapsed));
}
public WindowItemsControlItem(WindowItemsControl windowItemsControl)
{
windowItemsControl.AssertNotNull("windowItemsControl");
this.windowItemsControl = windowItemsControl;
this.window = this.CreateWindow(windowItemsControl);
this.Loaded += delegate
{
if (this.windowItemsControl.ShowDialog)
{
this.window.ShowDialog();
}
else
{
this.window.Show();
}
};
this.Unloaded += delegate
{
this.window.Close();
};
}
public Window Window
{
get { return this.window; }
}
private Window CreateWindow(WindowItemsControl windowItemsControl)
{
var window = new Window
{
Owner = windowItemsControl.Owner,
WindowStartupLocation = windowItemsControl.WindowStartupLocation
};
BindingOperations.SetBinding(window, Window.DataContextProperty, new Binding("Content") { Source = window });
BindingOperations.SetBinding(window, Window.StyleProperty, new Binding("ItemContainerStyle") { Source = windowItemsControl });
BindingOperations.SetBinding(window, Window.ContentTemplateProperty, new Binding("ItemTemplate") { Source = windowItemsControl });
BindingOperations.SetBinding(window, Window.ContentTemplateSelectorProperty, new Binding("ItemTemplateSelector") { Source = windowItemsControl });
window.Closed += delegate
{
// orphan the content because it might be hosted somewhere else later (including in another window)
window.Content = null;
// if the window closes, attempt to remove the original item from the underlying collection, which will result in this surrogate being removed too
if (windowItemsControl.RemoveDataItemWhenWindowClosed)
{
var editableItems = windowItemsControl.Items as IEditableCollectionView;
if (editableItems != null && editableItems.CanRemove)
{
editableItems.Remove(this.DataContext);
}
}
};
return window;
}
}
relevant properties on the WindowItemsControl are bound to the correct properties on the Windows themselves
Windows are displayed when the surrogate is initialized, and closed when the surrogate is unloaded
as mentioned earlier, Windows that are closed before the surrogate is destroyed (perhaps by the user clicking the close button) result in the related data item in the underlying collection being removed (unless the RemoveDataItemWhenWindowClosed property has been set to false). This, in turn, will cause the surrogate to be removed from the visual tree. In other words, if I close a widget Window, the corresponding WidgetViewModel will be removed from my collection of widget view models. Then, the ItemsControl will remove the related surrogate container from the visual tree.
I'm trying to migrate my app from PHP and RDBMS (MySQL) to Google App Engine and have a hard time figuring out data model and relationships in JDO. In my current app I use a lot of JOIN queries like:
SELECT users.name, comments.comment
FROM users, comments
WHERE users.user_id = comments.user_id
AND users.email = 'john#example.com'
As I understand, JOIN queries are not supported in this way so the only(?) way to store data is using unowned relationships and "foreign" keys. There is a documentation regarding that, but no useful examples. So far I have something like this:
#PersistenceCapable
public class Users {
#PrimaryKey
#Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
#Persistent
private String name;
#Persistent
private String email;
#Persistent
private Set<Key> commentKeys;
// Accessors...
}
#PersistenceCapable
public class Comments {
#PrimaryKey
#Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
#Persistent
private String comment;
#Persistent
private Date commentDate;
#Persistent
private Key userKey;
// Accessors...
}
So, how do I get a list with commenter's name, comment and date in one query? I see how I probably could get away with 3 queries but that seems wrong and would create unnecessary overhead. Please, help me out with some code examples.
--
Paul.
Can only comment on what is valid JDOQL for that
SELECT this.name, commentVar.comment FROM mydomain.Users
WHERE this.key == commentVar.userKey && this.email = 'john#example.com'
VARIABLES mydomain.Comments commentVar
whether GAE/J deems to implement it is for Google to answer.