Skip to main content
All,

 

I am requesting for assistance with a cross object trigger that I have been struggling to create. This is my first attempt in creating a Cross object trigger and I would appreciate any assistance.

 

Below are the names of the objects and fields.

 

In the Judgment_Event__c I have the following fields below.

 

Action_Type__c                    (picklist)

 

Recording_Location__c          (picklist)

 

Expiration_Date__c                (date)

 

Recording_Date__c                 (date)

 

Recording_Office_State__c   (Picklist)

 

 

Below are fields I have in the Judgment_Exp_Matrix__c object.

 

Years__c       (Number)

State1__c      Lookup(State)

 

 

MY REQUEST BELOW

 

In my trigger within the Judgment_Event__c ,   when a user selects Action_Type__c  = = ‘Recording’

And  Recording_Location__c   = = ‘County’

And  Recording_Office_State__c ==’a state that user chose  from a picklist’ (example if someone selects California)

 

AND

 

In Judgment_Exp_Matrix__c when a user selects a number in the Years__c and a state within the State1__c (example: this has to be the same state in the Recording_Office_State__c in Judgment_Event__c  ). I would like the number in  Years__c to add with the date in Recording_Date__c in Judgment_Event__c , and the final calculation will be automatically populate in the Expiration_Date__c.  For example: if the date in the Recording_Date__c is 1/01/2012 and a user selects the number 2 in the Years__c field, 1/01/2014 will be automatically populated in the Expiration_Date__c .

 

Note: Just a reminder, in the Judgment_Event__c object, the State in the Recording_Office_State__c has to be the same state in the  State1__c in Judgment_Exp_Matrix__c . If the state values are not the same in both fields, then the trigger should not initiate.

 

Below is the only coding I have currently which is not much.

 

trigger NewYear on Judgment_Event__c (before insert,before update) {

    for(Judgment_Event__c JE: Trigger.new){

        if(je.Action_Type__c == 'Recording' && je.Recording_Location__c == 'County') {

    }

}

Regards,
29 answers
  1. Jan 6, 2015, 9:08 PM
    can you try below code and send back the debug log once again, i dont see any issue but just to make sure

     

    trigger NewYear on Judgment_Event__c (before insert,before update) {

    /*

    Since we can not have the correct value of formula fields in before triggers, we need to fire a query to fetch the state information and the record it belongs to.

    */

    Set<String> States_Set=new Set<String>();//This is to collect all the states from the records on which the trigger fired.

    Map<Id,String>JEStateMap=new Map<Id,String>();//This is to collect the id of JE record and the state it needs to be used

    Map<String,Integer>CountyStateYearMap=new Map<String,Integer>();//This is to collect all the information necessary based on State in County record

    Map<String,Integer>OtherStateYearMap=new Map<String,Integer>();//This is to collect all the information necessary based on State if other state is selected

    Map<String,Integer>SecretaryStateYearMap=new Map<String,Integer>();//This is to collect all the information necessary based on State if secretary state is selected

    Map<Id,County__c>CountyInfoMap=new Map<Id,County__c>();//To collect all the information related to county

    Set<Id>CountyIds=new Set<Id>();

    system.debug('JE Information ***'+Trigger.new);

    for(Judgment_Event__c JE: Trigger.new){

    if(je.Action_Type__c == 'Recording' ) {

    if(JE.Recording_Location__c=='County'){

    if(JE.County__c!=Null)

    CountyIds.add(JE.County__c);

    }

    if(JE.Recording_Location__c=='Other State Office' || JE.Recording_Location__c=='Secretary of State'){

    if(JE.Recording_Office_State__c!=Null)

    States_Set.add(JE.Recording_Office_State__c);

    }

    }

    }// By End of this for loop, we will have the county id information, states from Recording_Office_State__c based on conditions.

    system.debug('*** States_Set, CountyIds'+States_Set+'**'+CountyIds);

    //Time to query county and get the required state information and add it to our state list

    for(County__c countyRecord:[select id,state__r.Name From county__c where id iN :CountyIds]){

    CountyInfoMap.put(countyRecord.id,countyRecord);

    States_Set.add(countyRecord.state__r.Name);

    }

    //Now we have all the state information based on conditions, query the Judgment_Exp_Matrix__c and get corresponding year information.

    system.debug('*** States_Set, CountyInfoMap'+States_Set+'**'+CountyInfoMap);

    for(Judgment_Exp_Matrix__c JEM:[select id,Years__c,Action_Type__c,

    Recording_Location__c,State1__c,State1__r.Name,

    Recording_Location__c

    from Judgment_Exp_Matrix__c where State1__r.Name IN :States_Set]){

    if(JEM.Action_Type__c == 'Recording'){

    IF(JEM.Recording_Location__c=='County' ) {

    if(JEM.Years__c!=Null)

    CountyStateYearMap.put(JEM.State1__r.Name,Integer.valueof(JEM.Years__c));

    }

    If(JEM.Recording_Location__c == 'Other State Office' ) {

    if(JEM.Years__c!=Null)

    OtherStateYearMap.put(JEM.State1__r.Name,Integer.valueof(JEM.Years__c));

    }

    If(JEM.Recording_Location__c == 'Secretary of State' ) {

    if(JEM.Years__c!=Null)

    SecretaryStateYearMap.put(JEM.State1__r.Name,Integer.valueof(JEM.Years__c));

    }

    }

    }// By end of this loop we will have all the information grouped on our conditions, its time to add the years to the Recording_Date__c

    system.debug('Maps Coming as'+'**+CountyStateYearMap+'***'+OtherStateYearMap+'***'+SecretaryStateYearMap);

    for(Judgment_Event__c JE: Trigger.new){

    if(je.Action_Type__c == 'Recording' ) {

    if(JE.Recording_Location__c=='County' && JE.County__c!=Null && JE.Recording_Date__c!=Null){

    if(CountyStateYearMap.keyset().contains(CountyInfoMap.get(JE.County__c).state__r.Name))

    JE.Expiration_Date__c=JE.Recording_Date__c.addYears(CountyStateYearMap.get(CountyInfoMap.get(JE.County__c).state__r.Name));

    }

    if(JE.Recording_Location__c=='Other State Office' && JE.Recording_Office_State__c!=Null && JE.Recording_Date__c!=Null){

    if(OtherStateYearMap.keyset().contains(JE.Recording_Office_State__c))

    JE.Expiration_Date__c=JE.Recording_Date__c.addYears(OtherStateYearMap.get(JE.Recording_Office_State__c));

    }

    if(JE.Recording_Location__c=='Secretary of State'&& JE.Recording_Office_State__c!=Null && JE.Recording_Date__c!=Null ){

    if(SecretaryStateYearMap.keyset().contains(JE.Recording_Office_State__c))

    JE.Expiration_Date__c=JE.Recording_Date__c.addYears(SecretaryStateYearMap.get(JE.Recording_Office_State__c));

    }

    }

    system.debug('JE Final date for id **'+JE.Id+'***'+JE.Expiration_Date__c);

    }// By end of this loop we will have the expiration date populated based on conditions.

    system.debug('JE Final INFO ****'+trigger.new);

    }

     
0/9000