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

Add newly inserted record's ID to corresponding record lookup in after insert trigger
Hello,
I am creating after insert trigger in obj : CMS_Rule__c which checks certain attributes and creates a record for another object MC_Cycle_vod__c . This part is working fine, but I need to map newly created MC_Cycle_vod__c record ID into same object CMS_Rule__c lookup field as CMS_Rule__c object has a lookup relationship with MC_Cycle_vod__c object. I am stuck here and unable to correctly map newly record ID to correct CMS_Rule__c. Any help would be really apreciated as I can move forward with the code! Please find my code below:
trigger CMS_Create_MC_Cycle on CMS_Rule__c (after Insert) {
List<MC_Cycle_vod__c> cycleList = new List<MC_Cycle_vod__c>();
Map<id,MC_Cycle_vod__c> updatepoaList = new Map<id,MC_Cycle_vod__c>();
List<CMS_Rule__c> poaList = [Select id,CMS_Cycle_Name__c,CMS_Country_Code__c,CMS_MC_Cycle__c,CMS_Cycle_Start_Date__c,CMS_Cycle_End_Date__c FROM CMS_Rule__c WHERE id in: trigger.new and CMS_Status__c='Draft' and CMS_Is_Processed__c=false];
for(CMS_Rule__c rulerec:poaList){
MC_Cycle_vod__c cyclerec = new MC_Cycle_vod__c();
cyclerec.Name=rulerec.CMS_Cycle_Name__c;
cyclerec.CMS_Country_Code__c = rulerec.CMS_Country_Code__c;
cyclerec.Start_Date_vod__c = rulerec.CMS_Cycle_Start_Date__c;
cyclerec.End_Date_vod__c = rulerec.CMS_Cycle_End_Date__c;
cyclerec.Status_vod__c='Planned_vod';
cycleList.add(cyclerec);
}
insert cycleList;
Set<id> mccycleId=new Set<id>();
for(MC_Cycle_vod__c mccycleRec: cycleList){
mccycleId.add(mccycleRec.id); //newly inserted ID i need to map to correct record which is running in for loop
}
}
I am creating after insert trigger in obj : CMS_Rule__c which checks certain attributes and creates a record for another object MC_Cycle_vod__c . This part is working fine, but I need to map newly created MC_Cycle_vod__c record ID into same object CMS_Rule__c lookup field as CMS_Rule__c object has a lookup relationship with MC_Cycle_vod__c object. I am stuck here and unable to correctly map newly record ID to correct CMS_Rule__c. Any help would be really apreciated as I can move forward with the code! Please find my code below:
trigger CMS_Create_MC_Cycle on CMS_Rule__c (after Insert) {
List<MC_Cycle_vod__c> cycleList = new List<MC_Cycle_vod__c>();
Map<id,MC_Cycle_vod__c> updatepoaList = new Map<id,MC_Cycle_vod__c>();
List<CMS_Rule__c> poaList = [Select id,CMS_Cycle_Name__c,CMS_Country_Code__c,CMS_MC_Cycle__c,CMS_Cycle_Start_Date__c,CMS_Cycle_End_Date__c FROM CMS_Rule__c WHERE id in: trigger.new and CMS_Status__c='Draft' and CMS_Is_Processed__c=false];
for(CMS_Rule__c rulerec:poaList){
MC_Cycle_vod__c cyclerec = new MC_Cycle_vod__c();
cyclerec.Name=rulerec.CMS_Cycle_Name__c;
cyclerec.CMS_Country_Code__c = rulerec.CMS_Country_Code__c;
cyclerec.Start_Date_vod__c = rulerec.CMS_Cycle_Start_Date__c;
cyclerec.End_Date_vod__c = rulerec.CMS_Cycle_End_Date__c;
cyclerec.Status_vod__c='Planned_vod';
cycleList.add(cyclerec);
}
insert cycleList;
Set<id> mccycleId=new Set<id>();
for(MC_Cycle_vod__c mccycleRec: cycleList){
mccycleId.add(mccycleRec.id); //newly inserted ID i need to map to correct record which is running in for loop
}
}

You don't need to query again for the records