+ Start a Discussion
alok7049alok7049 

How to write trigger for join Object.

prob is :Add a trigger that when a join object is inserted, the trigger will copy the billing street address from the related Account to the other streets address on the related Contact.

 

can any one please help me out in doing this.

Best Answer chosen by Admin (Salesforce Developers) 
Chamil MadusankaChamil Madusanka

Try This with changing with your field names and object name

 

trigger copystreetaddress on Junction_Object__c (after update)
{
      
	if(trigger.isUpdate)
	{
		Set<Id> accountId = new Set<Id>();
		Set<Id> contactId = new Set<Id>();
		for(Junction_Object__c tempJ : Trigger.new)
		{
			accountId.add(tempJ.AccountID__c);
			contactId.add(tempJ.ContactID__c);
		}
		
		List<Account> AccountList =	[Select id,Name,BillingStreet from Account where id in: accountId];
		
		List<Contact> ContactList =	[Select id,Name,BillingStreet from Contact where id in: contactId];
		
		for(Junction_Object__c tempJ : Trigger.new)
		{
			for(Account tempAcc : AccountList)
			{
				if(tempJ.AccountID__c == tempAcc.Id)
				{
					for(Contact tempCon : ContactList)
					{
						if(tempJ.ContactID__c == tempCon.Id)
						{
							tempCon.BillingStreet = tempAcc.BillingStreet;
						}					
					
					}
				
				}
			
			}	
			
		}		
			update ContactList;
	}
       
}

 If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

All Answers

Ispita_NavatarIspita_Navatar

Hi Alok,

 

Please find attached the code for copying the related account billing address into other street address related fields in contact object as contact is getting created:-

 

trigger copystreetaddress on Contact (before insert ,before update)
{
      if(trigger.isbefore)
      {
                if(trigger.isinsert)
                 {
                        Contact con=trigger.new[0];
                         if(con.accountid!=null)
                         {
                                account a1=[select BillingStreet from account where id=:con.accountid];
                                   if(a1.BillingStreet!=null)
                                   {
                                                  con.OtherStreet =a1.BillingStreet;
                                    }
                         }
                }
        }
}

 

 Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

alok7049alok7049

there is a junction object between Account and Contact.And after inserting a joint object record the related account billing street should be copied to related contact other street.

 

do reply for this requirement.

Chamil MadusankaChamil Madusanka

Try This with changing with your field names and object name

 

trigger copystreetaddress on Junction_Object__c (after update)
{
      
	if(trigger.isUpdate)
	{
		Set<Id> accountId = new Set<Id>();
		Set<Id> contactId = new Set<Id>();
		for(Junction_Object__c tempJ : Trigger.new)
		{
			accountId.add(tempJ.AccountID__c);
			contactId.add(tempJ.ContactID__c);
		}
		
		List<Account> AccountList =	[Select id,Name,BillingStreet from Account where id in: accountId];
		
		List<Contact> ContactList =	[Select id,Name,BillingStreet from Contact where id in: contactId];
		
		for(Junction_Object__c tempJ : Trigger.new)
		{
			for(Account tempAcc : AccountList)
			{
				if(tempJ.AccountID__c == tempAcc.Id)
				{
					for(Contact tempCon : ContactList)
					{
						if(tempJ.ContactID__c == tempCon.Id)
						{
							tempCon.BillingStreet = tempAcc.BillingStreet;
						}					
					
					}
				
				}
			
			}	
			
		}		
			update ContactList;
	}
       
}

 If a reply to a post answers your question or resolves your problem, please mark it as the solution to the post so that others may benefit.

This was selected as the best answer