I am working on one requirement and I got stuck at one place. I hope someone will help me to resolve my problem and guide me in proper directions.

My use case is as follows:

SCENARIO 1: Close Opportunity when Upgrade Order is cancelled 

GIVEN a a person account record in Salesforce AND the has an open upgrade opportunity record in Salesforce AND the opportunity record type equals "Upgrade Opportunity" AND the opportunity stage is not "Closed - Won" WHEN the recipient places an upgrade order AND the status of the upgrade order has been changed to Cancelled THEN the open upgrade opportunity Stage field is set to "Closed - Lost" AND all other related opportunities that also meet this scenario criteria (Upgrade Opportunity, not Closed-Won) are set to "Closed - Lost"

SCENARIO 2: Close Opportunity when Upgrade Order is shipped (ie Closed in Salesforce)
GIVEN a person account record in Salesforce
    AND thehas an opportunity record in Salesforce 
    AND the opportunity record type equals "Upgrade Opportunity"
    AND the stage of the opportunity record is not Closed-Won
    AND the stage of the opportunity record is not Closed-Lost (unless the stage was automatically set to Closed-Lost within the past 90 days due to an order cancellation)
WHEN the places an upgrade order
    AND the upgrade order has a status of Closed
THEN the open upgrade opportunity Stage field is set to "Closed - Won"
    AND all other related opportunities that also meet this scenario criteria (Upgrade Opportunity, not Closed-Won, not Closed-Lost unless automatically changed to Closed-Lost within 90 days) are set to "Closed - Won"

I have done 1st scenario and works well.For 2nd scenario also I have done almost everything except one scenario where I need to track "the stage of the opportunity record is not Closed-Lost (unless the stage was automatically set to Closed-Lost within the past 90 days due to an order cancellation)"

Below is my Apex Class which runs in before update from Apex Trigger:

public with sharing class OrderTriggerService extends TriggerService {


public void updateOpportunityStage(){
    Set<Id> setIds = new Set<Id>();
    Map<Id,Order> orderOldMap =(Map<Id,Order>)oldMap;
    Set<Opportunity>  updateOppties=new Set<Opportunity>();

    for (Order childObj : (List<Order>) newList) {

        if(childObj.Account_Record_Type_Name__c == 'INDIA' && childObj.Record_Type_Dev_Name__c == 'Upgrade_Order'){ 
        setIds.add(childObj.AccountId);

        }
    }
    if (setIds.isEmpty()) { return; }

 //Set<Opportunity>  updateOppties=new Set<Opportunity>();
  for(Account acc : [select id,Name
                        (selectId,Name,closedate,amount,StageName
                             from opportunities where 
                            Record_Type_Dev_Name__c='Upgrade_Opportunity') 
                                from Account where id in:setIds]){

                         for(Order orders : (List<Order>) newList){            
                                for(Opportunity opps : acc.opportunities){

                                    if(opps.StageName<>'Closed Won' && orders.Status!=orderOldMap.get(orders.Id).Status && orders.Status=='Cancelled'){
                                             opps.StageName='Closed Lost'; 


                                    }

                            else if((opps.StageName<>'Closed Won'||opps.StageName<>'Closed Lost')&& orders.Status!=orderOldMap.get(orders.Id).Status && orders.Status=='Closed'){         
                                        opps.StageName='Closed Won';

                             }

                            updateOppties.add(opps);
                         }
                         }
                     }

                    List<Opportunity> updateopps=new List<Opportunity(updateOppties);
                    update updateopps;

}
  }

I am not able to find a way to track opp.stagename<>'Closed Lost'(unless the stage was automatically set to Closed-Lost within the past 90 days due to an order cancellation) in my class.

Kindly help.

Any help will be greatly appreciated.

Many thanks in advance

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.