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
Mark FazekasMark Fazekas 

beginner sfdc apex not working

Hello all !
I am new to SFDC, so I could use some help

I have to custom objects
Product Move and Deliveries
I want a trigger that makes a delivery when I make an order
with the delivery named after my product move
trigger ProductMoveTrigger on Product_Move__c ( after insert) {

    List<Delivery__c> deliveries= new List<Delivery__c>();

    for(Product_Move__c c : Trigger.new)
    {
        Delivery__c d = new Delivery__c();
        d.Name = c.Store__c + c.Product__c;

        deliveries.add(d);
    }

    try {
        insert deliveries;
    } catch (system.Dmlexception e) {
        system.debug (e);
    }   
}

where am I going wrong?

Thanks for the help
Shovan Mandal 14Shovan Mandal 14
Your code looks fine.What is the error you are getting? can you please elaborate it?
HARSHIL U PARIKHHARSHIL U PARIKH
Make sure you are not testing this trigger on After Update record. In order to test the trigger you need to insert record in Product_Move__c object.
 
trigger ProductMoveTrigger on Product_Move__c ( after insert) {

    List<Delivery__c> deliveries= new List<Delivery__c>();

    for(Product_Move__c c : Trigger.new)
    {

        Delivery__c d = new Delivery__c();
        d.Name = c.Store__c + c.Product__c;

        deliveries.add(d);
    }

    try 
    {
        If(!deliveries.IsEmpty())
        {
            insert deliveries;
        }
        
    } 
    
    catch (Exception e) 
    {
        system.debug(e.getMessage());
    }   
}


 
PreyankaPreyanka
Hello Mark,

The code is right. Please share the below information:
1. Is your Delivery__c object's Name fields is Text or Auto-Number : If it is auto number then it will not allow you to assign anything on that, as autonumber field is not writeable. If it is text then it will allow you to store whatever you want.
2. It will be good if you share the exception that you got.

Thanks
Preyanka
Mark FazekasMark Fazekas
My delivery__c object's name is Text.
exception: Arithmetic expressions must use numeric arguments (it does not allow me to add multiple strings)
And if it works I got delivery names like "a080Y00000A4jrjQAB" which is not a text I put into 
PreyankaPreyanka
I assume that your Product__c is a lookup field to Product standard object and Store__c is a Numeric field. 

If Store__c is a numeric field then while you are trying to save the trigger you will get the above error message at line
d.Name = c.Store__c + c.Product__c;

And as Product__c is a lookup field to Product so you get the delivery name as 18 digit id field, to get the name of the field find the code below

trigger ProductMoveTrigger on Product_Move__c ( after insert) {

    List<Delivery__c> deliveries= new List<Delivery__c>();
    Product2 pr = new Product2();

    for(Product_Move__c c : Trigger.new)
    {
        Delivery__c d = new Delivery__c();
        pr= [Select Name from Product2 where id=: c.Product__c];
        
        if(String.valueof(c.Store__c)!= null)
        d.Name = String.valueof(c.Store__c) + pr.Name;
        else
        d.Name = pr.Name;

        deliveries.add(d);
    }

    try {
        insert deliveries;
    } catch (system.Dmlexception e) {
        system.debug (e);
    }   
}
Thanks
Preyanka