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
taaataaa 

Attaching Related list from one object to another

Hi,

 

I have one custom object "Award", and there is lookup on award to Opportunity, In opportunity there is Product related list. What my require ment is, when user choose Opportunity from award, all products related to that Opportunity should attach to Award. 

 

i wrote code for this, however it is attaching only one product to award(updated one), please help me in this,

 

trigger trg_HAS_AssociateProductToAwardStagePO_AI_AU on Award__c (after insert,after update) {
//list<Award__c> awd= new list<Award__c>();
set<string> setId = new set<string>();
for(Award__c aw:trigger.new){
if(aw.Stage__c == 'Email Commitment' ) {
setId.add(aw.Opportunity__c);
system.debug('@@@@@'+setID);
}
}


//List<Product__c> pdt = [Select Id,Name,Opportunity__c, Award__c from Product__c where Opportunity__c IN: setId];
map<string,Product__c > pdtmap= new Map<string,Product__c>();
List<Product__c> pdt = new List<Product__c>();
for (Product__c obj :[Select Id,Name,Opportunity__c, Award__c from Product__c where Opportunity__c IN: setId]) {
pdtmap.put(obj.Opportunity__c ,obj);
}
Product__c prd = new Product__c();
for(Award__c aw1:trigger.new){

if(pdtmap.containskey(aw1.Opportunity__c)) {
//prd = new Product__c(id=pdtmap.get(aw1.Opportunity__c).id);
//prd = new Product__c(Opportunity__c=pdtmap.get(aw1.Opportunity__c).id);
prd = new Product__c();
prd.Opportunity__c = aw1.Opportunity__c;
prd.Award__c = aw1.Id;
pdt.add(prd);
}
}
if(pdt.size() > 0 ) {
insert pdt;

}
System.debug('pdt '+pdt);

}

 

 

 

 

Thanks

Hong_YanHong_Yan

You're missing a for loop to go through each product on the opportunity

Try something like this instead:

NOTE: This doesn't handle the case of an update very well as you will end up with duplicate products on an update of an award. I would suggest just doing it on insert.

 

//This will get you each opportunity related to the Award along with all the products on the opportunity.

Map<Id,Opportunity> oppMap=[SELECT Id, (SELECT Id, Opportunity__c, Award__c FROM Product__r) FROM Opportunity WHERE Id IN :Trigger.newMap.keySet()];

 

//loop through each award in the trigger to add the opportunity products

for(Award__c a: Trigger.newMap)

{

//get the corresponding opportunity

    Opportunity o=oppMap.get(a.Opportunity__c);

    if(o!=null)

    {

//Loop through each product in the opportunity.

//Read up on relationship queries that allows us to use getProducts__c() as a standard function.

//http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_soql_relationships.htm

        for(Product__c p:o.getProducts__c())

        {

            pdt.Add(new Product__c(Opportunity__c=o.Id, Award__c=a.Id));

        }

    }

}

 

if(!pdt.isEmpty())

    insert pdt;

Hong_YanHong_Yan

Woops, error on that first line. Need to do a sub-query to get the opportunity IDs from the Awards object.

 

Map<Id,Opportunity> oppMap=[SELECT Id, (SELECT Id, Opportunity__c, Award__c FROM Product__r) FROM Opportunity WHERE Id IN (SELECT Opportunity__c FROM Award__c WHERE Id IN :Trigger.newMap.keySet())];