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
TilluTillu 

Unable to update opportunity record with trigger ?

From opportunity , while creating Task...if the Task is associated with opportunity via 'Related To'  field   Then  Opportunity fields need to populate some values and update the Opportunity record. I have tried but the Opportunity record was not updated.

Please check where i have missed ?

trigger TaskonOpp on Task (before insert,before update) {

set<Id> Tid = new set<id>();

for(Task t:trigger.new){
Tid.add(t.whatid);
}

List<Opportunity> ollist = [select id,name from opportunity where id in:Tid];

for(Task T : Trigger.new){
for(Opportunity O:ollist){

if(T.whatId==O.id&&T.RecordType.name=='Annuity Log a Call'&&T.Activity_Type__c == 'Voicemail'){
O.StageName = '3-Rep follow up - VM';
O.CloseDate = System.ToDay()+30;
}
if(T.whatId==O.id&&T.RecordType.name=='Annuity Log a Call'&&T.Activity_Type__c == 'Client Meeting - IW'){
O.StageName = '4-Client Meetings';
O.CloseDate = System.ToDay()+7;
}
if(T.whatId==O.id&&T.RecordType.name=='Annuity Log a Call'&&T.Activity_Type__c == 'Business drop'){
O.StageName = '5-Paperwork/Ticketing incoming';
O.CloseDate = System.ToDay()+7;
}
update ollist;
}
}
 
lakslaks
Hi,

Have you checked whether the trigger was invoked successfully/whether it gave any error ?
If so, please provide that information.

It would be a good idea to put in System.debug() statements in the code and check the debug logs so that you can zero in on the problem.

Regards,
Lakshmi.
TilluTillu
I have verified no errors while saving record or update/insert
lakslaks
Ok, in that case put relevant debug statements within the if loops and check if the control is going in any of them.

If the trigger is invoked successfully and still no value is updated, chances are it may not be entering any of the if loops which set the values to be assigned.

 
James LoghryJames Loghry
It's likely that your SOQL query is not returning any records, or that the query is returning records, but none match the three if checks you have in place.  Like laks mentioned, throw some System.debugs to print out the results of your SOQL query, and also in your second loop to print out each opportunity / task, so that you can watch the result of each if statement.

Furthermore, you could add a query to the beginning of your trigger to grab the Annuity Log a Call record type, and then filter out any irreleavant Tasks in your first for-loop.  This should help improve performance by just a bit.