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
pcmca_2007@yahoo.co.inpcmca_2007@yahoo.co.in 

Error on Opportunity Product insertion

Hi all,

           I am getting error on product insertion on Opportunity.apex class Update_OpportunityStageAndStatus_Class in which if you see line number 186,187 you will find getStage(lowest) having null value.The class file is too long,so

 i am sending it in two post.So please combine these two section into one and solve my problem

 

FIRST SECTION:

 

public with sharing class Update_OpportunityStageAndStatus_Class {

public static void updateOpenStatus(Map<Id, OpportunityLineItem> opplineitemMap)
  {
    System.debug('inside method');
    List <Opportunity> oppList = [Select id, StageName, INTL_Status__c from Opportunity where Id in :opplineitemMap.keySet()];
    System.debug('inside method' +oppList);
    if(oppList != null && oppList.size()>0)
    {
      for(Opportunity opp : oppList)
      {

        opp.INTL_Status__c = 'Open';
      }

      update oppList;
      System.debug('Opportunity are updated for status as open');
    }
  }

  private static integer assignWeight(String Stage)
  {
    Integer retString =0;
    if(Stage.contains('0') )
        retString =0;
    if(Stage.contains('1') )
        retString =1;
    if(Stage.contains('2') )
        retString =2;
    if(Stage.contains('3') )
        retString =3;
    if(Stage.contains('4') )
        retString =4;
    if(Stage.contains('5') )
        retString =5;
    if(Stage.contains('6') )
        retString =6;
    if(Stage.contains('7') )
        retString =7;
    if(Stage.contains('8') )
        retString =8;
    if(Stage.contains('9') )
        retString =9;
    return  retString;

  }

    private static String getStage(integer Weight)
    {
      String retString ='';

    if(Weight== 0)
          retString ='0. Identify';
    if(Weight== 1)
          retString ='1. Qualify';
    if(Weight== 2)
          retString ='2. First Contact';
    if(Weight== 3)
          retString ='3. Assess Needs';
    if(Weight== 4)
          retString ='4. Formal Offer';
    if(Weight== 5)
          retString ='5. Verbal Win';
    if(Weight== 6)
          retString ='6. Complete';
    if(Weight== 7 || Weight== 8 || Weight== 9 )
          retString ='6. Complete';
      return  retString;

      }

  public static void updateOpenStatus_2(List<OpportunityLineItem> opplineitemList , List<Opportunity> oppList,Map<ID, Opportunity> oppMap)
  {
        List<Opportunity> LastUpdateOppList = new List<Opportunity>();
        List<OpportunityLineItem> LineItembyOpp = new List<OpportunityLineItem>();
        List<Opportunity> updateOppList = new List<Opportunity>();
        Map<ID , List<OpportunityLineItem>> MapOppandLineItem = new  Map<ID , List<OpportunityLineItem>>();
        System.debug('inside method');

            for(Opportunity opp : oppList)
            {
                 List<OpportunityLineItem> tempList = new List<OpportunityLineItem>();

                 for(OpportunityLineItem oppListitem2 : opplineitemList)
                 {
                    if(oppListitem2.OpportunityId == opp.ID)
                    {
                        tempList.add(oppListitem2);
                    }
                 }
            //This map contains opportunity and list of opportunity line items under it
             MapOppandLineItem.put(opp.ID , tempList);
             system.debug('Map' +MapOppandLineItem);
            }

             for(ID oppID :  MapOppandLineItem.keySet())
             {  system.debug('inside for1');
                Opportunity opptoUpdate=oppMap.get(oppID);
                opptoUpdate.Flag__c = true;
                String oppStage=opptoUpdate.StageName;
               
                Integer lineItemSize = MapOppandLineItem.get(oppID).size();
                Integer RejectCount = 0;
                Integer lineItemCount = 0;
                Integer oppStageWt = assignWeight(oppStage);
                Boolean lineItemOpen = false;
                Boolean lineItemSigned = false;
                Boolean lineitemrejected = false;
                system.debug('opptoUpdatebeforeupdate' +oppStage);
                system.debug('stagein opp' +oppStageWt);
                String lineItemSatge = MapOppandLineItem.get(oppID)[0].stage__c;
                integer lowest = assignWeight(lineItemSatge);
                system.debug('loweststagebeforeiteration' + lowest);
                for(OpportunityLineItem oppLineItem : MapOppandLineItem.get(oppID))
                {   system.debug('inside for2');
                    Integer lineItemWt = assignWeight(oppLineItem.Stage__c);
                    system.debug('stagein opplineitem' +lineItemWt);
                    //if(oppLineItem.Status__c!='Rejected')
                      // {


         With Regards

Prabhash Mishra

pcmca_2007@yahoo.co.inpcmca_2007@yahoo.co.in

SECTION SECTION: Simply Past next to FIRST SECTION

 

 if(lineItemWt <= lowest)
                            {   system.debug('inside if for low stage');
                                lowest = lineItemWt;
                                //break;
                            }
                            if(oppLineItem.Status__c == 'Open')
                            {
                                lineItemOpen= true;
                                system.debug( 'inside open');
                                opptoUpdate.INTL_Status__c = 'Open';
                                system.debug('opp to be open' +opptoUpdate);
                                continue;
                            }
                             if(oppLineItem.Status__c == 'Signed')
                            {
                                lineItemSigned = true;

                            }

                      // }


                      // else if(oppLineItem.Status__c == 'Rejected')
                       if(oppLineItem.Status__c == 'Rejected')

                       {
                            System.debug('Reject Count' +  RejectCount);
                            RejectCount++;
                            System.debug('Reject Count after adding' +  RejectCount);
                            if(RejectCount == lineItemSize)
                            { 
                              opptoUpdate.INTL_Status__c = 'Rejected';
                             //lineitemrejected = true;
                               
                                //break;
                            }
                        }
                lineItemCount ++;
                }
                system.debug('loweststageafteriteration' + lowest);
                if(lineItemCount==lineItemSize && !lineItemOpen && lineItemSigned)
                {
                    system.debug('inside if for signed');
                    opptoUpdate.INTL_Status__c = 'Signed';
                    opptoUpdate.StageName = '6. Complete';
                    system.debug('opp to be signed' + opptoUpdate);
                }
               /* else if(lineitemrejected)
                {
                opptoUpdate.INTL_Status__c = 'Rejected';
                opptoUpdate.StageName = '6. Complete';
                }*/
               
                else
                {
                system.debug('Lowest stage is :::: '+ getStage(lowest));
                opptoUpdate.StageName = getStage(lowest);
                }
                system.debug('Updated Opportunity' + opptoUpdate);
                updateOppList.add(opptoUpdate);


             }

             if(updateOppList!=null && updateOppList.size()>0)
             update updateOppList;
            
             for(Opportunity LastUpdatedOpp : updateOppList)
             {
             LastUpdatedOpp.Flag__c = false;
             LastUpdateOppList.add(LastUpdatedOpp);
             }
            
             if(LastUpdateOppList!=null && LastUpdateOppList.size()>0)
             update LastUpdateOppList;
            

  }


}

With Regards

Prabhash Mishra