+ Start a Discussion
Benjy HuntBenjy Hunt 

Trigger to create multiple Custom object records based on the opportunity quantity

Hi All,

I have been asked to create a trigger that will create records on a custom object when an opportinity is closed won. i can do this without issue, but i am running into a problem when the quantity is greater than 1.

What i need is for it to create 2 records if the opportunity quantity is 2 but i have no idea how to do this.

any help would be greatly appreciated
Naresh YadavNaresh Yadav
Hi Benjy

Please take a look on the below code. It is just a dummy code, you just need to replace the customObject with your real object name.
 
trigger testTrigger on Opportunity (before update) {
    List<CustomObject> customObjToInsert = new List<CustomObject>();
    for(Opportunity opp : trigger.New){
        if(opp.IsClosed && opp.IsWon){
            for(Integer i=0; i<opp.Qunatity;i++){
                CustomObject customObj = new CustomObject();
                // set all fields here
                customObjToInsert.add(customObj);
            }
        }
    }
    
    if(customObjToInsert.size()>0){
        insert customObjToInsert;
    }
}

Hope this helps you out.

Mark it as best.

Thanks
Naresh. 
Shamsi 110Shamsi 110
I think Naresh code should work assuming your custom object records depends on the quantity field of opportunity like If it is set to three than three records will be inserted into custom object. 

If you always want 2 records than set exit condition  to i<2 in the second classic for loop.

 
Benjy HuntBenjy Hunt
Just having a look at trying to implement this now, and it looks like the quantity is stored against the opportunity line item rather than the opportunity itself.

any further idea's?
Naresh YadavNaresh Yadav
Hi Benjy

It is on the opportunity. The field API name is TotalOpportunityQuantity, repalce quantity with this.
Akshay_DhimanAkshay_Dhiman
Hi Benjy,
 * ‘Opportunity’ object don’t have any ‘Quantity’ field on it.
But OpportunityLineItems are those Products which are inside any Opportunity, and we have Quantity field for OpportunityLineItems,
* So, We can write code for Quantity of OpportunityLineItems(Products).
So, I have written code for ‘ Quantity’ of OpportunityLineItems as per your question.
* I have written code for ‘update event',
* Whenever you will update any Opportunity, Trigger will be fired
And as per the Quantity of ‘OpportunityLineItems’ within that ‘Opportunity',  Number of Records Will be Created on Your Custom Object.

* And Please Replace ‘My_Custom_Obj__c’ with your ‘custom object's name.
After this, you can use this code completely.
 If any doubt, you can ask me.
//Trigger Code

trigger TriggerCustomObj on Opportunity (before update) {
	if(trigger.isUpdate && trigger.isBefore){
    	Custom_Handler.mmm(Trigger.new);
	}
}


//Trigger Handler Class

public class Custom_Handler{
	public static void mmm(List<Opportunity> newOpplist){
    	Set<Id> oppset = new Set<Id>();
    	for(Opportunity opp: newOpplist){
        	if(opp.StageName=='Closed Won')
        	{
            	oppset.add(opp.Id);
        	}  
    	}
   	 
    	List<AggregateResult> arList = new List<AggregateResult>();
    	arList = [Select sum(Quantity) s, OpportunityId ids from OpportunityLineItem where OpportunityId In:oppset      group by OpportunityId];
    	System.debug('arList'+arList);
    	Map<Id,Decimal> oppIdVsQuantity = new Map<Id,Decimal>();
    	for(AggregateResult ar: arList){
        	oppIdVsQuantity.put((Id)ar.get('ids'), (Decimal)ar.get('s'));
    	}
    	List<My_Custom_Obj__c> mlist = new List <My_Custom_Obj__c>();
    	for(Id idKey : oppIdVsQuantity.keySet())
    	{
        	Decimal totalOli=oppIdVsQuantity.get(idKey);
        	for(Integer i=1; i<=totalOli; i++){
            	My_Custom_Obj__c mco = new My_Custom_Obj__c();
            	mco.Name='TestDemo '+i;
            	mlist.add(mco);
        	}
    	}
    	if(mlist.size()>0){
        	insert mlist;
    	}
	}
}

Hope this may help you.
Regards,
Akshay.