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
OnCloud9OnCloud9 

Auto populate account name depending on opportunity lookup.

Hi!  My first post here so please take it easy :)

 

I have a custom object that has two lookups: Related Account & Related Opportunity.  Having to look up both of these, 1 by 1, is a hassle for some of our end users.

 

Therefore, is there a way (trigger?) to do the following:

 

Lookup the opportunity name and have the related account name populate under Related Account lookup field?  (before or after the save is fine)

 

Thanks in advance.

 

 

 

 

bob_buzzardbob_buzzard

Welcome to the boards.

 

This can be done in a trigger.  Its not a trivial one as the opportunity fields aren't available to the trigger, so you'll have to query those out and it requires use of a collection or two to bulkify.  It also needs to handle updates, but only if the opportunity has been changed.  

 

I think the following should be close - but I haven't compiled it.  I'm assuming you want to overwrite the account with that from the opportunity in all cases, so it will need tweaking if that is not the case.  I've made it a before insert/update trigger, as that means that the objects in trigger.new can changed without requiring a separate update call.

 

 

 

trigger myTrigger on MyObj__c (before insert, before update) 
{
	List<Id, List<MyObj__c>> objsByOppId=new List<Id, List<MyObj__c>>();
	for (MyObj__c obj : trigger.new)
	{
		if ( (Trigger.isInsert)) ||
		     (Trigger.oldMap.get(obj.id).Opportunity__c!=obj.Opportunity__c) )
		{
			List<MyObj__c> objList=objsByOppId.get(obj.Opportunity__c);
			if (null==objList)
			{
				objList=new List<MyObj__c>();
				objsByOppId.put(obj.Opportunity__c, objList);
			}
			objList.add(obj);
		}
		
		List<Opportunity> opps=[select id, Account from Opportunity where id IN :objsByOppId.keySet()];
		for (Opportunity opp : opps)
		{
			List<MyObj__c> objList=objsByOppId.get(opp.id);
			for (MyObj__c obj : objList)
			{
				obj.Account__c=opp.Account;
			}
		}
	}
}

 

 

OnCloud9OnCloud9

Thanks Bob!  I tweaked the code as necessary and added my custom object & related account/opportunity field names.

 

However, I get an error around " || " (line 6)  here:  if ( (Trigger.isInsert) } ||

 

I removed the "||" but still got the same error (Save error: list mus thave exactly 1 type argument).  Do you know what this means?

 

 

 

trigger populateAccountFromOpportunity on Site_Location__c (before insert, before update) 
{
	List<Id, List<Site_Location__c>> objsByOppId=new List<Id, List<Site_Location__c>>();
	for (Site_Location__c obj : trigger.new)
	{
		if ( (Trigger.isInsert) } ||
		     (Trigger.oldMap.get(Site_Location__c.id).Related_Opportunity__c!=Site_Location__c.Related_Opportunity__c) )
		{
			List<Site_Location__c> objList=objsByOppId.get(Site_Location__c.Related_Opportunity__c);
			if (null==objList)
			{
				objList=new List<Site_Location__c>();
				objsByOppId.put(Site_Location__c.Related_Opportunity__c, objList);
			}
			objList.add(Site_Location__c);
		}
		
		List<Opportunity> opps=[select id, Account from Opportunity where id IN :objsByOppId.keySet()];
		for (Opportunity opp : opps)
		{
			List<Site_Location__c> objList=objsByOppId.get(opp.id);
			for (Site_Location__c obj : objList)
			{
				Site_Location__c.Related_Account__c=opp.Account;
			}
		}
	}
}

 

Thanks again!

 

bob_buzzardbob_buzzard

A round bracket seems to have morphed into a brace:

 

Original:

 

 

if ( (Trigger.isInsert)) ||

 

 

Latest:

 

 

if ( (Trigger.isInsert) } ||

 

Change this back to a closing round bracket and that should sort it.

 

OnCloud9OnCloud9

Thanks...I've been stuck on this for a while.  I changed it back to the parenthesis, however I get errors: (line 1:"Multiple markers at this line; list must have exactly 1 argument & line 6: unexpected token "||").  

 

Not sure what to do...I've tried rearranging some brackets, parthensis, etc. but don't understand apex enough to pinpoint the problem.  

 

I know "||" usually stands for OR so what I did was remove the ending parentehsis on line 6.  That way it says line 6 or line 7, etc.

 

Let me know if I'm missing something...confused..Thanks! 

 

 

if ((Trigger.isInsert ) ||
		     (Trigger.oldMap.get(Site_Location__c.id).Related_Opportunity__c!=Site_Location__c.Related_Opportunity__c) )
		{
			List<Site_Location__c> objList=objsByOppId.get(Site_Location__c.Related_Opportunity__c);
			if (null==objList)
			{
				objList=new List<Site_Location__c>();
				objsByOppId.put(Site_Location__c.Related_Opportunity__c, objList);
			}
			objList.add(Site_Location__c);
		}