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
kevinjia1984kevinjia1984 

Create two records when one records created

Hi All,

 

I have three object Publication_Mention__c ,  Opportunity, Product. They all have lookup relationship between each other. The requirement is when a new Publication_Mention__c object is inserted into database with its tickbox field referall__c being ticked. A new Opportunity and the related Product record will be both created. I have tested the code I wrote. The Opportunity object inserting works fine(If I Comment out "insert newProduct;" in my code) . But when I try to insert a Product, error occurred.  Here is my code

 

Ps. 1.The "Product" renamed "Product2" in our data Schema

        2. I am using a trigger to call createNewOpportunity on Publicatiopn_Mention__c object

 

public with sharing class NewPublicationMention {
 public static void createNewOpportunity(List<Publication_Mention__c> PublicationMentions)
 {
 for(Publication_Mention__c PM: PublicationMentions)
 {
 //when the Referral__c checkbox ticked
 if(PM.Referral__c==True)
 {
 //a new Opportunity object will be created
 Opportunity newOpportunity = new Opportunity(Publication_Mention__c = PM.Id);
 
 //fields of the new Opportunity object
 newOpportunity.Name = newOpportunity.Contact_Name__r.FirstName + newOpportunity.Contact_Name__r.LastName + '-' + newOpportunity.Product__r.Name;
 newOpportunity.StageName = 'New';
 newOpportunity.CloseDate = Date.today();
 newOpportunity.Type = 'Free';
 newOpportunity.Order_Source__c = 'Helpline';
 newOpportunity.Order_Date__c = Date.today();
 newOpportunity.Amount = 0;
 
 insert newOpportunity;
 
 
 //a new Product object will be created
 Product2 newProduct = new Product2(Publication_Mention__c = PM.Id);
 
 //fields of the new Product object
 newProduct.Quantity_in_Stock__c = 1;
 newProduct.Price__c = 0.00;
 newProduct.First_Publication_Date__c = Date.today();
 
 insert newProduct;
 }
 }
}
}

 

 

Flowing is the error message: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Product Name]: [Product Name]: 

 

I'd appreciate it very  much for any help. Thanks

 

Best Answer chosen by Admin (Salesforce Developers) 
SwarnasankhaSwarnasankha

The error message states that the Product Name (which is a mandatory field) is not being passed while inserting the Product record. While you set the values for the different Product fields before inserting, you will need to set a value for the Name field also.

 

You will need to add the following line before inserting newProduct:

 

newProduct.Name = 'Test Product'; // You will need to decide on what value you would like to use as the Product Name 

All Answers

SwarnasankhaSwarnasankha

The error message states that the Product Name (which is a mandatory field) is not being passed while inserting the Product record. While you set the values for the different Product fields before inserting, you will need to set a value for the Name field also.

 

You will need to add the following line before inserting newProduct:

 

newProduct.Name = 'Test Product'; // You will need to decide on what value you would like to use as the Product Name 

This was selected as the best answer
Pradeep_NavatarPradeep_Navatar

It seems to me that you are missing required field of product, that is product name. You just need to give product name and then insert product :

 

 //a new Product object will be created

 Product2 newProduct = new Product2(Publication_Mention__c = PM.Id);

 

 //fields of the new Product object

newProduct.Name= ’testName’;

 newProduct.Quantity_in_Stock__c = 1;

 newProduct.Price__c = 0.00;

 newProduct.First_Publication_Date__c = Date.today();

 

 insert newProduct;

kevinjia1984kevinjia1984

Thanks a lot