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
Andres Monge 9Andres Monge 9 

Error Trigger insert Lookup Id into the Custom Object

I have to elements united with a lookup relation: bMerchants and Products.
If you insert one Merchant two Products will be create, I write a trigger but it throw me this error:

Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger InsertProducts caused an unexpected exception, contact your administrator: InsertProducts: execution of AfterInsert caused by: System.StringException: Invalid id: 24: Trigger.InsertProducts: line 29, column 1


This is the code of the Trigger:
 
trigger InsertProducts on Merchant__c (after insert) {

  List<Product__c> products= new List<Product__c>();

  for(Merchant__c m: Trigger.new)
      {
        Product__c pproducts = new Product__c();
        pproducts.Merchant__c = String.valueOf(m.Name);
        products.add(pproducts);
      }
    
  insert products;

 }

I made everything I don't know what to do, please if any body could help me I would appreciate.

Thanks and Regards.
sbbsbb
Not sure what you are trying to do but try replacing whatever is on line 8 in your sample with this:
pproducts.Merchant__c = m.Id;