0

I have 2 objects, "Ticket" and "Ticket Item". They have a master-detail relationship with Ticket as master.

I need to replace the value of the integer sum with the value of Starting_Sequence_Number__c from the parent run. The for loop will then take the new value of sum and update the value of Starting_Sequence_Number__c back to the parent.

I have seen code like this to pull info from a parent just not sure how to assign it to a integer.

List<ParentObject> List = [SELECT Name, Id FROM ChildRecord WHERE ParentObject__c IN : trigger.newMap.keySet();

trigger SAP_Seq_Number on FX5__Ticket_Item__c (Before insert) {
    integer sum = 10;

    for(integer i= 0;i<Trigger.new.size();i++){
        sum = i *10;         
        Trigger.new[i].SAP_Sequence_Number__c = sum;
    }
}
2
  • 1
    Your question is confusing, at least to me, could you try explaining it again? In particular, how do you want to calculate the Starting_Sequence_Number__c on the parent records?
    – smukov
    Oct 9, 2016 at 19:22
  • The way I need it to work is every time a “ticket item” is added to a "ticket" it get a Sequence number starting at 10. The sequence would jump by 10 for each ticket item added to a ticket, so for example 10,20,30,40,ECT. If a ticket items is deleted from the ticket the seq number does not change it stays the same for that ticket. If the user adds more ticket items to a ticket it does not start over it use the value in Starting_Sequence_Number__c + 10 then updates Starting_Sequence_Number__c when its done with the for loop.
    – Jeremy
    Oct 9, 2016 at 19:26

1 Answer 1

0

Try the trigger below. I wrote it in a more generic form, so you'll have to update the API names in order for it to work in your org. You should get a general idea on how to implement this.

trigger SAP_Seq_Number on Ticket_Item__c (before insert) {

    //first get all the parents 
    Set<Id> parentIds = new Set<Id>();
    for(Ticket_Item__c child : Trigger.new){
        parentIds.add(child.Ticket__c);
    }

    Map<Id, Ticket__c> parents = new Map<Id, Ticket__c>([
        SELECT Id, Ticket_Counter__c 
        FROM Ticket__c
        WHERE Id in :parentIds
    ]);

    //now update the Ticket Item counter on the parent
    //and calculate the Ticket Item Sequence
    for(Ticket_Item__c child : Trigger.new){
        Ticket__c parent = parents.get(child.Ticket__c);
        parent.Ticket_Counter__c++;
        child.SAP_Sequence_Number__c = 10 * parent.Ticket_Counter__c;
    }

    update parents.values();
}
  • Ticket__c and Ticket_Item__c are your two objects.
  • Ticket__c field on the Ticket_Item__c object is the M-D field pointing to the parent Ticket.
  • Ticket_Counter__c is a Number field on Ticket object, it will keep the count of the Ticket Items added to the Ticket record.
4
  • Getting error "Illegal assignment from FX5__Ticket__c to Case" on Line 18
    – Jeremy
    Oct 10, 2016 at 15:52
  • @Jeremy Sorry, I tested the logic on two objects in my dev org, one of which included the Case object, and I haven't fixed the code when I pasted it. Just replace the Case parent = ... inside the for loop, with FX5__Ticket__c parent = ... in your code.
    – smukov
    Oct 10, 2016 at 15:56
  • New error Line 0 "duplicate value found: <unknown> duplicates value on record with id: <unknown>"
    – Jeremy
    Oct 10, 2016 at 16:04
  • Never mind I got it
    – Jeremy
    Oct 10, 2016 at 16:14

You must log in to answer this question.

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