+ Start a Discussion
koti91koti91 

Trigger Code Help

Hi 

 

I am new to Apex Coding and i had a scenario like the below which i dont know how to start. can any one plz help.

 

I need to write a trigger on Object A(with 4 record types in it say 1,2,3,4)

After update trigger fires only  when

 

1.  A.checkbox is checked

2.  A.Picklist1='test'

3.  A.Picklist2=somevalue

4.  A.previous value of picklist1  != 'test' 

 

if all the conditions above are met then trigger needs to create records in object B with a Recordtype of R with the following conditions.

 

1. if ObjectA. record type =1 

then need to copy some values from A and put it in B

2. if if ObjectA. record type =2

then need to copy some values from A and put it in B

3. if ObjectA. record type =3

then need to copy some values from A and put it in B

4. if ObjectA. record type =4

then need to copy some values from A and put it in B. 

 

I need this logic to be in a BUlkify way.....can any one help......


 

 

 

 

koti91koti91

I had written my trigger in this way:

 

Trigger tname on Obj A (After update){

for(ObjA a: Trigger.New){

if(a.checkbox is checked

& a.picklist1='test'

&a.picklist2=somevalue

& Trigger.oldmap.get(a.id).picklist1!='test')

{

objB b=new objB();

b.recordtype=R;

if(A.recordtype=1){

assign some values of A to B;

}

else if (A.recordtype=2){

assign some values of A to B;

}

else if(A,recordtype=3){

assign some values of A to B;

}

else if(A,recordtype=4){

assign some values of A to B;

}

insert b;

}

}

}

 

 

I need to bulkify this one....Plz Help

venkat-Dvenkat-D

try below approach...highlighted in blue..

 

Trigger tname on Obj A (After update){

 

//create a list of objects b List<object b> objb = new List<object b>();

for(ObjA a: Trigger.New){

if(a.checkbox is checked

& a.picklist1='test'

&a.picklist2=somevalue

& Trigger.oldmap.get(a.id).picklist1!='test')

{

objB b=new objB();

b.recordtype=R;

if(A.recordtype=1){

assign some values of A to B;

}

else if (A.recordtype=2){

assign some values of A to B;

}

else if(A,recordtype=3){

assign some values of A to B;

}

else if(A,recordtype=4){

assign some values of A to B;

}

objb.add(b);

//insert b; 

}

}

 

insert objb;

}

 

if it solves your problem, mark it as solution.