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
REKREK 

regarding apex trigger

My requirement as follows...

 

 

 

1. There are 2 fields on Oppty - Stage and Disposition Reason. Disposition Reason is dependent on Stage. (already built)

2. The following action should happen whenever Stage is any of the values with the word "Closed", so closed won, closed lost etc.

3.  Say I choose Stage as Closed Won, and Disposition as Cyhicle.
4. On the Oppty Prod Line item related list i.e (Products), all products where Primary Quote = TRUE, the Stage and Disposition Reason should be updated with the values chosen on the Oppty. Please note ALL line items should be updated and the condition is Primary Quote field is checked. So, prodcut should also have Stage as Closed Won, and Disposition as Cycle.

Hint - there should be a apex trigger written on the Oppty Object and it should run on AFTER INSERT and AFTER UPDATE functions.

Thanks

 

Rekha

Best Answer chosen by Admin (Salesforce Developers) 
SargeSarge

Hi Rekha,

 

Ther are two ways to achive this

 

1) Use 2 formula fields (Most preffered)

 

For your formula field  "Stage" in Opportunity Product

 

IF( 
     AND(
          CONTAINS(Opportunity.StageName,"Closed"),
          Primary_Quote__c
      ),
 
      Opportunity.StageName,
      ""
    
)

 

Formula for "Disposition Reason" field in Opportunity Products

 

IF( 
     AND(
          CONTAINS(Opportunity.StageName,"Closed"),
          Primary_Quote__c
      ),
 
      Opportunity.Disposition_Reason__c,
      ""
    
)

 

 

 

 

2)Use the Apex trigger as below (not likely to be preffered)

 

 

Here is the code. Change the field names as necessary

 

trigger trickleDownValuesToProduct on Opportunity (after insert, after update){

//Have a map variable ready
Map<Id,Opportunity> oppMap = trigger.newMap;

//collect all Opportunity with stage having "Closed"
List<Id> oppIds = new List<Id>();
for(Opportunity opp : trigger.new){
if(opp.StageName.contains('Closed')){
oppIds.add(opp.Id);
}
}

//collect all opportunity products of opportunity in context and the criteria
List<OpportunityLineItem> oliToUpdate = [Select Id,Stage__c,Disposition_Reason__c,OpportunityId from OpportunityLineItem where OpportunityId IN :oppIds AND Primary_Quote__c = true];
for(OpportunityLineItems oli : oliToUpdate )
{
//change opportunity product record's fields
oli.Stage__c = oppMap.get(oli.OpportunityId).StageName;
oli.Disposition__c = oppMap.get(oli.OpportunityId).Disposition_Reason__ c;
}

if(!oliToUpdate.isEmpty()){
try{
update oliToUpdate;
}catch(DmlException d){
trigger.new[0].addError('Some problem occured during updating Products');
System.debug('***DML Exception Caught****\n'+d+"\nHere are the Products'+\n+ oliToUpdate);
}catch(Exception e){
trigger.new[0].addError('Some problem occured during updating products');
System.debug('***Exception Caught***\n'+e+'\nHere are the products'+\noliToUpdate);
}
}
}

 

Hope this helps

Cheers...

All Answers

SargeSarge

Hi Rekha,

 

Ther are two ways to achive this

 

1) Use 2 formula fields (Most preffered)

 

For your formula field  "Stage" in Opportunity Product

 

IF( 
     AND(
          CONTAINS(Opportunity.StageName,"Closed"),
          Primary_Quote__c
      ),
 
      Opportunity.StageName,
      ""
    
)

 

Formula for "Disposition Reason" field in Opportunity Products

 

IF( 
     AND(
          CONTAINS(Opportunity.StageName,"Closed"),
          Primary_Quote__c
      ),
 
      Opportunity.Disposition_Reason__c,
      ""
    
)

 

 

 

 

2)Use the Apex trigger as below (not likely to be preffered)

 

 

Here is the code. Change the field names as necessary

 

trigger trickleDownValuesToProduct on Opportunity (after insert, after update){

//Have a map variable ready
Map<Id,Opportunity> oppMap = trigger.newMap;

//collect all Opportunity with stage having "Closed"
List<Id> oppIds = new List<Id>();
for(Opportunity opp : trigger.new){
if(opp.StageName.contains('Closed')){
oppIds.add(opp.Id);
}
}

//collect all opportunity products of opportunity in context and the criteria
List<OpportunityLineItem> oliToUpdate = [Select Id,Stage__c,Disposition_Reason__c,OpportunityId from OpportunityLineItem where OpportunityId IN :oppIds AND Primary_Quote__c = true];
for(OpportunityLineItems oli : oliToUpdate )
{
//change opportunity product record's fields
oli.Stage__c = oppMap.get(oli.OpportunityId).StageName;
oli.Disposition__c = oppMap.get(oli.OpportunityId).Disposition_Reason__ c;
}

if(!oliToUpdate.isEmpty()){
try{
update oliToUpdate;
}catch(DmlException d){
trigger.new[0].addError('Some problem occured during updating Products');
System.debug('***DML Exception Caught****\n'+d+"\nHere are the Products'+\n+ oliToUpdate);
}catch(Exception e){
trigger.new[0].addError('Some problem occured during updating products');
System.debug('***Exception Caught***\n'+e+'\nHere are the products'+\noliToUpdate);
}
}
}

 

Hope this helps

Cheers...

This was selected as the best answer
REKREK

Hi....:smileysurprised:

 

 

Thanks for u r solution ,

according to your example i did modifications for my requirement

 

 

 

 

Very Very Thanks..............