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
sdjagdja wdguqwesdjagdja wdguqwe 

I have created same fields for both contact object and opportunity object. I would like to automatically update the field values on opportunity object whenever I cretae new customer record.

I'm new to Apex and creating Triggers so hoping someone can help. Here's the scenario;
 
I have created same fields for both contact object and opportunity object. I would like to automatically update the field values on opportunity object whenever I cretae new customer record.
For this I have written the trigger with the event after insert. But I am getting error
Can anyone correct the below code plz….
trigger updateField on Contact (after insert) {
 Set<Id> Ids= new Set<Id>();
    for (Contact contact : Trigger.new)
    {
    Ids.add(customer.Id);  
    }
  List<Contact> contactList = new List<Contact>([Select Id,First_name__c, Last_name_c  FromContact where Id in :Ids]);   
       for(Contact temp : contactList )
       {
       Oppertunities oppor = new  Oppertunities();
       oppor.First_Name__c = temp. First_name__c,;
       oppor.Last_Name_c  = Last_name_c;
       insert oppor;
       }
    
Gaurav Gulanjkar 18Gaurav Gulanjkar 18
Hi, Try this code and understand each line of code.
trigger copyContactToOpportunity on Contact (after insert) {
    List<opportunity> oppList = new List<Opportunity>();
    for(Contact cont:trigger.new){
        opportunity opp = new Opportunity();
        opp.AccountId = cont.AccountId;
        opp.Description = cont.Description;
    	oppList.add(opp);
    }
    
    if(oppList.size()>0 && oppList!=null)
		insert(oppList);
}

 
sdjagdja wdguqwesdjagdja wdguqwe
Thank u so much for your code.I understood very clearly.It is working fine.

The  below trigger is to autoppulate the field values from contact to opportunity.
Suppose if i change any field value in Opportunity object that should update in Opportunity as well as in Contact also.
So for this i need to proceed with event after Update. Can you please guide me how to proceed further

trigger ConOpp on Contact(after insert) {
  List<Opportunity> newOpps = new List<Opportunity>();
  for (Contact con : Trigger.new) {
    Opportunity opp = new Opportunity();
    opp.Name        = con.LastName+' Opportunity';
    opp.StageName   = 'Prospecting';
    opp.Status__c = con.Status__c;
    opp.version__c = con.version__c;
    opp.CloseDate   = Date.today() + 90;
   // opp.AccountId   = acc.Id; // Use the trigger record's ID
    newOpps.add(opp);
  }
  if(newOpps.size()>0)
  insert newOpps;
}