0

I have one Object called "BillingEvent" It has 4 fields.

Ordernumber_c, vinnumber__c, CenterExitDate__c, MADCDate__c

I have another Object called "Vehicle".It has 6 fields.

Ordernumber_c,vinnumber__c,CenterExitDate__c,MADCDate__c,Status__c,Sourcing__c

BillingEvent and Vehicle dont have any realtionships. Whenever i want to insert record in BillingEvent,I want to check 3 conditions

  1. BillingEvent vinnumber__c = Vehicle vinnumber__c
  2. BillingEvent CenterExitDate__c = Vehicle CenterExitDate__c
  3. BillingEvent MADCDate__c = Vehicle MADCDate__c

I have written after insert trigger. I used a map. I am stuck here. Please give me some idea

Apex:

trigger BillingEvent_AfterInsert on BillingEvent__c (after insert) {

    List<BillingEvent__c> lstBillingEvent = new List<BillingEvent__c>([Select ID,OrderNumber__c,CATCenterExitDate__c,MADARealDate__c from BillingEvent__c where Id in :trigger.newmap.keySet()]);
    Map<Id,BillingEvent__c> mapBillingEvent=new Map<Id,BillingEvent__c>();
    Map<Id,Vehicle__c> mapVehicle=new Map<Id,Vehicle__c>();

    for(BillingEvent__c BillingEvent:lstBillingEvent) {

        mapBillingEvent.put(BillingEvent.Id,BillingEvent);
    }
    Vehicle__c vehicle=new Vehicle__c([Select id,CATCenterExitDate__c,MADARealDate__c,VINNumber__c from VEH_Vehicle__c where VINNumber__c='' ]);    
    /*for(VEH_Vehicle__c VehObj:vehicle){

      mapVehicle.put(VehObj.Id,VehObj)

    }*/
} 
1
  • Is Vehicle vinnumber__c unique in object "Vehicle"?
    – Mahmood
    Nov 15, 2016 at 11:14

1 Answer 1

1

You said that you want to check 3 conditions
1.BillingEvent vinnumber__c=Vehicle vinnumber__c
2.BillingEvent CenterExitDate__c=Vehicle CenterExitDate__c
3.BillingEvent MADCDate__c=Vehicle MADCDate__c

since there is no relationship, there is also not data to compare with.

I can suggest you to create a map

Map<string,id> VNEDMD_VidMap = new Map<string,id>();

which will be populated with vinnumber+centerExitDate+MADCDate and return you the vehicle id.

So you can change your code to something like this

List<BillingEvent__c> lstBillingEvent = new List<BillingEvent__c>([Select ID,OrderNumber__c,CATCenterExitDate__c,MADARealDate__c from BillingEvent__c where Id in :trigger.newmap.keySet()]);

set<integer> VinNo = new set<integer>();    

for(BillingEvent__c b:lstBillingEvent){
    VinNo.add(b.vinnumber__c);
}

List<Vehicle__c> vehicleList =[Select id,CATCenterExitDate__c,MADARealDate__c,VINNumber__c from VEH_Vehicle__c where VINNumber__c in:VinNo];

for(Vehicle__c v:vehicleList){
   VNEDMD_VidMap.put(v.vinnumber__c+''+v.CenterExitDate__c+v.MADCDate__c,v.id);
}

for(BillingEvent__c b:lstBillingEvent){
    if(VNEDMD_VidMap.containsKey(b.vinnumber__c+''+b.CenterExitDate__c+b.MADCDate__c)){
         system.debug('vehicle id ===> '+VNEDMD_VidMap.get(b.vinnumber__c+''+b.CenterExitDate__c+b.MADCDate__c));
    }
}

Now you know if there is a match.

And if you change your context to before insert, you can even addError to the ones doesn't have any match and avoid their creation in the system

for(BillingEvent__c b:lstBillingEvent){
    if(VNEDMD_VidMap.containsKey(b.vinnumber__c+''+b.CenterExitDate__c+b.MADCDate__c)){
         system.debug('vehicle id ===> '+VNEDMD_VidMap.get(b.vinnumber__c+''+b.CenterExitDate__c+b.MADCDate__c));
    }
    else{
        b.AddError('there is no Match!!!');
    }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .