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
NikiG22NikiG22 

Insert Records on a Custom Object

Hello - I am trying to add to my current apex trigger that automaticlly adds a line item to a custom object based on the product family.

 

I need to add in here that if the product quantity = 10 then we add 10 line items insted of one...

 

Any help would be appriciated

 

Trigger:

trigger trg_new_ResumeSubscription on Opportunity (after update)
{
   List<Opportunity> closedWonOpps=new List<Opportunity>();

   for (Opportunity opp : trigger.new)
   {
       Opportunity oldOpp=trigger.oldMap.get(opp.id);
       if ( (opp.StageName=='Closed Won') &&
            (oldOpp.StageName!='Closed Won') )
       {
          closedWonOpps.add(opp);
       }
   }
    
   if (!closedWonOpps.isEmpty())
   {
List<OpportunityLineItem> olis = [SELECT ID, OpportunityId, Subscription_Term1__c, UnitPrice, 
        Product_Name__c,Closed_Won_Date__c, Daily_View_Limit1__c, Views_Purchased1__c FROM OpportunityLineItem WHERE Product_Family__c = 'Resume Search' and
              OpportunityId in :closedWonOpps];

      List<Resume_Subscriptions__c> rsToInsert=new List<Resume_Subscriptions__c>();
      for (OpportunityLineItem oli : olis)
      {
 Resume_Subscriptions__c newRS= new Resume_Subscriptions__c();
         newRS.Views_Purchased3__c = oli.Views_Purchased1__c;
         newRS.Subscription_Term3__c = oli.Subscription_Term1__c;
         newRS.Price__c = oli.UnitPrice;
         newRS.Product_Name__c = oli.Product_Name__c ;
         newRS.Opportunity__c = oli.Opportunityid;
         newRS.Purchase_Date2__c = oli.Closed_Won_Date__c;
         newRS.Daily_View_Limit3__c = oli.Daily_View_Limit1__c;
              rsToInsert.add(newRS);
      }

      insert rsToInsert;
   }
}

 

Best Answer chosen by Admin (Salesforce Developers) 
Jake GmerekJake Gmerek

I am assuming that you want to add a number of Resume_Subscriptions__c equal to the quantity field on the OpportunityLineItem object.  Here is the code for that:

 

trigger trg_new_ResumeSubscription on Opportunity (after update)
{
   List<Opportunity> closedWonOpps=new List<Opportunity>();

   for (Opportunity opp : trigger.new)
   {
       Opportunity oldOpp=trigger.oldMap.get(opp.id);
       if ( (opp.StageName=='Closed Won') &&
            (oldOpp.StageName!='Closed Won') )
       {
          closedWonOpps.add(opp);
       }
   }
    
   if (!closedWonOpps.isEmpty())
   {
	List<OpportunityLineItem> olis = [SELECT ID, OpportunityId, Subscription_Term1__c, UnitPrice, 
        Product_Name__c,Closed_Won_Date__c, Daily_View_Limit1__c, Views_Purchased1__c, Quantity FROM OpportunityLineItem WHERE Product_Family__c = 'Resume Search' and
        OpportunityId in :closedWonOpps];

	List<Resume_Subscriptions__c> rsToInsert=new List<Resume_Subscriptions__c>();
   for (OpportunityLineItem oli : olis)
   {
	For (integer i = 0; i < oli.Quantity; i++){
   		Resume_Subscriptions__c newRS= new Resume_Subscriptions__c();
        	newRS.Views_Purchased3__c = oli.Views_Purchased1__c;
        	newRS.Subscription_Term3__c = oli.Subscription_Term1__c;
        	newRS.Price__c = oli.UnitPrice;
        	newRS.Product_Name__c = oli.Product_Name__c ;
        	newRS.Opportunity__c = oli.Opportunityid;
        	newRS.Purchase_Date2__c = oli.Closed_Won_Date__c;
        	newRS.Daily_View_Limit3__c = oli.Daily_View_Limit1__c;
        	rsToInsert.add(newRS);
        }
    }

    insert rsToInsert;
   }
}

 If I have not made the correct assumptions, let me know what you really want and I will try to help.

 

Hope this helps.

All Answers

Jake GmerekJake Gmerek

I am assuming that you want to add a number of Resume_Subscriptions__c equal to the quantity field on the OpportunityLineItem object.  Here is the code for that:

 

trigger trg_new_ResumeSubscription on Opportunity (after update)
{
   List<Opportunity> closedWonOpps=new List<Opportunity>();

   for (Opportunity opp : trigger.new)
   {
       Opportunity oldOpp=trigger.oldMap.get(opp.id);
       if ( (opp.StageName=='Closed Won') &&
            (oldOpp.StageName!='Closed Won') )
       {
          closedWonOpps.add(opp);
       }
   }
    
   if (!closedWonOpps.isEmpty())
   {
	List<OpportunityLineItem> olis = [SELECT ID, OpportunityId, Subscription_Term1__c, UnitPrice, 
        Product_Name__c,Closed_Won_Date__c, Daily_View_Limit1__c, Views_Purchased1__c, Quantity FROM OpportunityLineItem WHERE Product_Family__c = 'Resume Search' and
        OpportunityId in :closedWonOpps];

	List<Resume_Subscriptions__c> rsToInsert=new List<Resume_Subscriptions__c>();
   for (OpportunityLineItem oli : olis)
   {
	For (integer i = 0; i < oli.Quantity; i++){
   		Resume_Subscriptions__c newRS= new Resume_Subscriptions__c();
        	newRS.Views_Purchased3__c = oli.Views_Purchased1__c;
        	newRS.Subscription_Term3__c = oli.Subscription_Term1__c;
        	newRS.Price__c = oli.UnitPrice;
        	newRS.Product_Name__c = oli.Product_Name__c ;
        	newRS.Opportunity__c = oli.Opportunityid;
        	newRS.Purchase_Date2__c = oli.Closed_Won_Date__c;
        	newRS.Daily_View_Limit3__c = oli.Daily_View_Limit1__c;
        	rsToInsert.add(newRS);
        }
    }

    insert rsToInsert;
   }
}

 If I have not made the correct assumptions, let me know what you really want and I will try to help.

 

Hope this helps.

This was selected as the best answer
NikiG22NikiG22

That worked!!

 

Thank you sooo much for your help!

Jake GmerekJake Gmerek

No problem!

ajnixxajnixx

Hi,

 

I am trying to insert the opportunity line items to a custom object and i succeeded, but i had a new requirement. I need to be able to filter only line items based from a vendor i will select. I need help for a visualforce code that can do filtering.Does anyone had the same requirement?

 

Any help would be appriciated.

 

Thanks

Jake GmerekJake Gmerek

I used this blog post the first time that I had this requirement and it helped me alot.  Check it out:

 

http://blog.jeffdouglas.com/2010/07/13/building-a-dynamic-search-page-in-visualforce/