0

Specifically, I'm trying to figure out the syntax to add the parent/master object id, to the child/detail record so the insert works without giving an error of malformed Id. I know the master name, just not the id at the time of the insert.

Apologies in advance, first day with Triggers. I don't know Java, but I do code in C#. This has a C# feeling, but I've heard Java is very similar.

Anyway, using Bulk API to add child record but failing because I don't have the Id of the Master record in the Detail record. I've heard a trigger on "before insert" will solve and I just went through the trailhead tutorial on bulk API. I think I'm missing the final aspect to being complete.

So documentation indicates a before insert operates on all records in the api call, thus single assignment is deemed bad.

I would assume at end of trigger, it will process the updates, I don't have to give it a command to continue. I would also assume I can change the values in the set of inserts to be preformed. In this case, I'm trying to replace the MasterName with the Id of the object.

If this is not the right approach - please blast away. Otherwise a couple of friendly suggestions on this code would be greatly appreciated.

trigger DetailTrigger on Detail__c (before insert) {

    List<Master__c> masts = [select id, name from Master__c];
         
    for(Detail__c dt : Trigger.new) {
        ID mastId = [select Id from masts where Name = dt.Name];
        if(dt.MasterName__c != null) dt.MasterName__c = parts.Name.contains(dt.MasterName__c).Id; 
    }
}
5
  • 1
    Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
    – Community Bot
    Dec 2, 2022 at 21:16
  • 1
    A master detail child must, by definition, have a parent. Without that parent ID, how can you know which parent to associate the record with? Are you trying to use external IDs? Are you performing a composite API call to do the creation? Please edit to include more detail.
    – Phil W
    Dec 2, 2022 at 22:56
  • BTW, C# was created by Microsoft after Sun Microsystems threatened to sue them for creating J++, which was an extended version of Java. C# core syntax (not Linq) is very similar to Java (rather than the other way round).
    – Phil W
    Dec 2, 2022 at 23:02
  • Specifically, I'm trying to figure out the syntax to add the parent/master object id, to the child/detail record so the insert works without giving an error of malformed Id. I know the master name, just not the id at the time of the insert.
    – van Zylin
    Dec 2, 2022 at 23:20
  • Well it finally dawned on me that it might not be failing on the insert, but rather the deserialization. I saw a post earlier that had a type of ID for the id and not a string. In this context, meaning I could not pass in the part number in the id field and then translate it to an ID on a lookup as it wasn't making it past the deserialization of the request.
    – van Zylin
    Dec 4, 2022 at 15:50

1 Answer 1

0

Try this (we use maps to bulkify)

trigger DetailTrigger on Detail__c (before insert) {

  // Pass 1 - get all the Master names from inserted Detail
  Set<String> masterNames = new Set<String>();
  for (Detail__c dt: Trigger.new) {
     if (dt.Master__c == null) {masterNames.add(dt.Name);}
  }
  // pivot by name
  Map<String,Master__c> mastersByName = new Map<String,Master__c>();
  if (!masterNames.isEmpty()) [
     for (Master__c master: [select id, name from Master__c WHERE Name IN: masterNames]) {
        mastersByName.put(master.Name,master);
     }
  }

  // Pass 2 , go thru Trigger.new and associate to master by Name
 for(Detail__c dt : Trigger.new) {
    if (dt.Master__c != null) {continue;} // already parented
    if (!masterByName.containsKey(dt.Name__c)) {
       dt.addError('can\'t insert this detail as no master found for ' + dt.Name__c);
       continue;
    }
    dt.Master__c = mastersByName.get(dt.Name__c).Id;
 }
}

All that said, Phil W's point is valid - normally, when one inserts details one knows the master's Id. If inserted from the UX, the master's ID is prepopulated.

1
  • Cropredy, Thanks, this makes sense as I learn more about Apex. I think the easier approach long term is to save the object id in the calling system, thus I too know the id at the time of the insert.
    – van Zylin
    Dec 4, 2022 at 15:56

You must log in to answer this question.

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