You need to sign in to do that
Don't have an account?
foodrunner
Trigger to Pull Product Name to Custom Object upon creation
I am working on a trigger that will create a custom object record when a product is add to the Opportunity, where the product name will be copied to the custom record. I qwould like the product name be the name of the custom record, but the system is returning the record id in the name field. Here is my code:
trigger newSpecSheet on OpportunityLineItem (after Insert) { List<Manufacturing_Design_Sheet__c> mds = new List<Manufacturing_Design_Sheet__c>(); for (OpportunityLineItem oli: Trigger.new) { if(Pricebookentry.id != null) { mds.add(new Manufacturing_Design_Sheet__c ( Name = oli.Pricebookentry.Product2.name, Opportunity_Name__c = oli.Opportunityid, RecordTypeId = '012500000001COp', Quotation_Item_Number__c = 1)); } } insert mds; }
How do I put the name of the product in the name field on the custom record?
Thank you
Your code, while having the best of intentions, makes one minor (and non-obvious) assumption: You assume that PricebookEntry.Product2.Name is automatically available. In reality, this is not true; anything beyond the first member of an object is not automatically passed into the trigger. This is because, like most programming languages, Apex Code is a "lazy" language-- if you do not tell it to query something, it probably will not do so "for free."
Consider the following:
If you wanted to make the previous code work, you might do something like the following:
Here's some guidelines for data that is (and is not) available in triggers "for free" (does not need to be queried to access):
List<Manufacturing_Design_Sheet__c> mds = new List<Manufacturing_Design_Sheet__c>();
Id recordType = null;
for(RecordType rt:[select id,name from RecordType where SObject='Manufacturing_Design_Sheet__c' and DeveloperName='MDS_SIMPLE']) {
recordType = rt.Id;
for(OpportunityLineItem oli:[Select Id,PricebookEntry.Product2.Name,Opportunity.Id From OpportunityLineItem where Id In :Trigger.new]) {
if(oli.PricebookEntry<>null) {
mds.ass(new Manufacturing_Design_Sheet__c (
Name = oli.PricebookEntry.Product2.Name, Opportunity_Name__c = oli.Opportunity.Id, recordTypeId = rt.Id,, Qutation_Item_Number__c = 1));
}
}
insert mds;
}
Note that you should never embed an ID. You're asking for headaches later. Take the one-query hit and optimize elsewhere to make up for it.
All Answers
Your code, while having the best of intentions, makes one minor (and non-obvious) assumption: You assume that PricebookEntry.Product2.Name is automatically available. In reality, this is not true; anything beyond the first member of an object is not automatically passed into the trigger. This is because, like most programming languages, Apex Code is a "lazy" language-- if you do not tell it to query something, it probably will not do so "for free."
Consider the following:
If you wanted to make the previous code work, you might do something like the following:
Here's some guidelines for data that is (and is not) available in triggers "for free" (does not need to be queried to access):
List<Manufacturing_Design_Sheet__c> mds = new List<Manufacturing_Design_Sheet__c>();
Id recordType = null;
for(RecordType rt:[select id,name from RecordType where SObject='Manufacturing_Design_Sheet__c' and DeveloperName='MDS_SIMPLE']) {
recordType = rt.Id;
for(OpportunityLineItem oli:[Select Id,PricebookEntry.Product2.Name,Opportunity.Id From OpportunityLineItem where Id In :Trigger.new]) {
if(oli.PricebookEntry<>null) {
mds.ass(new Manufacturing_Design_Sheet__c (
Name = oli.PricebookEntry.Product2.Name, Opportunity_Name__c = oli.Opportunity.Id, recordTypeId = rt.Id,, Qutation_Item_Number__c = 1));
}
}
insert mds;
}
Note that you should never embed an ID. You're asking for headaches later. Take the one-query hit and optimize elsewhere to make up for it.
This might help you....
trigger newSpecSheet on Opportunity (after Insert) {
List<Manufacture__c> mds = new List<Manufacture__c>();
Product__c nn;
for (Opportunity oli: Trigger.new) {
if(oli.Product__c != null) {
nn=[select Name from Product__c where id=:oli.product__c];
mds.add(new Manufacture__c (Name =nn.name));
}
}
insert mds;
}
giri,
You should always avoid placing queries inside of a "for" loop, because you will run into governor limits when using the data loader or any mass-update integration.
It is a best practice to use the following pseudo-code:
1) Gather all record ID values you will use.
2) Query all records, placing them into a map.
3) Use the map to look up values that you queried.
This process is sometimes referred to as "bulkifying" code (writing code that supports multiple record updates).
Hi sfdcfox,
I am new to apex coding .
Can you tell me what is governers limit.???
You can find detailed info on Governor Limits here.
Wonderful. Thank you very much for your help.