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 

child record not inserting

Hi all,
My requirement is when opportunity is closed won and product family is 'recurring' then 12 child record shouls be inserted according to closed date. here is my code which is not inserted record.
////code

trigger ArrOpptrigger3 on Opportunity (after insert,after update) {
     if(trigger.isInsert||trigger.isAfter){
     
//list<opportunityLineItem> olilist= new list<OpportunityLineItem>();
list<ARRCalcualateOpportunity__c> Arropplist= new list<ARRCalcualateOpportunity__c>();
   // List<PriceBookEntry> priceBookList = [SELECT Id, Product2Id, Product2.Id, Product2.Name,product2.family 
    //FROM PriceBookEntry WHERE Product2.Family ='software Recurring' LIMIT 1];

    //OpportunityLineItem oli= New OpportunityLineItem();

    for(opportunity op:Trigger.New){
        if(Op.StageName == 'closed Won')  
        {
    for(OpportunityLineItem Oli:[select Id,Name,Product2Id,Product2.Family from 
                                 OpportunityLineItem where OpportunityId=:op.Id])
          
            {
    if(oli.product2.family == 'software Recurring__c')
    {
        ARRCalcualateOpportunity__c arrOpp = new ARRCalcualateOpportunity__c();
      //  arropp.Id = op.ID;
       arropp.name = op.Name;
      // arropp.OppArrAccount__c = op.AccountId;
    arropp.ownerId = op.OwnerId; 
     arropp.OpportunityArr__c=op.Id;   
    Arropplist.add(arropp);
    }
        }
    //if(olilist!=null&&olilist.size()>0)
        
}
 }
         insert Arropplist;
}
}
mukesh guptamukesh gupta
Hi Pramila,

Please use below code:-
 
trigger ArrOpptrigger3 on Opportunity (after insert,after update) {
     if(trigger.isInsert||trigger.isAfter){
     
	Map<Id, Opportunity> oppItems = new Map<Id, Opportunity>();
	list<ARRCalcualateOpportunity__c> Arropplist= new list<ARRCalcualateOpportunity__c>();
    for(opportunity opp:Trigger.New){
        if(opp.StageName == 'closed Won')  
        {
		  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')
			{
			  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;   
			  Arropplist.add(arropp);
			}
		}
		
		if(Arropplist.size() > 0){
		  insert Arropplist;
		}
	}
}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 
DolgoldyDolgoldy
Hello Mukesh Thank you for your quick response. I used your code but still record is not inserting on child side.
McCuboMcCubo
Hi Pramila,
Make sure you are using the API Name for both the Stage and Family picklist fields, for instance, use Closed Won instead close won, sometimes the label and value are different, and I would suggest double check the same with the software Recurring__c picklist option.
Hope it helps.
DolgoldyDolgoldy
Hi Miguel Cubias, Thank you for your valuable suggestions, I,ve checked all the Labels & Api still it doesn't inserting record.
McCuboMcCubo
Are you getting any errors? Is the trigger active?
I would suggest adding a few log statements to really see what value each variable has. for instance, before the the if statement, you could add a
System.debug('Op StageName is: ' + op.StageName);

the same for the second conditional:
System.debug('Oli Family is: ' + oli.product2.family);

beside the SOQL query inside the for loop, your code looks fine. Let me know what you can get from the logs.
DolgoldyDolgoldy
Hi There was some problem in my org. now its working..Thank you