1

I have two object , abc__c and xyz__c. xyz__c has a field named key__c which should store the id of abc__c object.

abc__c has a picklist field status__c which can have different value "In-progress" , "Approved", "Sent","Error","complete". xyz__c has a status field as a text field. xyz__c record is created when status of abc__c is changed from "Approved" to "Sent". The key__c field of xyz__c will store id value of abc__C. I am using this to eastablish a connection between xyz__c and abc__c.

What I want to do:

I want to create a trigger on abc__C object and have a logic when the abc__c record is updated(afterUpdate). I want to do the following when the record is updated:

  1. when abc__c status is changed from "Approved" to "Sent" - I have to create a new record for xyz__c object if it is not exsisting yet. The key__c value should be sfdc id of abc__c. I find whether xyz__c record for an abc__c record is available or not based on the value of key__c field which holds abc__c's id.

    I am storing the id of abc__c in the field - key__c of xyz__c object so that I can query the xyz__c record which was related to abc__c object.

    How can I query inside the trigger to find the exsisting abc__c record which has the id of abc__c record. I see a challenge that I might hit the governs limit for this.

    List<abc__c> a;
    for(abc__c s: trigger.new){
        a=[select id from xyz__c where key__c = s];
    }
    
  2. Once I find the xyz__c record which stores the id of abc__c record:

    I want to make the status__c field on xyz__C record as "sent" if the status of abc__c record is changed from "approved" to "sent" only

2 Answers 2

1

You are correct. You need to get your query outside of the loop. To do this you can use

List<xyz__c> xyzList=[SELECT id, key__c FROM xyz__c WHERE key__c in: trigger.newmap.keyset()]

This will get all the xyz items associated with the updated abc items.

Next build a map linking the xyz to the abc items easily.

Map<Id, xyz__c> xyzMap=new Map<Id, xyz__c>();
for(xyz___c item: xyzList){
    xyzMap.put(item.key__c, item);
}

This gives us a map where the key is the abc__c id.

Now you can update or create the xyz items.

for(abc__c s: trigger.new){
    if(Tigger.oldmap(s.id).status__c=='approved' && s.status__c=="approved"){
        if(xyzMap.ContainsKey(s.id){
            //xyz records exsist, update it
            xyzMap.get(s.id).key__c=s.id;
        } else{
            //xyz item needs to be created.
            xyzMap.put(s.id, new xyzMap(key__c=s.id);
        }
    }
}
upsert xyzMap.values();

I have not tested this, however it should be usable.

3
  • It should be Map<Id, List<XYZ__c>> if you go this route.
    – Adrian Larson
    Commented Jan 30, 2017 at 21:37
  • I am assuming only one XYZ__C record for each ABC__c record. If there is more than one XYZ__z record for each key, as @AdrianLarson stated, you will need to use Map<Id, List<XYZ__c>> and the code will need to be modified.
    – Fred B
    Commented Jan 30, 2017 at 21:47
  • there is only one xyz__c record for one abc__c record
    – SfdcBat
    Commented Jan 30, 2017 at 22:09
2

Use a Lookup Relationship field to store this Id value, and you can take advantage of relationship queries.

for (ABC__c parent : [
    SELECT (SELECT Key__c FROM XYZs__r)
    FROM ABC__c WHERE Id IN :trigger.new
]){
    List<XYZ__c> children = parent.XYZs__r;
}

Otherwise, this is a common grouping strategy:

Map<Id, List<XYZ__c>> groupedChildren = new Map<Id, List<XYZ__c>>();
for (XYZ__c child : [SELECT Key__c FROM XYZ__c WHERE Key__c IN :trigger.newMap().keySet()])
{
    if (!groupedChildren.containsKey(child.Key__c))
        groupedChildren.put(child.Key__c, new List<XYZ__c>());
    groupedChildren.get(child.Key__c).add(child);
}

for (ABC__c record : trigger.new)
{
    List<XYZ__c> children = groupedChildren.get(record.Id);
    if (children != null)
    {
        // logic
    }
}
2
  • Hi I do not have option to make it as a lookup relationship.
    – SfdcBat
    Commented Jan 30, 2017 at 21:37
  • Hi, Sorry I forgot to mention that I have only one xyz__c record only for one abc__c record.
    – SfdcBat
    Commented Jan 31, 2017 at 4:10

You must log in to answer this question.

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