function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Preethi SPreethi 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;
}
}
Best Answer chosen by Preethi S
Ramu_SFDCRamu_SFDC
Hi Preethi,

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

Vinit_KumarVinit_Kumar
Try below code :-

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.Values()];

for(Account acc: accnt)
{
	acc.CustomAccountNumber__c = oppMap.get(acc.id).CustomOpportunityNumber__c;
}

If this helps,please mark it as best answer to help others :)
Ramu_SFDCRamu_SFDC
Hi Preethi,

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;
}

This was selected as the best answer
Vinit_KumarVinit_Kumar
Yes that's right.That was a typo,replace it with oppMap.keyset()
Preethi SPreethi S
Thank u Vinit.
Preethi SPreethi S
Thanks Ram..It is working...