You need to sign in to do that
Don't have an account?
Preethi S
Opportunity insertion
Hi all,
i am new to apexcode.. If I insert one record in opportunity that opportunity number will be displayed on account number field. I tried these functionality by using trigger... But it shows Invalid bind expression error in line7.. Can u help me for this?
This is my trigger,
trigger OpportunityInsertion on Opportunity (after insert) {
List<Opportunity> listOfOpportunities = new List<Opportunity>();
for(Opportunity opp : Trigger.new)
{
listOfOpportunities.add(opp);
}
List<Account> accnt= [SELECT CustomAccountNumber__c FROM Account WHERE Id IN:listOfOpportunities];
List<Account> acc = new List<Account>();
for(Opportunity op : listOfOpportunities)
{
for(Account a: accnt)
{
if(a.Name == op.AccountName)
{
a.CustomAccountNumber__c = op.CustomOpportunityNumber__c;
}
acc.add(a);
}
update acc;
}
}
i am new to apexcode.. If I insert one record in opportunity that opportunity number will be displayed on account number field. I tried these functionality by using trigger... But it shows Invalid bind expression error in line7.. Can u help me for this?
This is my trigger,
trigger OpportunityInsertion on Opportunity (after insert) {
List<Opportunity> listOfOpportunities = new List<Opportunity>();
for(Opportunity opp : Trigger.new)
{
listOfOpportunities.add(opp);
}
List<Account> accnt= [SELECT CustomAccountNumber__c FROM Account WHERE Id IN:listOfOpportunities];
List<Account> acc = new List<Account>();
for(Opportunity op : listOfOpportunities)
{
for(Account a: accnt)
{
if(a.Name == op.AccountName)
{
a.CustomAccountNumber__c = op.CustomOpportunityNumber__c;
}
acc.add(a);
}
update acc;
}
}
In Vinit's code please change line # 11 as below (it shoudl be keyset() not Values())
List<Account> accnt= [SELECT Id,CustomAccountNumber__c FROM Account WHERE Id IN:oppMap.keyset()];
Hence the new code would be
trigger OpportunityInsertion on Opportunity (after insert) {
Map<Id,Opportunity> oppMap = new Map<Id,Opportunity>();
for(Opportunity opp : Trigger.new)
{
if(opp.AccountId!=null)
{
oppMap.put(opp.AccountId,opp);
}
}
List<Account> accnt= [SELECT Id,CustomAccountNumber__c FROM Account WHERE Id IN:oppMap.keyset()];
for(Account acc: accnt)
{
acc.CustomAccountNumber__c = oppMap.get(acc.id).CustomOpportunityNumber__c;
}
All Answers
If this helps,please mark it as best answer to help others :)
In Vinit's code please change line # 11 as below (it shoudl be keyset() not Values())
List<Account> accnt= [SELECT Id,CustomAccountNumber__c FROM Account WHERE Id IN:oppMap.keyset()];
Hence the new code would be
trigger OpportunityInsertion on Opportunity (after insert) {
Map<Id,Opportunity> oppMap = new Map<Id,Opportunity>();
for(Opportunity opp : Trigger.new)
{
if(opp.AccountId!=null)
{
oppMap.put(opp.AccountId,opp);
}
}
List<Account> accnt= [SELECT Id,CustomAccountNumber__c FROM Account WHERE Id IN:oppMap.keyset()];
for(Account acc: accnt)
{
acc.CustomAccountNumber__c = oppMap.get(acc.id).CustomOpportunityNumber__c;
}