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
Venkat Reddy sfdVenkat Reddy sfd 

inserting lookup ID error : Trigger

Hi All,

when i insert record in Metadata_brand__c object, automatically record should insert in Metadata_campaign__c. for that i have written trigger please check. i am stuck in the lookup fields value passing. please anyone can help me. this is trigger
 
trigger BrandTrigger on METADATA_Brand__c (after insert, after delete, after undelete,  before delete,after update, before insert, before update) {
  TACTHelper.setStatus();
  
  List<Metadata_Campaign__c> NewCampaign= new list<Metadata_Campaign__c>();
  
  List<Metadata_Program__c> Program= [select id,name from Metadata_Program__c where Program_ID__c='54'];
  
  List<METADATA_Therapy_Class__c> therapy=[select id,name from METADATA_Therapy_Class__c where Therapy_Class_ID__c='6'];
   
   for(METADATA_Brand__c brand : Trigger.new){
   	
  Metadata_Campaign__c camp= new Metadata_Campaign__c();
  
    camp.Name=brand.Name;
    camp.Program__c=Program.Id;  // I stucked here.
    camp.Therapy_Class__c=therapy.Id; // i stucked here.
    camp.Campaign_Type__c='CIC';
    camp.Description__c=brand.Name + 'CIC General Information';
    camp.Metadata_Status__c='A';
    camp.DISPLAY_FLAG__c='Y';
    camp.Branded__c='Yes';
    camp.USMM__c='No';
    camp.HCP__c='No' ;	
  	Newcampaign.add(camp);
  }
   insert NewCampaign;
  }

 // there is no relationship with each objects. how can i achieve this.

Error Message:System.StringException: Invalid id: Program.Id: Trigger.BrandTrigger: line 15, column 1 
Best Answer chosen by Venkat Reddy sfd
Jerome LusinchiJerome Lusinchi

Hi,
here is the code :

trigger BrandTrigger on METADATA_Brand__c (after insert, after delete, after undelete,  before delete,after update, before insert, before update) {

TACTHelper.setStatus(); 

if(Trigger.isInsert() && Trigger.isAfter()){

List<Metadata_Campaign__c> NewCampaign= new list<Metadata_Campaign__c>();
List<Metadata_Program__c> Program= [select id,name from Metadata_Program__c where Program_ID__c='54']; List<METADATA_Therapy_Class__c> therapy=[select id,name from METADATA_Therapy_Class__c where Therapy_Class_ID__c='6']; for(METADATA_Brand__c brand : Trigger.new){
Metadata_Campaign__c camp= new Metadata_Campaign__c();
camp.Name=brand.Name;
if(Program.size() > 0){
     camp.Program__c=Program[0].Id;
}
if(therapy.size() > 0){
camp.Therapy_Class__c=therapy[0].Id;
}
camp.Campaign_Type__c='CIC';
camp.Description__c=brand.Name + 'CIC General Information';
camp.Metadata_Status__c='A';
camp.DISPLAY_FLAG__c='Y';
camp.Branded__c='Yes';
camp.USMM__c='No';
camp.HCP__c='No' ;
Newcampaign.add(camp);
}
insert NewCampaign;
}
}

line 3 allow you to detect if you are in an after insert trigger.


Jerome

All Answers

Jerome LusinchiJerome Lusinchi
Hi Venkat,

Program is a list, and might be empty.
try to replace line 15 by :
if(Program.size() > 0){
     camp.Program__c=Program[0].Id;
}

do the same for therapy line 16.

be carefull as well, your trigger will fire even before and after delete, are you sure that is what you want ?

Jerome
ManojjenaManojjena
Hi Venkat,

Here in your code program ,therapy are list type variable ,You are accessing list.id which is not correct . As list has many records which records you want to refer .

Metadata_Program__c,METADATA_Therapy_Class__c,Metadata_Campaign__c Is there any relationship between these three objects .

Program_ID__c='54' ,Therapy_Class_ID__c='6' how many records are there with these  conditions for each object  .

Please give more details .

 
Venkat Reddy sfdVenkat Reddy sfd

Hi jerome lusinchi,

Thanks for your reply,

I have changed like as you suggested, but i am facing problem is: when i am insrting Four records are inserting into Metadata_Campaign__c Object. could you please do favour for me. 

for this class ''TACTHelper.setStatus();'' i written all events, but  from Line 4 to 26 logic needs to Execute only after record inserted into Metadata_brand__c object.

its bit Urgent could you please help me!!!


 
Jerome LusinchiJerome Lusinchi

Hi,
here is the code :

trigger BrandTrigger on METADATA_Brand__c (after insert, after delete, after undelete,  before delete,after update, before insert, before update) {

TACTHelper.setStatus(); 

if(Trigger.isInsert() && Trigger.isAfter()){

List<Metadata_Campaign__c> NewCampaign= new list<Metadata_Campaign__c>();
List<Metadata_Program__c> Program= [select id,name from Metadata_Program__c where Program_ID__c='54']; List<METADATA_Therapy_Class__c> therapy=[select id,name from METADATA_Therapy_Class__c where Therapy_Class_ID__c='6']; for(METADATA_Brand__c brand : Trigger.new){
Metadata_Campaign__c camp= new Metadata_Campaign__c();
camp.Name=brand.Name;
if(Program.size() > 0){
     camp.Program__c=Program[0].Id;
}
if(therapy.size() > 0){
camp.Therapy_Class__c=therapy[0].Id;
}
camp.Campaign_Type__c='CIC';
camp.Description__c=brand.Name + 'CIC General Information';
camp.Metadata_Status__c='A';
camp.DISPLAY_FLAG__c='Y';
camp.Branded__c='Yes';
camp.USMM__c='No';
camp.HCP__c='No' ;
Newcampaign.add(camp);
}
insert NewCampaign;
}
}

line 3 allow you to detect if you are in an after insert trigger.


Jerome
This was selected as the best answer
Venkat Reddy sfdVenkat Reddy sfd
Hi jerome lusinchi, 

Its working fine, Thank you so much.
One small correction in line 3 Instead of 
if(Trigger.isInsert() && Trigger.isAfter()){

we have to put 
if(Trigger.isInsert && Trigger.isAfter){
Thanks again...