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
PaulMacPaulMac 

Best Approach - Automatic Record Insert

I'm just looking for a high level approach to what I want to do (i.e. do I need a Trigger or a Class, or something else).

 

When a user adds a certain product code to a sales order, look for associated product codes and automatically enter them in for the user.

 

I tried a huge trigger with many Else If statements and Insert statements. Although the code seems to compile. I can't save it to the server. Looks like I violating some governer limits.

 

Ideally, I'll have Product and Associated products in a table.

 

ProductA | ProductAQuaterlyUpdate1

ProductA | ProductAQuaterlyUpdate2

ProductA | ProductAQuaterlyUpdate3

ProductA | ProductAQuaterlyUpdate4

ProductB | ProductBSemiAnnualUpdate1

ProductB | ProductBSemiAnnualUpdate2

 

Any thoughts on how I go about this?

 

thanks for your time!

aalbertaalbert

Can you post the code that compiled but threw governor limit exceptions? I can try to recommend ways to refactor the code to avoid the governor limits.

 

More information on Apex Coding best practices here

PaulMacPaulMac

Thanks for looking at this. Below is the "top" part of the code...the actual code contains many more else if statements. I can post the entire code if you like.

Thanks, Paul

 

trigger trg_InsertUpdateProductLines on SFDC_520_QuoteLine__c (after insert) {

for (SFDC_520_QuoteLine__c obj : Trigger.new)

 

if (obj.Product2__c == '01t500000014PepAAE') //US 5 Digit ZIP Codes (annual update)

{

SFDC_520_QuoteLine__c newLineItem = new SFDC_520_QuoteLine__c(

Quote__c = obj.Quote__c, //SalesOrder Number

Product2__c = '01t500000014PeqAAE',

Qty_Ordered__c = 1,

Unit_Price__c = 0);

insert newLineItem;

 

SFDC_520_QuoteLine__c newLineItem2 = new SFDC_520_QuoteLine__c(

Quote__c = obj.Quote__c,Product2__c = '01t500000014PerAAE',Qty_Ordered__c = 1,Unit_Price__c = 0);

insert newLineItem2;

}

else if (obj.Product2__c == '01t500000014PesAAE') //US 5 Digit ZIP Codes Renewal (annual update)

{

SFDC_520_QuoteLine__c newLineItem3 = new SFDC_520_QuoteLine__c(

Quote__c = obj.Quote__c,Product2__c = '01t500000014PetAAE',Qty_Ordered__c = 1,Unit_Price__c = 0);

insert newLineItem3;

}

else if (obj.Product2__c == '01t500000014PeuAAE') //US 5 Digit ZIP Codes (quarterly updates)

{

SFDC_520_QuoteLine__c newLineItem4 = new SFDC_520_QuoteLine__c(

Quote__c = obj.Quote__c,Product2__c = '01t500000014PevAAE',Qty_Ordered__c = 1,Unit_Price__c = 0);

insert newLineItem4;

 

SFDC_520_QuoteLine__c newLineItem5 = new SFDC_520_QuoteLine__c(

Quote__c = obj.Quote__c,Product2__c = '01t500000014PewAAE',Qty_Ordered__c = 1,Unit_Price__c = 0);

insert newLineItem5;

 

SFDC_520_QuoteLine__c newLineItem6 = new SFDC_520_QuoteLine__c(

Quote__c = obj.Quote__c,Product2__c = '01t500000014PexAAE',Qty_Ordered__c = 1,Unit_Price__c = 0);

insert newLineItem6;

 

SFDC_520_QuoteLine__c newLineItem7 = new SFDC_520_QuoteLine__c(

Quote__c = obj.Quote__c,Product2__c = '01t500000014PeyAAE',Qty_Ordered__c = 1,Unit_Price__c = 0);

insert newLineItem7;

}

else if (obj.Product2__c == '01t500000014PezAAE') //US 5 Digit ZIP Codes Renewal (quarterly updates)

{

SFDC_520_QuoteLine__c newLineItem4 = new SFDC_520_QuoteLine__c(

Quote__c = obj.Quote__c,Product2__c = '01t500000014Pf0AAE',Qty_Ordered__c = 1,Unit_Price__c = 0);

insert newLineItem4;

 

SFDC_520_QuoteLine__c newLineItem5 = new SFDC_520_QuoteLine__c(

Quote__c = obj.Quote__c,Product2__c = '01t500000014Pf1AAE',Qty_Ordered__c = 1,Unit_Price__c = 0);

insert newLineItem5;

 

SFDC_520_QuoteLine__c newLineItem6 = new SFDC_520_QuoteLine__c(

Quote__c = obj.Quote__c,Product2__c = '01t500000014Pf2AAE',Qty_Ordered__c = 1,Unit_Price__c = 0);

insert newLineItem6;

 

SFDC_520_QuoteLine__c newLineItem7 = new SFDC_520_QuoteLine__c(

Quote__c = obj.Quote__c,Product2__c = '01t500000014Pf3AAE',Qty_Ordered__c = 1,Unit_Price__c = 0);

insert newLineItem7;

}