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 

script not running plz help

Hi,
Here is my script written in anonymous window to create record against opportuniyt for 3 month jan,feb,march but its not working please help me to solve




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,opportunity.amount,opportunity.closedate from 
                                         OpportunityLineItem where OpportunityId IN:oppItems.keyset()];
        
        system.debug('oli'+ oliList);
        for(OpportunityLineItem oli :oliList)
        {
             for(Integer i =1;i<=12;i++)
                     {
                         if(oli.product2.family == 'software Recurring')
            {
              ARRCalcualateOpportunity__c arrOpp = new ARRCalcualateOpportunity__c();
             
                 arropp.name=string.valueOf(oli.opportunity.closedate);
                arropp.OpportunityArr__c= oli.OpportunityId;      
                arrOpp.Opportunity_Product__c= oli.product2Id;
                system.debug('arropp'+arropp);
              Arropplist.add(arropp);
            }
        }
        }
        
        if(Arropplist.size() >0){
          insert Arropplist;
            System.debug('arropp list'+arropplist);
        }
    
             


            
                 

                    
                    
Best Answer chosen by Dolgoldy
AnkaiahAnkaiah (Salesforce Developers) 
Hi Dolly,

try with below code.
 
Map <Id, Opportunity> oppItems = new Map <Id, Opportunity>();
        
    list<ARRCalcualateOpportunity__c> Arropplist= new list<ARRCalcualateOpportunity__c>();
        list<Opportunity> opplist= [select id, name from 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,opportunity.amount,opportunity.closedate from 
                                         OpportunityLineItem where OpportunityId IN:oppItems.keyset()];
        
        system.debug('oli'+ oliList);
        for(OpportunityLineItem oli :oliList)
        {
             for(Integer i =1;i<=12;i++)
                     {
                         if(oli.product2.family == 'software Recurring')
            {
              ARRCalcualateOpportunity__c arrOpp = new ARRCalcualateOpportunity__c();
             
                 arropp.name=string.valueOf(oli.opportunity.closedate);
                arropp.OpportunityArr__c= oli.OpportunityId;      
                arrOpp.Opportunity_Product__c= oli.product2Id;
                system.debug('arropp'+arropp);
              Arropplist.add(arropp);
            }
        }
        }
        
        if(Arropplist.size() >0){
          insert Arropplist;
            System.debug('arropp list'+arropplist);
        }

If this helps, Please mark it as best answer.

Thanks!!

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi Dolly,

try with below code.
 
Map <Id, Opportunity> oppItems = new Map <Id, Opportunity>();
        
    list<ARRCalcualateOpportunity__c> Arropplist= new list<ARRCalcualateOpportunity__c>();
        list<Opportunity> opplist= [select id, name from 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,opportunity.amount,opportunity.closedate from 
                                         OpportunityLineItem where OpportunityId IN:oppItems.keyset()];
        
        system.debug('oli'+ oliList);
        for(OpportunityLineItem oli :oliList)
        {
             for(Integer i =1;i<=12;i++)
                     {
                         if(oli.product2.family == 'software Recurring')
            {
              ARRCalcualateOpportunity__c arrOpp = new ARRCalcualateOpportunity__c();
             
                 arropp.name=string.valueOf(oli.opportunity.closedate);
                arropp.OpportunityArr__c= oli.OpportunityId;      
                arrOpp.Opportunity_Product__c= oli.product2Id;
                system.debug('arropp'+arropp);
              Arropplist.add(arropp);
            }
        }
        }
        
        if(Arropplist.size() >0){
          insert Arropplist;
            System.debug('arropp list'+arropplist);
        }

If this helps, Please mark it as best answer.

Thanks!!
This was selected as the best answer
Geoffrey HillsGeoffrey Hills
While Cecil's concern over some weaknesses in the answer are fair, I wanted to point out a few things. 1) His two links appear to open the same page for me. 2) If you want scripts to simply run, setting the execution policy is probably still the way to go, and possibly unrelated to OP: 3) it seems the best policy is for startup scripts, specify the execution policy and scope in the command line running the script, and for logon set the session configuration as needed. If you want high security during the Windows session but reduced for logon, you'd want to get nuanced in your script order.
https://www.mylabcorp.me/
DolgoldyDolgoldy
Hi ankakiah
Your code is working but giving error in this line so plz help me to solve this out.
 
ravi soniravi soni
hi Dolly,
try below code. I don't know why you are using integer for loop for 12 digits inside loop. In my code I commented it. if it is necessary you can uncommented it.
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);
	}
}

if(oppItems.size() > 0){
	 List<OpportunityLineItem> oliList = [select Id, Name, Product2Id, Product2.Family,opportunity.amount,opportunity.closedate,OpportunityId from 
									 OpportunityLineItem where OpportunityId IN:oppItems.keyset()];
									 system.debug('oli'+ oliList);
									  for(OpportunityLineItem oli :oliList)
	{
		 //for(Integer i =1;i<=12;i++)	 {
					 if(oli.product2 != null && oli.product2.family == 'software Recurring')
		{
		  ARRCalcualateOpportunity__c arrOpp = new ARRCalcualateOpportunity__c();
		 
			 arropp.name=string.valueOf(oli.opportunity.closedate);
			arropp.OpportunityArr__c= oli.OpportunityId;      
			arrOpp.Opportunity_Product__c= oli.product2Id;
			system.debug('arropp'+arropp);
		  Arropplist.add(arropp);
		}
	//}
	}
	
	
}


	
	
   
	if(Arropplist.size() >0){
	  insert Arropplist;
		System.debug('arropp list'+arropplist);
	}

if it is helpful to you, let me know by marking it as best answer.
Thank you
DolgoldyDolgoldy
Hi actually i want to insert 12 record against opportunity thats why used loop there.
DolgoldyDolgoldy
Thank you for your response. It help me lot.