+ Start a Discussion
SFDC@ErrorSFDC@Error 

How to create child record when Parent is create

HI All

I have 3 object Contact,Boutique and Opportunity.All are relationship with each other.I need to auto opportunity record creation when boutique will be created .I have created this trigger but its showing in boutique layout .but i need in contact layout.
trigger CreateOpportunity on Boutique__c(after insert) {
    List<Opportunity> Childs = new List<Opportunity>();

    for(Boutique__c a : trigger.new)
    {
       Opportunity Child = new Opportunity();
       Child.Boutique__c= a.Customer_Name__c;
       Child.Name= a.Name;
        Child.StageName= 'Generate Opportunity';
         Child.CloseDate= System.Today()+150;
      
     
       Childs.add(Child);      
    }

    Insert Childs;
}
User-added image
 
Balayesu ChilakalapudiBalayesu Chilakalapudi
Add below line
Child.ContactId=a.ContactId;
Trigger will be like this,
trigger CreateOpportunity on Boutique__c(after insert) {
    List<Opportunity> Childs = new List<Opportunity>();

    for(Boutique__c a : trigger.new)
    {
       Opportunity Child = new Opportunity();
       Child.Boutique__c= a.Customer_Name__c;
       Child.Name= a.Name;
        Child.StageName= 'Generate Opportunity';
         Child.CloseDate= System.Today()+150;
       Child.ContactId=a.ContactId;
     
       Childs.add(Child);      
    }

    Insert Childs;
}
Let us know if it helps you
SFDC@ErrorSFDC@Error
Chilakalapudi Its showing [image: Error] Error: Compile Error: Invalid field ContactId for SObject Opportunity at line 11 column 10
Balayesu ChilakalapudiBalayesu Chilakalapudi
Open Opportunity fields and find the api name of Lookup(Contact) and replace ContactId with the api name.
SFDC@ErrorSFDC@Error
Chilakalapudi Thanks for quick response I need Opportunity name should be this format contact name+opportunity(eg.ABC_Opportunity).how to set this format in the above trigger
Balayesu ChilakalapudiBalayesu Chilakalapudi
In your trigger, try like this,
Child.Name= a.Contact.Name;
SFDC@ErrorSFDC@Error
I have tried Child.Name= a.Contact.Name ; but it is not working [image: Error] Error: Compile Error: Invalid foreign key relationship: Boutique__c.Contact at line 8 column 20
SFDC Ravi KumarSFDC Ravi Kumar
Hi Pradhan,
                  Try this code.

trigger CreateOpportunity on Boutique__c (after insert) 
{
    List<Opportunity> Childs = new List<Opportunity>();

    for(Boutique__c a : trigger.new)
    {
       Opportunity Child = new Opportunity();
         Child.Boutique__c = a.id;
         Child.Name = a.Name;
         Child.StageName = 'Generate Opportunity';
         Child.CloseDate = System.Today()+150;
         Child.Contact__c = a.contact__c;
       
       Childs.add(Child);      
    }

    Insert Childs;
}
Karthi XaviKarthi Xavi
Hi Pradhan,

If you want to show the Oprrtunity under the contact record that opprtunity record should have a value in the contact field so you need to add a contact while you auto create Opportunity . kindly use the below updated snippet.

In the below code Contact__c is your contact field API in Opportunity Object.

trigger CreateOpportunity on Boutique__c(after insert) {
    List<Opportunity> Childs = new List<Opportunity>();

    for(Boutique__c a : trigger.new)
    {
       Opportunity Child = new Opportunity();
       Child.Boutique__c= a.Customer_Name__c;
       Child.Name= a.Contact__r.Name+'_Opportunity';
       Child.Contact__c = a.Contact__c;
       Child.StageName= 'Generate Opportunity';
       Child.CloseDate= System.Today()+150;
          
       Childs.add(Child);      
    }
    Insert Childs;
}