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
sean.gorman@ipc.comsean.gorman@ipc.com 

Trigger to create new Opp from Custom Object - need Opp ID in Custom Object

Hi,

I have a new custom object stores Opportunity related data which can be used to create a new opportunity.

 

I cannot think of a way to store the new OPPID in the relavent record.

 

trigger Process_IB2U on IB2U__c (Before Update) {

set<id> IBIds = new set<id>();
map<id,id> mapIBtoOpp = new map<id,id>();
list<IB2U__c> newIB2U = new list<IB2U__c>();

set<IB2U__c> IBs_toUpdate = new set<IB2U__c>();
list<Opportunity> Opp_forInsert = new list<Opportunity>();
Pricebook2[] priceBook = [Select p.Name, p.Id From Pricebook2 p where p.Name = 'Trading Price Book'];

	for(IB2U__c IB: trigger.new)
	{ 
		if(IB.Convert_to_Opportunity__c == TRUE)
		{
			IBs_toUpdate.add(IB);
			Opportunity oppty = new Opportunity (StageName = 'Price / Quote', Opp_Probability__c = '60', Name = 'IB2U Converted Opp',
									Estimated_Revenue_Amount__c = IB.Estimated_Revenue_Amount__c, CloseDate = IB.Estimated_Contract_Date__c,
									Amount = 200, AccountId = IB.Account__c, CurrencyIsoCode = IB.CurrencyIsoCode, Unigy__c = TRUE, 
									Site_Name__c = IB.Account_Site__c, Distributor__c = IB.Distributor__c, Pricebook2Id = priceBook[0].Id, Incumbent__c = IB.Incumbant__c );
			Opp_forInsert.add(oppty);

		}
	}
	insert Opp_forInsert;


	for(Opportunity Oppt: Opp_forInsert)
	{
		// this does not work as IB is out of context
		IB.Opportunity__c = Oppt.id;
		
	}
	// Again - will not work
	update IB;

}

 

 

I have a couple of SETs and MAPs that are not currently used... I was playing with them to get the ID of the IB and OPP together - but I cannot without requerying the Opportunity table with a related list of IB.IDs and saving the related IB on the Opportunity.

 

 

Best Answer chosen by Admin (Salesforce Developers) 
Sean TanSean Tan

You can do this by creating a map with the Id of the IB that is associated with the opportunity being inserted.

 

Try something like this:

 

trigger Process_IB2U on IB2U__c (Before Update) {
		
	Map<Id, Opportunity> oppToInsertMap = new Map<Id, Opportunity>{};
	
	Pricebook2[] priceBook = [Select p.Name, p.Id From Pricebook2 p where p.Name = 'Trading Price Book'];
	
	for(IB2U__c IB: trigger.new)
	{ 
		if(IB.Convert_to_Opportunity__c == TRUE)
		{						
			Opportunity oppty = new Opportunity (StageName = 'Price / Quote', Opp_Probability__c = '60', Name = 'IB2U Converted Opp',
													Estimated_Revenue_Amount__c = IB.Estimated_Revenue_Amount__c, CloseDate = IB.Estimated_Contract_Date__c,
													Amount = 200, AccountId = IB.Account__c, CurrencyIsoCode = IB.CurrencyIsoCode, Unigy__c = TRUE, 
													Site_Name__c = IB.Account_Site__c, Distributor__c = IB.Distributor__c, Pricebook2Id = priceBook[0].Id, Incumbent__c = IB.Incumbant__c );
			oppToInsertMap.put(IB.Id, oppty);			
		}
	}
	insert oppToInsertMap.values();
	
	for (Id key : oppToInsertMap.keySet())
	{
		Opportunity newOpportunity = oppToInsertMap.get(key);
		IB2U__c ib = Trigger.newMap.get(key);
		
		ib.Opportunity__c = newOpportunity.Id;
	}	
}

 

All Answers

Sean TanSean Tan

You can do this by creating a map with the Id of the IB that is associated with the opportunity being inserted.

 

Try something like this:

 

trigger Process_IB2U on IB2U__c (Before Update) {
		
	Map<Id, Opportunity> oppToInsertMap = new Map<Id, Opportunity>{};
	
	Pricebook2[] priceBook = [Select p.Name, p.Id From Pricebook2 p where p.Name = 'Trading Price Book'];
	
	for(IB2U__c IB: trigger.new)
	{ 
		if(IB.Convert_to_Opportunity__c == TRUE)
		{						
			Opportunity oppty = new Opportunity (StageName = 'Price / Quote', Opp_Probability__c = '60', Name = 'IB2U Converted Opp',
													Estimated_Revenue_Amount__c = IB.Estimated_Revenue_Amount__c, CloseDate = IB.Estimated_Contract_Date__c,
													Amount = 200, AccountId = IB.Account__c, CurrencyIsoCode = IB.CurrencyIsoCode, Unigy__c = TRUE, 
													Site_Name__c = IB.Account_Site__c, Distributor__c = IB.Distributor__c, Pricebook2Id = priceBook[0].Id, Incumbent__c = IB.Incumbant__c );
			oppToInsertMap.put(IB.Id, oppty);			
		}
	}
	insert oppToInsertMap.values();
	
	for (Id key : oppToInsertMap.keySet())
	{
		Opportunity newOpportunity = oppToInsertMap.get(key);
		IB2U__c ib = Trigger.newMap.get(key);
		
		ib.Opportunity__c = newOpportunity.Id;
	}	
}

 

This was selected as the best answer
sean.gorman@ipc.comsean.gorman@ipc.com
Great response... and I learn another new thing. This is fantastic