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
Bayarea 101Bayarea 101 

need help to write a trigger for opoortunity

Hi there,
i am new to Apex triggers. I need to write a trigger for Opportunity object. The logic is when any update is made on opportuntiy object. means if a user make change to an opportuntiy object certain fields should be get update based on the value of other object Credit App.
Opportunity Fields to be updated:
1. Broker_Firm__c
2. Broker_Firm_Office__c
3. Primary_Contact_Role__c
I am trying to it thru this Apex trigger please let me know if i am doing soemthing because it is no compiling
trigger UpdateOpportunity on Opportunity (After update)
//Map broker related opportunity fields to keep credit App up to date.
{
Opportunity Opp = [Select broker_firm__c, broker_firm_office__c, primary_contact_role__c from Opportunity WHERE ID In:Trigger.newMap.keySet()];
Underwriting__c U=[Select Opportunity__c from Underwriting__c where Opportunity_Id__c =:Opp.Id];

For (Opportunity O:Trigger.new){        
                 U.broker_firm__c = o.broker_firm__c;
                 U.broker_firm_office__c = o.broker_firm_office__c;               
                 U.broker_name__c = o.primary_contact_role__c;
                 }
}
 
Cyrus TalladenCyrus Talladen
It looks like the syntax is correct I dont see why the compiler is not compiling your code.  If you can add the error and the stack trace as well?  Also dont forget to update your opportunity at the end of the loop.


Cyrus Talladen
CRM Engineer
www.levementum.com
Bayarea 101Bayarea 101
here is the error message it generates.
Error:Apex trigger UpdateOpportunity caused an unexpected exception, contact your administrator: UpdateOpportunity: execution of AfterUpdate caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.UpdateOpportunity: line 5, column 1
Cyrus TalladenCyrus Talladen
The syntax is correct you are on the right track.  But on runtime the query that you have maybe returning null.  Try adding this line in the for loop: 
if (Opp != null){
 For (Opportunity O:Trigger.new){        
                 U.broker_firm__c =o.broker_firm__c;
                 U.broker_firm_office__c = o.broker_firm_office__c;               
                 U.broker_name__c = o.primary_contact_role__c;
                 }
}

And also add debug logs after the query to make sure that it is returning what you like.

Cyrus T
www.levementum.com
Bayarea 101Bayarea 101
Still same error
Error:Apex trigger UpdateOpportunity caused an unexpected exception, contact your administrator: UpdateOpportunity: execution of AfterUpdate caused by: System.QueryException: List has no rows for assignment to SObject: Trigger.UpdateOpportunity: line 5, column 1
Cyrus TalladenCyrus Talladen
Can you inspect the result of your debug statements?  I would like to know what results you are getting with the query.
 
System.debug('############### opp' + Opp);

System.debug('############### U' + U);

 
ra811.3921220580267847E12ra811.3921220580267847E12
Hi,

trigger UpdateOpportunity on Opportunity (before update)

{
Opportunity Opp = [Select broker_firm__c, broker_firm_office__c, primary_contact_role__c from Opportunity WHERE ID In:Trigger.newMap.keySet()];
List<Underwriting__c> U=[Select Opportunity__c from Underwriting__c where Opportunity_Id__c =:Opp.Id];

if(u.isEmpty())
return;
List<Opportunity> opptoUpdate = new List<Opportunity>();
For (Opportunity O:Trigger.new){        
                 O.broker_firm__c = U[0].broker_firm__c;
                 O.broker_firm_office__c = U[0].broker_firm_office__c;               
                 O.broker_name__c = U[0].primary_contact_role__c;
                 
                 opptoUpdate.add(O);
                 }
                 
                 if(opptoUpdate.size()>0)
                 update opptoUpdate;
}
Bayarea 101Bayarea 101
hi there when i update an opportunity nothing happens to the credit app object. It is not updating the object