You need to sign in to do that
Don't have an account?

Trigger Help
I had two objects : Obj A and Obj B
ObjB has a lookup relationship to objA.
objA-> fields->a1,a2,a3
objB->fields->b1,b2,b3
i need to create a trigger on objB (before insert and before update)
if trigger.isinsert then copy three fields from objA to obj B(i.e copy filed values a1,a2,a3 to b1,b2,b3)
if trigger .is update then check if the three fields are equal on obj A . if equal do nothing else update values from Obj A.
I need to write this in bulkify way. plzzz help.
Hi,
Try the below code snippet as reference:
Trigger updateobjB on objB (before insert, before update)
{
objA aa = [select a1,a2,a3 from objA where id=:trigger.new[0].lookupname];//note lookupname is the name of the lookup field from objB to objA
if(Trigger.IsInsert)
{
for(objB bb:trigger.new)
{
bb.b1 = aa.a1;
bb.b2 = aa.a2;
bb.b3 = aa.a3;
}
}
if(Trigger.Isupate)
{
for(objB bb:trigger.new)
{
if(bb.b1 != aa.a1)
bb.b1 = aa.a1;
if(bb.b2 != aa.a2)
bb.b2 = aa.a2;
if(bb.b3 != aa.a3)
bb.b3 = aa.a3;
}
}
}
Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
Hi @navatar
u write that
objA aa = [select a1,a2,a3 from objA where id=:trigger.new[0].lookupname];
u write trigger.new[0].lookupname cau plz explane who it behave when mass record of B is insert or update
it don't think so it's a bulk trigger
plz... Don't mind but it will be good for user that u check u r solutions before u post it.
I think this can solve u r issues
Trigger updateobjB on objB (before insert, before update)
{
set<Id> ArecIDSet = new set<Id>();
for(objB tempb : trigger.new)
ArecIDSet.add(tempb.lookupname) //note lookupname is the name of the lookup field from objB to objA
map<id,objA> aa = new Map<ID, objA>([select id,a1,a2,a3 from objA where id in : ArecIDSet]);
if(Trigger.IsInsert)
{
for(objB bb:trigger.new)
{
bb.b1 = aa.get(bb.lookupname).a1;
bb.b2 = aa.get(bb.lookupname).a2;
bb.b3 = aa.get(bb.lookupname).a3;
}
}
if(Trigger.Isupate)
{
for(objB bb:trigger.new)
{
if(bb.b1 != aa.get(bb.lookupname).a1)
bb.b1 = aa.get(bb.lookupname).a1;
if(bb.b2 != aa.get(bb.lookupname).a2)
bb.b2 = aa.get(bb.lookupname).a2;
if(bb.b3 != aa.get(bb.lookupname).a3)
bb.b3 = aa.get(bb.lookupname).a3;
}
}
}
do we need to write insert and update statements at the end.
No it's trigger & until u don't want to insert or update any other record which in not invole in tigger.new u do not need to write inset or update
As we are making chaes to bb object which we take from trigger.new u do not need to write insert or update
other wise u r trigger run again & again