+ Start a Discussion
venkateshyadav1243venkateshyadav1243 

bulkifying the trigger

hi can any one tell me how can i bulky fy this trugger

 

when am inserting the opportunity through dat aloader am geeting error list has more than one row for assigne ment

 

this is my code

 


trigger Populating_CCY_Pair  on Opportunity (after insert,after update) {
List<opportunity> op=new List<opportunity>();
List<opportunity> opp=new List<opportunity>();
Set<id> accIds = new Set<Id>();
set<string> uniquerecords=new Set<string>();
string ccy_pair;
set<string> uniuqvalues=new set<String>();
op=[select id,name,Bought_CCY__c,CCY_Pair__c,Sold_CCY__c,accountid from opportunity where id=:trigger.new];

for(opportunity o:op)
{
if(o.accountid != null)
        {
            accIds.add(o.accountid);
        }
}
Account ac=[select id,name,CCY_Pairs_Traded__c from account where id=:accIds];
opp=[select id,name,Bought_CCY__c,CCY_Pair__c,Sold_CCY__c,accountid  from opportunity where accountid=:accIds];
ccy_pair='';
try{
for(Opportunity o1:opp)
{
if(!uniuqvalues.contains(o1.CCY_Pair__c.toUpperCase().trim()))
{
uniuqvalues.add(o1.CCY_Pair__c.toUpperCase().trim());
ccy_pair=ccy_pair+o1.CCY_Pair__c+';';
system.debug('uniuqvalues of valuews'+ccy_pair);
}

}
}catch(Exception e){system.debug('@@@'+e);}
//ac.CCY_Pairs_Traded__c=uniquerecords;
ac.CCY_Pairs_Traded__c=ccy_pair.toUpperCase().trim();

system.debug('account updated fields'+ac.CCY_Pairs_Traded__c);
update ac;
system.debug('account updated records'+ac);

}

 

 

am getting error an this line

 

Account ac=[select id,name,CCY_Pairs_Traded__c from account where id=:accIds];

 

AmitdAmitd

Hi Venkatesh,

 

Account ac=[select id,name,CCY_Pairs_Traded__c from account where id=:accIds];

 

Problem in this line is that, this SOQL can return more than one account record, where as you have used a single instace of Account to save SOQL query resultset. So, Please use this code to solve your issue.

 

trigger Populating_CCY_Pair on Opportunity (after insert,after update) {
List<opportunity> op=new List<opportunity>();
List<opportunity> opp=new List<opportunity>();
Set<id> accIds = new Set<Id>();
set<string> uniquerecords=new Set<string>();
string ccy_pair;
set<string> uniuqvalues=new set<String>();
op=[select id,name,Bought_CCY__c,CCY_Pair__c,Sold_CCY__c,accountid from opportunity where id=:trigger.new];

for(opportunity o:op)
{
if(o.accountid != null)
{
accIds.add(o.accountid);
}
}
Map<Id,Account> acmap= new Map<Id,Account>();

for(Account acc:[select id,name,CCY_Pairs_Traded__c from account where id=:accIds]){
acmap.put(acc.id,acc);
}
opp=[select id,name,Bought_CCY__c,CCY_Pair__c,Sold_CCY__c,accountid from opportunity where accountid=:accIds];
ccy_pair='';
try{
for(Opportunity o1:opp){
if(!uniuqvalues.contains(o1.CCY_Pair__c.toUpperCase().trim())){
uniuqvalues.add(o1.CCY_Pair__c.toUpperCase().trim());
ccy_pair=ccy_pair+o1.CCY_Pair__c+';';
system.debug('uniuqvalues of valuews'+ccy_pair);
acmap.get(o1.accountid).CCY_Pairs_Traded__c=ccy_pair.toUpperCase().trim();
}

}
}catch(Exception e){system.debug('@@@'+e);}
update acmap.values();

 

Pleae mark this as solution if it works for you. KUDOS