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
trictric 

Trigger Problem

 

Hi ,

 

I have trigger before update on opportunity.I am fetching all the opportunities related to particular account .I am retrievung highest amount value on the opporunity and then updating somefield on the acocunt object

 

I have not been able to update.How do I write Dml statement at the end of the code.When I try to write I get an error .Null pointer exceptionon the line in red

 

Can somebody please help me?

 

 

Trigger Target on Opportunity (before update)
{

set<id> sup=new set<id>();
for(opportunity opp:trigger.new)
{

sup.add(opp.accountid);

}
list<account>dry;
Account[]acc=[select id,refereed__c,(select name,amount from opportunities order by amount desc)from account where id in:sup];  
for(account lope:acc)
{

for(integer i=0; i<lope.opportunities.size();i++)
{

lope.label__c=lope.opportunities[0].name;
//update acc;

System.debug('The value in the variable of tupoe acocunt is'+lope.refereed__c);
//dry.add(lope);
}
}
//update dry;
}

 

Best Answer chosen by Admin (Salesforce Developers) 
steve456steve456

Take the line update dry and try once

 

bcos you are writing the code on before update and you dont need again an update dry before the record is being saved

All Answers

kiranmutturukiranmutturu

try this



set<id> sup=new set<id>();
for(opportunity opp:trigger.new)
{

sup.add(opp.accountid);

}
list<account>dry = new list<account>();;
Account[]acc=[select id,refereed__c,(select name,amount from opportunities order by amount desc)from account where id in:sup];  
for(account lope:acc)
{

for(integer i=0; i<lope.opportunities.size();i++)
{

lope.label__c=lope.opportunities[0].name;
dry.add(lope);
}
}
update dry;
}

kiranmutturukiranmutturu

u can try this code

Trigger target on Opportunity (before update)
{

set<id> sup=new set<id>();
for(opportunity opp:trigger.new)
{

sup.add(opp.accountid);

}
list<account>dry = new list<account>();
Account[]acc=[select id,(select name,amount from opportunities where amount != null order by amount desc limit 1) from account where id in:sup];
for(account lope:acc)
{
system.debug('lope.opportunities.size()'+lope.opportunities.size()+'opps'+lope.opportunities);
if(lope.opportunities.size() > 0){
system.debug('name'+lope.opportunities[0].name+'amount'+lope.opportunities[0].amount);
lope.previous_rating__c = lope.opportunities[0].name;
//System.debug('The value in the variable of tupoe acocunt is'+lope.refereed__c);
dry.add(lope);
}
}
update dry;
}

trictric

Hi Kiran,

 

When I put update DML  statement below is what happens .Any thoughts on this.  

 

Apex trigger target caused an unexpected exception, contact your administrator: target: execution of BeforeUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 001U0000007uQnvIAE; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Tiger1: maximum trigger depth exceeded Opportunity trigger event BeforeUpdate for [006U0000004xGer] Account trigger event AfterUpdate for [001U0000007uQnv] Account trigger event AfterUpdate for [001U0000007uQnv] Account trigger event AfterUpdate for [001U0000007uQnv] Account trigger event AfterUpdate for [001U0000007uQnv] Account trigger event AfterUpdate for [001U0000007uQnv] Account trigger event AfterUpdate for [001U0000007uQnv] Account trigger event AfterUpdate for [001U0000007uQnv] Account trigger event AfterUpdate for [001U0000007uQnv] Account trigger event AfterUpdate for [001U0000007uQnv] Account trigger event AfterUpdate for [001U0000007uQnv] Account trigger event AfterUpdate for [001U0000007uQnv] Account trigger event AfterUpdate for [001U0000007uQnv] Account trigger event AfterUpdate for [001U0000007uQnv] Account trigger event AfterUpdate for [001U0000007uQnv] Account trigger event AfterUpdate for [001U0000007uQnv]: []: Trigger.target: line 20, column 1

 

 

Thanks,

Tric

steve456steve456

Take the line update dry and try once

 

bcos you are writing the code on before update and you dont need again an update dry before the record is being saved

This was selected as the best answer
trictric

Hi Steve,

 

 

I also have same understanding of before update trigger.If i take away update statement then it  doesn't update my field on the account object..If I put it it kind of runs recursively and give an exception.

 

 

So how do I update referred field on the accouint object from opporunitty before update trigger.

 

Thanks,

Trick

kiranmutturukiranmutturu

u have a trigger on account object na..? u need to consider than also.. this update will invoke that trigger and the logic inside may lead to recursive loop......so find out that