You need to sign in to do that
Don't have an account?
Manish Anand 10
Trigger:-Error while updating related record
Hi There,
I am writing a trigger to update a opporutnity related record, if any new /updated account doesn't have a one.
trigger AddRelatedRecord on Account (after insert,after update) {
List<Opportunity> oppList = new List<Opportunity>();
// Get the related opportunities for the accounts in this trigger
Map<Id,Account> acctsWithOpps = new Map<Id,Account>(
[SELECT Id,(SELECT Id FROM Opportunities) FROM Account WHERE Id IN :Trigger.New]);
// Add an opportunity for each account if it doesn't already have one.
// Iterate through each account.
for(Account a : Trigger.New) {
System.debug('acctsWithOpps.get(a.Id).Opportunities.size()=' + acctsWithOpps.get(a.Id).Opportunities.size());
// Check if the account already has a related opportunity.
if (acctsWithOpps.get(a.Id).Opportunities.size() == 0) {
// If it doesn't, add a default opportunity
oppList.add(Name=a.Account + ' Opportunity',
StageName='Prospecting',
CloseDate=System.today().addMonths(1),AccountId=a.Id);
}
}
if (oppList.size() > 0) {
insert oppList;
}
}
It throws an error in line 15: Variable doesn't exist:Name.
What I am missing here?
I am writing a trigger to update a opporutnity related record, if any new /updated account doesn't have a one.
trigger AddRelatedRecord on Account (after insert,after update) {
List<Opportunity> oppList = new List<Opportunity>();
// Get the related opportunities for the accounts in this trigger
Map<Id,Account> acctsWithOpps = new Map<Id,Account>(
[SELECT Id,(SELECT Id FROM Opportunities) FROM Account WHERE Id IN :Trigger.New]);
// Add an opportunity for each account if it doesn't already have one.
// Iterate through each account.
for(Account a : Trigger.New) {
System.debug('acctsWithOpps.get(a.Id).Opportunities.size()=' + acctsWithOpps.get(a.Id).Opportunities.size());
// Check if the account already has a related opportunity.
if (acctsWithOpps.get(a.Id).Opportunities.size() == 0) {
// If it doesn't, add a default opportunity
oppList.add(Name=a.Account + ' Opportunity',
StageName='Prospecting',
CloseDate=System.today().addMonths(1),AccountId=a.Id);
}
}
if (oppList.size() > 0) {
insert oppList;
}
}
It throws an error in line 15: Variable doesn't exist:Name.
What I am missing here?
Use the below statement,
for(Account a : Trigger.New) {
System.debug('acctsWithOpps.get(a.Id).Opportunities.size()=' + acctsWithOpps.get(a.Id).Opportunities.size());
// Check if the account already has a related opportunity.
if (acctsWithOpps.get(a.Id).Opportunities.size() == 0) {
// If it doesn't, add a default opportunity
oppList.add(Name=a.name + ' Opportunity',
StageName='Prospecting',
CloseDate=System.today().addMonths(1),AccountId=a.Id);
}
}
Let me know if you need any help regarding this.
Thanks,
Vijay
Use the below code,
Let me know if you need any help regarding this
Thanks,
Vijay
The first query gives the same error-Variable doesn't exist:Name
The second query gives error-Invalid Constructor Syntax,name=Value pairs can only be used for Sobjects.
Please take the below code which covers all scenarios and bulkified trigger.
Also added the comments so that it will be easy for you to understand.
Regards,
Mahesh
I also tested the above code in my DE environment and everything is working properly.
Regards,
Mahesh
Above code throws an error-Variable Doesn't exist:AccountID, in line11.
Regards,
Manish.
I tested it im org and it is working fine.
Please check if if you org is enabled Person Accounts?
Regards,
Mahesh
Could you please check one of my VF and Apex question in the below link?
https://developer.salesforce.com/forums/ForumsMain?id=906F0000000DBaZIAW
This is unsolved, so far. It would be great, if you could, please provide some of tips/hints/solution for this.
Regards,
Manish.
Please paste your latest code here.
I would like to see what was the error, because as I tested in my environment why it didn't work for you.
Regards,
Mahesh