+ Start a Discussion
Sonam PatilSonam Patil 

Trigger to insert in one object and get updated in other object

Hi,
I am new to SFDC development.

Can anyone explain to me how to write a trigger, if we inserting data in one object and should get updated in another object.
Best Answer chosen by Sonam Patil
Amit Chaudhary 8Amit Chaudhary 8
Set is used to get unique account. in List there can be duplicate account id
Map is key value pare with account id we can get account record
trigger ContactTrigger on Contact (before insert, before update) 
{
	Set<Id> accIds = new Set<Id>(); // Created a set to store all unique account if
	for(Contact con : trigger.new) 
	{
		accIds.add(con.AccountId);
	}
	
	Map<Id,Account> accountMap = new Map<Id,Account>( [select Phone from Account where id in: accIds] ); // get all account record which are associated with contact
	for(Contact con : trigger.new) 
	{
		con.phone = accountMap.get(con.accountId).Phone; // get account record and set account phone on contact phone
	}
}

let us know if this will help you

All Answers

Salesforce seekarSalesforce seekar
hi sonam ,  please find this example : 

https://developer.salesforce.com/forums/ForumsMain?id=906F0000000kBYfIAM
which is helpful 
JyothsnaJyothsna (Salesforce Developers) 
Hi Sonam,

Please check the below sample code.For example, I want to create a record in Account object automatically created a record in contact object.
Trigger:
 
trigger CreateAccountContact on Account (after insert, after update){

if(Trigger.isInsert){

    List<Contact> ct = new List <Contact>();

    for(Account acc : trigger.new){

        Contact c = new Contact(LastName = acc.name,
                    AccountId=acc.id,
                    Fax=acc.Fax,
                    MailingStreet=acc.BillingStreet,
                    MailingCity=acc.BillingCity,
                    /* similarly add all fields which you want */
                    MailingState=acc.BillingState,
                    MailingPostalCode=acc.BillingPostalCode,
                    MailingCountry=acc.BillingCountry,
                    Phone=acc.Phone);

        ct.add(c);
    }
    insert ct; 
}
}

Hope this helps you!
Regards,
Jyothsna
Amit Chaudhary 8Amit Chaudhary 8
Hi Sonam,

I will suggest you please check trailhead to learn about trigger. I hope that will help you
1) https://developer.salesforce.com/trailhead/module/apex_triggers

Sample code
trigger DmlTriggerNotBulk on Account(after update) {   
    // Get the related opportunities for the accounts in this trigger.        
    List<Opportunity> relatedOpps = [SELECT Id,Name,Probability FROM Opportunity
        WHERE AccountId IN :Trigger.New];          

    // Iterate over the related opportunities
    for(Opportunity opp : relatedOpps) {      
        // Update the description when probability is greater 
        // than 50% but less than 100% 
        if ((opp.Probability >= 50) && (opp.Probability < 100)) {
            opp.Description = 'New description for opportunity.';
            // Update once for each opportunity -- not efficient!
            update opp;
        }
    }    
}
Let us know if this will help you

Thanks
Amit Chaudhary
 
Sonam PatilSonam Patil
Hi,
Thank you for the quick response.
Can you explain me the below code in detail.
And why set, why not List.
And why Map


trigger ContactTrigger on Contact (before insert, before update) {
Set<Id> accIds = new Set<Id>(); for(Contact con : trigger.new) {
 accIds.add(con.AccountId);
}
 Map<Id,Account> accountMap = new Map<Id,Account>( [select Phone from Account where id in: accIds] );
for(Contact con : trigger.new) {
con.phone = accountMap.get(con.accountId).Phone;
 }
 }
Amit Chaudhary 8Amit Chaudhary 8
Set is used to get unique account. in List there can be duplicate account id
Map is key value pare with account id we can get account record
trigger ContactTrigger on Contact (before insert, before update) 
{
	Set<Id> accIds = new Set<Id>(); // Created a set to store all unique account if
	for(Contact con : trigger.new) 
	{
		accIds.add(con.AccountId);
	}
	
	Map<Id,Account> accountMap = new Map<Id,Account>( [select Phone from Account where id in: accIds] ); // get all account record which are associated with contact
	for(Contact con : trigger.new) 
	{
		con.phone = accountMap.get(con.accountId).Phone; // get account record and set account phone on contact phone
	}
}

let us know if this will help you
This was selected as the best answer
Sonam PatilSonam Patil
Tq Amit, It was helpfull