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
Supriya P 24Supriya P 24 

Can i create related list of opportunity object to Custom object?

Hi All,

Is it possible to create related list records to an custome object(look up relation) of a Opportunity object when ever the status field in opportunity updated and matches the criteria?

when ever opportunity stages= Booked then it should create a new record (custom object) and update the related opportunity to it?

Please help me if anyone have idea on such scenerio.

Thanks,
Supriya
Dushyant srivastava 8Dushyant srivastava 8
Hi Supriya,

I hope I understand this clearly, You have an Opportunity and a Custom Object(Let's Name it X) and X is the parent of Opportunity. 

We can write a trigger on After Insert & Check if stage = Booked, store Id's of all the opportunity which come in the criteria and create the X object records storing the Opportunity Id and X object in map. Once DML is made the list of X will have Id's which can be used to map both records.
 
trigger OpportunityTrigger on Opportunity (after insert)
{
	OppTriggerHandler.onAfterInsert(List<Opportunity> lstOppotunity);
}

public class OppTriggerHandler {
	public static void onAfterInsert(List<Opportunity> lstOppotunity)
    {
        linkOppWithX(lstOppotunity);
    }
    
	public static void linkOppWithX(List<Opportunity> lstOppotunity)
	{
		List<Opportunity> lstBoookedOpp = new List<Opportunity>();
		for(Opportunity objOpportunity : lstOppotunity)
		{
			if(objOpportunity.Stage == 'Booked')
				lstBoookedOpp.add(objOpportunity);
		}
		
		map<Opportunity , X__c> mapOppX = new map<Opportunity , X__c>();
		
		for(Opportunity objOpportunity : lstBoookedOpp)
		{
			X__c objX = new X__c();
			objX.Name = objOpportunity.Name;
			.
			.
			.
			.
			.
			.
			
			mapOppX.put(objOpportunity , objX);
		}
		
		if(mapOppX.Values().size() > 0)
			Insert mapOppX.Values();
		
		List<Opportunity> lstUpdateOpp = new List<Opportunity>();
		
		for(Opportunity objOpportunity : mapOppX.Values())
		{
			objOpportunity.X__c = mapOppX.get(objOpportunity).Id;
			lstUpdateOpp.add(objOpportunity);
		}
		
		if(lstUpdateOpp.size() > 0)
			Update lstUpdateOpp;
	}
}

You have to make few changes in this code but logic will work fine.

Thanks & Best Regards,
Dushyant Srivastava​​​​​​​
Supriya P 24Supriya P 24
Hi Dushyant,

Thank you for your response and really grateful fro quick response. 
Below is my scenerio and i wanted to know if this is possible to implement in salesforce. Its big ask kindly let me know if this can be implemneted.

When the opportunity stage= Booked then it should check in custom object(OpportunityStageStat__c ) if there is any record existing with current date and opportunity owner as sales user.
If not then create a record in custom object(OpportunityStageStat__c ) and update that opportunity in related list.Mean time there is custom field "demo Count" should increament the count to +1.


Thanks,
Supriya