+ Start a Discussion
Pandu@SfdcPandu@Sfdc 

Trigger after insert , after update

Relation: Opportunity has related list A and B custom objects. A and B has no relation. 

Criteria: When I insert or Update A records, B records have to create or update

Help: How to I create/update records in B

 

trigger A2B on A (after insert, after update){
set<id> optId = new set<id>();
for(A o : Trigger.new){
optId.add(o.Opportunity__c);
}
map<Id,Opportunity> oppMap = new map<Id,Opportunity>([Select ID from Opportunity where Id IN: optId]);

for (A opt : Trigger.new){
if (oppMap.containskey(opt.Opportunity__c)){
// Opportunity thisOpportunity = optMap.get(opt.Opportunity__c);
}
}
}

 

Thanks,

Pandu

Best Answer chosen by Admin (Salesforce Developers) 
SFDCStarSFDCStar

trigger A2B on A (after insert, after update){

set<Id> oppId = new set<Id>();


for(A op : Trigger.new){
oppId.add(op.Opportunity__c);
}

map<Id,A> aMap = new map<Id,A>([Select Id, Field you want where Opportunity__c IN: oppId]);

map<Id,B> bMap = new Map<Id,B>([Select Id, AIdHold__c(Unique field in Object B) from B where Opportunity__c in :oppId]);

Map<String,Id> AIdMap = new Map<String,Id>();
if(bMap.size()>0){
for(B s : bMap.values()){
AIdMap.put(s.AIdHold__c,s.Id);
}
}

List<B> insertB = new List<B>();
List<B> updateB = new List<B>();

for (A opt : aMap.values()){

if(bMap.size() > 0 && !AIdMap.containsKey(opt.Id)){

B sq = new B();
sq.AIdHold__c = opt.Id;

//Use whatever fields you want
insertB.add(sq);
}
else{
B sq1 = new B();
sq1 = bMap.get.AIdMap.get(opt.Id));
///Use whatever fields you want
sq1.Fieldname = opt.Fieldname;

updateB.add(sq1);
}

}
if(insertB.size()>0){
insert insertB;
}
if(updateB.size()>0){
Update updateB;
}
}

All Answers

Jia HuJia Hu
What is the connection of A and B, when A record is updated, how do you determine which B record should be updated?

And then in the Trigger of A object, use this connection to query B object and find the record to update.
SFDCStarSFDCStar

Based on Opportunity Id. If Opportunity has 4 records for object A; in Object B 4 records have to create if they are not exist based on unique value in object B, It holds Object A record ID.

SFDCStarSFDCStar

trigger A2B on A (after insert, after update){

set<Id> oppId = new set<Id>();


for(A op : Trigger.new){
oppId.add(op.Opportunity__c);
}

map<Id,A> aMap = new map<Id,A>([Select Id, Field you want where Opportunity__c IN: oppId]);

map<Id,B> bMap = new Map<Id,B>([Select Id, AIdHold__c(Unique field in Object B) from B where Opportunity__c in :oppId]);

Map<String,Id> AIdMap = new Map<String,Id>();
if(bMap.size()>0){
for(B s : bMap.values()){
AIdMap.put(s.AIdHold__c,s.Id);
}
}

List<B> insertB = new List<B>();
List<B> updateB = new List<B>();

for (A opt : aMap.values()){

if(bMap.size() > 0 && !AIdMap.containsKey(opt.Id)){

B sq = new B();
sq.AIdHold__c = opt.Id;

//Use whatever fields you want
insertB.add(sq);
}
else{
B sq1 = new B();
sq1 = bMap.get.AIdMap.get(opt.Id));
///Use whatever fields you want
sq1.Fieldname = opt.Fieldname;

updateB.add(sq1);
}

}
if(insertB.size()>0){
insert insertB;
}
if(updateB.size()>0){
Update updateB;
}
}

This was selected as the best answer