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
SAPOCSAPOC 

How to add Product information to a field on Opportunity

I have Type field on OpportunityLineitem which is different for different products.As users add products ,the list of types should be added to a field on Opportunity.

Eg: product 1 has type 1,Product 2 has Type 2 Product 3 has Type 3 if these products are added ,then field on Opportunity must be type1,type2,type 3.

Best Answer chosen by Admin (Salesforce Developers) 
Naidu PothiniNaidu Pothini
Trigger OppLineItemTrigger on OpportunityLineitem(after insert, after update)
{
	if(trigger.isInsert)
	{
		List<Id> OppIds = new List<Id>();

		List<Opportunity> Oppts = new List<Opportunity>();

		for(OpportunityLineitem Opl : Trigger.new)
		{
			OppIds.add(opl.OpportunityId);
		}

		Map<Id, Opportunity> OppMap = new Map<Id, Opportunity>([SELECT Id, Name, TypesofProducts FROM Opportunity WHERE Id IN :OppIds]);

		for(OpportunityLineitem Opl : [SELECT Id, Type, OpportunityId FROM OpportunityLineitem WHERE OpportunityId IN :OppIds])
		{
			Opportunity Oppty = OppMap.get(Opl.OpportunityId);

			if(opl.Type <> null && !Oppty.TypesofProducts.Contains(opl.Type))
			{
				String str = Oppty.TypesofProducts +', '+Opl.Type;

				Oppty.TypesofProducts = str;
			}

			OppMap.put(Oppty.Id, Oppty);
		}

		Oppts = OppMap.values();

		update Oppts;
	}

	if(Trigger.isUpdate)
	{
		List<OpportunityLineitem> OplList = new List<OpportunityLineitem>();

		List<Opportunity> Oppts = new List<Opportunity>();

		for(Integer i = 0; i < Trigger.new.size(); i++)
		{
			if(Trigger.new[i].Type <> Trigger.old[i].Type)
			{
				Opllist.add(Trigger.new[i]);
			}
		}

		if(Opllist.size() > 0)
		{
			List<Id> OppIds = new List<Id>();

			for(OpportunityLineitem Opx : Opllist)
			{
				OppIds.add(Opx.OpportunityId);
			}

			Map<Id, Opportunity> OppMap = new Map<Id, Opportunity>([SELECT Id, Name, TypesofProducts FROM Opportunity WHERE Id IN :OppIds]);

			for(OpportunityLineitem Opl : [SELECT Id, Type, OpportunityId FROM OpportunityLineitem WHERE OpportunityId IN :OppIds])
			{
				Opportunity Oppty = OppMap.get(Opl.OpportunityId);

				if(!Oppty.TypesofProducts.Contains(opl.Type))
				{
					String str = Oppty.TypesofProducts +', '+Opl.Type;

					Oppty.TypesofProducts = str;
				}

				OppMap.put(Oppty.Id, Oppty);
			}

			Oppts = OppMap.values();

			update Oppts;
		}
	}	
}

 try this.

All Answers

Cloud CredenceCloud Credence

Hi,

 

I think you have to write a trigger on Opportunity Line Item(After Insert, After Update), that takes up the Product details and update the field on opportunity(after framing the unique list).

 

thanks

sivaextsivaext

Hi 

 

The type on opportunity is picklist field. 

suppose first opportunity with values type1, type2 etc

second opportunity with values type3, type4 etc.

 

  by using trigger, you can't restrict  picklist field based record level.

 

you need to overwrite page with visualforce page to achieve above functionality.

SAPOCSAPOC

The field on Opportunity is text field. Also i have the field on OpportunityLineItem- not required to get it from product.Can anyone please provide code for trigger.

Naidu PothiniNaidu Pothini
Trigger OppLineItemTrigger on OpportunityLineitem(after insert, after update)
{
	if(trigger.isInsert)
	{
		List<Id> OppIds = new List<Id>();

		List<Opportunity> Oppts = new List<Opportunity>();

		for(OpportunityLineitem Opl : Trigger.new)
		{
			OppIds.add(opl.OpportunityId);
		}

		Map<Id, Opportunity> OppMap = new Map<Id, Opportunity>([SELECT Id, Name, TypesofProducts FROM Opportunity WHERE Id IN :OppIds]);

		for(OpportunityLineitem Opl : [SELECT Id, Type, OpportunityId FROM OpportunityLineitem WHERE OpportunityId IN :OppIds])
		{
			Opportunity Oppty = OppMap.get(Opl.OpportunityId);

			if(opl.Type <> null && !Oppty.TypesofProducts.Contains(opl.Type))
			{
				String str = Oppty.TypesofProducts +', '+Opl.Type;

				Oppty.TypesofProducts = str;
			}

			OppMap.put(Oppty.Id, Oppty);
		}

		Oppts = OppMap.values();

		update Oppts;
	}

	if(Trigger.isUpdate)
	{
		List<OpportunityLineitem> OplList = new List<OpportunityLineitem>();

		List<Opportunity> Oppts = new List<Opportunity>();

		for(Integer i = 0; i < Trigger.new.size(); i++)
		{
			if(Trigger.new[i].Type <> Trigger.old[i].Type)
			{
				Opllist.add(Trigger.new[i]);
			}
		}

		if(Opllist.size() > 0)
		{
			List<Id> OppIds = new List<Id>();

			for(OpportunityLineitem Opx : Opllist)
			{
				OppIds.add(Opx.OpportunityId);
			}

			Map<Id, Opportunity> OppMap = new Map<Id, Opportunity>([SELECT Id, Name, TypesofProducts FROM Opportunity WHERE Id IN :OppIds]);

			for(OpportunityLineitem Opl : [SELECT Id, Type, OpportunityId FROM OpportunityLineitem WHERE OpportunityId IN :OppIds])
			{
				Opportunity Oppty = OppMap.get(Opl.OpportunityId);

				if(!Oppty.TypesofProducts.Contains(opl.Type))
				{
					String str = Oppty.TypesofProducts +', '+Opl.Type;

					Oppty.TypesofProducts = str;
				}

				OppMap.put(Oppty.Id, Oppty);
			}

			Oppts = OppMap.values();

			update Oppts;
		}
	}	
}

 try this.

This was selected as the best answer
SAPOCSAPOC

Thank You.It worked.