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
DolgoldyDolgoldy 

Another Script tried

HI All,

Here is another script tried to insert 3 related record for opprtunity for 3 month (Jan,feb,March). But not working can anyone help please.
Script
Map <Id, Opportunity> oppItems = new Map <Id, Opportunity>();
       
    list<ARRCalcualateOpportunity__c> Arropplist= new list<ARRCalcualateOpportunity__c>();
        list<Opportunity> opplist= new list<Opportunity>();
    for(opportunity opp:opplist){
       if (opp.StageName == 'Closed Won'&& opp.closeDate.month() >=1 && opp.closeDate.month() <=3 ) 
        {
          oppItems.put(opp.Id, opp);
        }
    }
    
    List<OpportunityLineItem> oliList = [select Id, Name, Product2Id, Product2.Family from 
                                         OpportunityLineItem where OpportunityId IN:oppItems.keyset()];
        
        for(OpportunityLineItem oli :oliList)
        {
             

            if(oli.product2.family == 'software Recurring__c')
                 
                //Date dt = oppobj.closedate;
                    
                     for(Integer i =1;i<=12;i++)
            {
              ARRCalcualateOpportunity__c arrOpp = new ARRCalcualateOpportunity__c();
              arropp.name = oppItems.get(oli.OpportunityId).Name;
              arropp.ownerId = oppItems.get(oli.OpportunityId).OwnerId; 
              arropp.OpportunityArr__c=oppItems.get(oli.OpportunityId).Id;  
                System.debug('arropp list'+arropp);
              Arropplist.add(arropp);
               System.debug('arropp list'+arropplist);
            }
        }
        
        if(Arropplist.size() >0){
          insert Arropplist;
        }
    

 
Best Answer chosen by Dolgoldy
Rohit Kumar SainiRohit Kumar Saini
Hello,

Even though there are many areas of improvement on this piece of code, functionally it should work. A couple of thoughts below - 
  • It only works on Closed Won opportunities
  • For eligible opportunities, it only works for OpportunityLineItems with product2.family == 'software Recurring__c'
  • Let's add opening brace({) after 
    if(oli.product2.family == 'software Recurring__c')
    and wrap whatever code you want inside this if clause before you add a closing brace (}). That will save you from different issues down the road
  • I am not sure about the significance of for loop with static count 12. Do we want to make 12 ARRCalcualateOpportunity__c  records for each eligible OpportunityLineItem ( because of 12 months)?
  • What is not working in this code? Are you getting any errors running this? Is it not creating the records and not giving any errors? How are you invoking this code ( using apex trigger/batch etc ?). Can you please provide more information regarding this?
Thanks

All Answers

Rohit Kumar SainiRohit Kumar Saini
Hello,

Even though there are many areas of improvement on this piece of code, functionally it should work. A couple of thoughts below - 
  • It only works on Closed Won opportunities
  • For eligible opportunities, it only works for OpportunityLineItems with product2.family == 'software Recurring__c'
  • Let's add opening brace({) after 
    if(oli.product2.family == 'software Recurring__c')
    and wrap whatever code you want inside this if clause before you add a closing brace (}). That will save you from different issues down the road
  • I am not sure about the significance of for loop with static count 12. Do we want to make 12 ARRCalcualateOpportunity__c  records for each eligible OpportunityLineItem ( because of 12 months)?
  • What is not working in this code? Are you getting any errors running this? Is it not creating the records and not giving any errors? How are you invoking this code ( using apex trigger/batch etc ?). Can you please provide more information regarding this?
Thanks
This was selected as the best answer
DolgoldyDolgoldy
HI Rohit, Thank you for your valuable answer. Now its working as per requirement. yes There are many improvement in this code since i'm new to development. so can you send updated code. yes you are right I want to make 12 ARRCalcualateOpportunity__c records for each eligible OpportunityLineItem ( because of 12 months)? Yes the issue was Its not gave any error but was not creating record. but now its working. I am invoking this code by writing script in anonymous window.
DolgoldyDolgoldy
HI Again its not working. It works only one time now again here record not inserted. not giving any error but not giving output as weell plz help