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
koti91koti91 

Trigger Help

hello

 

I am new to SFDC Development and i need some help with the trigger.

 

I created two objects A and B. there is no relation between these objects and i had event id and event id fields on both objects. 

On Object A i also had a status pick list field which has pass and Fail values.

 

on object A records are loaded by some ETL Process and after loading that records i am firing a trigger on A  which will process the reocrds for  the status value pass and then

1. checks to see if event exists by checking event ID  on A against event ID on object B.

 

how to compare this ......plz help

 

Starz26Starz26

Are you saying that you want to search ALL records for object B to see if the event ID from a appears anywhere in Object B? Is there any criteria for object B? If not, you will need to set the event id on object b to an external id to allow for indexing.

 

Also, this would be best handled in a batch or future process as the  process could take to long in a trigger. For now, here is a start.

 

trigger CheckEventID on ObjectA__c(before insert, before update){

 

Set<String> bIDs = New Set<String>();

 

for(ObjectB__c b : [Select EventID__c From ObjectB__c Where EventID__c != Null])

     bIDs.add(b.EventID__c);

 

for(ObjectA__c a : trigger.new){

 

  if(bIDs.contains(a.EventID__c){

      //do stuff

  }

 

}

 

}

koti91koti91

there is no specific condition on B. it needs to check if any event ID on A exists on B. I hope this condition works. but here there is one more thing where i need to compare the reocrds of A for which only the status on A is equal to pass value. For this  i am writinng the trigger as:

 

trigger checkeventID on A(After Insert, after update)

{

//first i am storing all the reocrds of A which needs to be compared against B in a list

 

list<A> passrecordslist =[select  id , name from A where ID In : Trigger.newMap.keyset() and status__C='pass'];

 

Set<String> bIDs = New Set<String>();

 

for(ObjectB__c b : [Select EventID__c From ObjectB__c Where EventID__c != Null])

     bIDs.add(b.EventID__c);

 

for(ObjectA__c a : passrecordslist){

 

  if(bIDs.contains(a.EventID__c){

      //do stuff

  }

 

}

 

}

 

will that work??

 

 

Starz26Starz26

just do:

 

 

trigger CheckEventID on ObjectA__c(before insert, before update){

 

Set<String> bIDs = New Set<String>();

 

for(ObjectB__c b : [Select EventID__c From ObjectB__c Where EventID__c != Null LIMIT 50000])

     bIDs.add(b.EventID__c);

 

for(ObjectA__c a : trigger.new){

 

  if(bIDs.contains(a.EventID__c) && a.Status__c == 'pass'){

      //do stuff

  }

 

}

 

}

koti91koti91

will the above method dont work

Starz26Starz26

can you elaborate a bit?

koti91koti91

I mean to say that using Trigger.newMap.KeySet() will not work. and also is it necessary to use before insert and before update . I am using after insert and after update

Starz26Starz26

if you use after, you will not be able to update anything on the object a as you will cause recursion. Much eaiser to use before

koti91koti91

ok how about using Trigger.newMap.KeySet to isolate the records that having status as pass and then use those records to check against object B

koti91koti91

Can you please reply me ....using Trigger.newMap.keyset is a correct methos or not.

Starz26Starz26

You could but you will be using an unecessary SOQL statement.

 

You already have the records in the trigger.new so thre is no need to query for them. Just check the value in the loop.

Starz26Starz26

koti91 wrote:

Can you please reply me ....using Trigger.newMap.keyset is a correct methos or not.


Was this necessary?

koti91koti91

I am using that becaue my trigger need to check for only pass records and then there are more conditions after that .

 

1. check to see if event ID exists in object B. 

2. if does not exist flag record on the Object A as faied and set Dataprocessed as checked

3. if does exist i need to see if lead exists using a loan number field on lead and Object A

4. if lead doesn't exist create lead record  and populate the loan number field and some other fields from obj A

     and also create object C record  and use the event ID  on object B to populate the event look up. object B and Object C are having a look up . Object C has a look up field to object B.

Lead and Object C has a look up. Object C has a look up field to Lead.

then set Data processed field as checked on the object A.

5. if lead exists then check to see if object C exists and and use the event ID on object B to populate the event look up.

6. if Object C does not exist create object c and use the event ID on object B to populate the event look up.

7. clean up code to delete records in object A table that are older than a week

 

This are the conditions for writing the trigger. could you please help me in writing this.