You need to sign in to do that
Don't have an account?
LAMCORP
Trigger that creates an object record - Pass "Name" details
Hi,
I have written a trigger to creates a record for a custom obj "Line_del__c" after a Lead is inserted.
The only thing I can not get the Name of the Lead to pass over to "Line_del__c" Name field. It keeps passing an id like "a0OV000000078Mg"
Can not work it out!! Can anyone help please?
trigger InsertNewMonitorLine on Lead (after insert) {
List<Lead> lam = new List<Lead>();
for(Lead a: trigger.new){
// insert a custom object
Line_del__c obj = new Line_del__c();
obj.Name = a.Name;
obj.Team_s_Dept_s_del__c = 'a0PV0000000EhlM';
obj.ME1__c = '00530000004WakH';
obj.Linked_NB__c = a.Id;
obj.DTR3__c = System.Today();
insert obj;
insert lam;
}
}
Can't see any reason why you wouldn't be getting the name, are you sure there's nothing setting a Lead's name to be the same as it's ID?
That aside, you need to restructure this code a little so that you're not doing an insert per lead, also you're inserting an empty list!
Another concerning thing is the 'Del' in the object's name — this suggest it's been deleted, undeleted and not subsequently renamed (objects have Del inserted into their name when they're deleted).
All Answers
Can't see any reason why you wouldn't be getting the name, are you sure there's nothing setting a Lead's name to be the same as it's ID?
That aside, you need to restructure this code a little so that you're not doing an insert per lead, also you're inserting an empty list!
Another concerning thing is the 'Del' in the object's name — this suggest it's been deleted, undeleted and not subsequently renamed (objects have Del inserted into their name when they're deleted).
Hey Matt,
Thanks for your reply.
Newbie to Apex hence the mistakes.
Think I was working on this one too late last night. Have had a closer look this morning and worked out that the "Name" field on the lead object is the 1st Name field!!! I was leaving it blank and it was obviously assigning an auto id. All working in that respect now.
Thanks for help with the List. I am still trying to get my head around collections. Got some material to read over weekend. Hoping it will soak in soon lol..
Can you help with how I would go about restructing my code so that I am not doing an insert per lead?
You are right I undeleted this object and never renamed it. Will that cause any probs? Did it a long time ago.
Hey,
Not renaming it won't cause any problems, but it's best practice to keep things neat and tidy! As for refactoring, if you do it the way I've done you can see that I don't have the insert inside the for loop, instead I put all the new objects to insert into a list, and then just insert that list in one go afterwards.
You can perform database operations on up to 200 records at once, which also happens to be the maximum number of records that can be passed into a trigger at once (i.e. the trigger.new collection in this case will have a max size of 200). Because there's a 1:1 mapping it saves a bit of faffing around as we know there'll only ever be 200 records in that list we're inserting.
Hope that helps, good luck with the platform!
Matt,
That's a great help.. starting to click. Appreciate your time and pointers.
Thanks a lot!