0

Newbie to salesforce and looking for a way to write a trigger that will create 11 line items for when a field is update. Object is credit request and when this is saved would like multiple credit lines to be added which is also an object. Below is a rough start, but looking for guidance. Picture attached showing what I would like to see after the request is saved and line items automatically created.

trigger TestCreditLinesAdd on Credit_Request__c(after insert){
List < Credit_Lines__c > creditlines = new List < Credit_Lines__c > ();
for (Credit_Request__c cr: trigger.new) {
    creditlines.add(new Credit_Lines__c(.Quantity__c = 0.001));
}
insert creditlines;}

enter image description here

2
  • If you want the trigger to run when a field is updated you will need to include after update either in place of or in addition to after insert.
    – JaredT
    Commented Jun 22, 2017 at 20:39
  • Yes will need to enable updates based on master qty. But if a line item is deleted intentionally need to ensure only the line items remain are updated.
    – Chris
    Commented Jun 22, 2017 at 22:21

1 Answer 1

0

Here are a couple of hints, showing how to check for a field value change and how to hook the child objects up to the parent object via the parent object Id:

for (Credit_Request__c cr: Trigger.new) {
    if (cr.FieldName__c != Trigger.oldMap.get(cr.Id).FieldName__c) {
        creditlines.add(new Credit_Lines__c(
                Credit_Request__c = cr.Id,
                Quantity__c = 0.001
                ));
        creditlines.add(new Credit_Lines__c(
                Credit_Request__c = cr.Id,
                Quantity__c = 0.002
                ));
        // Add others for a total of 11 child objects
    }
}
3
  • Thank you so much it is updating with some minor changes. I would like to add and update a Product__c field to each line item. This caused a failure. I put the terminology in '', but still fails. Would this be because this Product__c field looks up the product object
    – Chris
    Commented Jun 22, 2017 at 21:02
  • @ChrisSecrist I presume the Product__c field is a lookup? You will need to get hold of the right Ids somehow - via a query? - and set them in each item.
    – Keith C
    Commented Jun 22, 2017 at 21:18
  • Thanks yes it is a look up., will look into leveraging the id.
    – Chris
    Commented Jun 22, 2017 at 21:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .