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
PanchoPancho 

Trigger help

Hi Guys,

I am new to writing triggers.

I have an object called Store_Visits__c and one called Field_Check_In__c.

In the case that there exists a Field_Check_In__c with the same Owner, Date and StoreID as a Store_Visit__c Owner, Date and StoreID, i want to set Store_Visit.Related_Check_In to the Check_In__c.id.

 

Here is what I have so far:

trigger CheckInTrigger on Store_visit__c (after insert, after update) {

//Find all Field_Check_Ins where owner,date, and storeId matches Store_visit__c owner, date and storeId
List<Field_Check_In__c> cis = [SELECT Id FROM Field_Check_In__c WHERE (CheckIn_Date__c IN : Trigger.new.Actual_Date_of_Visit__c ) AND ( Retail_Store__c IN : Trigger.new.Retail_Store_Visited_del__c ) AND (Owner IN : Trigger.new.Owner)];
	
//Loop through related CheckIns and set Store_visit__c.
    	 for ( Field_Check_In__c c: cis){
				Trigger.new.Related_Field_Check_In__c = c.Id;
				update c;
    	 	}
}

 The error I am getting is "Initial term of field expression must be concrete SObject: List<Store_visit__c>

 

Any ideas?

 

 

Devender MDevender M

trigger CheckInTrigger on Store_visit__c (after insert, after update) {

//Find all Field_Check_Ins where owner,date, and storeId matches Store_visit__c owner, date and storeId
List<Field_Check_In__c> cis = [SELECT Id FROM Field_Check_In__c WHERE (CheckIn_Date__c IN : Trigger.new.Actual_Date_of_Visit__c ) AND ( Retail_Store__c IN : Trigger.new.Retail_Store_Visited_del__c ) AND (Owner IN : Trigger.new.Owner)];
 List<Field_Check_In__c> cisToUpdate = new    List<Field_Check_In__c>();
//Loop through related CheckIns and set Store_visit__c.
         for ( Field_Check_In__c c: cis){
               //Trigger.new.Related_Field_Check_In__c = c.Id; you cannot directly update trigger instance

               //  update cc; dont write update in for loop

              Field_Check_In__c cc = new Field_Check_In__c(Id = c.id, Related_Field_Check_In__c = c.Id);

              cisToUpdate.add(cc);
              
             }

  update cisToUpdate;
}

PanchoPancho

Thanks amigo, its very close to working now.

When I tried your code above, I got an error on "Related_Field_Check_In__c=c.Id   and I realized that I was assigning the wrong way.  I need to update the Store_visit__c object, it has a field called Related_Field_Check_In__c that needs to have the id of the check in.

 

Here is what I have now.  The problem is where the ???? is in the code.  I am not sure how to keep the original Store_visit__c id so I can put it in the update.

Thank you so much for your help.

 

trigger CheckInTrigger on Store_visit__c (after insert, after update) {

//Find all Field_Check_Ins where owner,date, and storeId matches Store_visit__c owner, date and storeId
List<Field_Check_In__c> cis = [SELECT Id FROM Field_Check_In__c WHERE (CheckIn_Date__c IN : Trigger.new.Actual_Date_of_Visit__c ) AND ( Retail_Store__c IN : Trigger.new.Retail_Store_Visited_del__c ) AND (Owner IN : Trigger.new.Owner)];

List<Store_visit__c> cisToUpdate = new    List<Store_visit__c>();
//Loop through related CheckIns and set Store_visit__c.
    	 for ( Field_Check_In__c c: cis){
		Store_visit__c cc = new Store_visit__c(Id = ????, Related_Field_Check_In__c = c.Id);
              	cisToUpdate.add(cc);
    	 }
    	 update cisToUpdate;	
}

 

 

Michael_TorchedloMichael_Torchedlo

If I understand your recent post, your object has a field Related_Field_Check_In__c that will take one id, and you are quering to find a List of Field_Check_In__c to find one that matches and populate that field.  If your field can only hold ONE id, then you could have problems if you query finds more than one Field_Check_In__c for the same date, owner, and store.  There is also no guarantee that it finds a matching Field_Check_In for every Store_visit__c - it's conceivable that sometimes the trigger will have no query rows to return, which would cause an exception.  The query will not necessarily give you a one-to-one correspondence, unless you have some other conditions in your organization that prevents users from creating two Field Check Ins on the same date at the same store for the same person.

 

Also, the trigger is on the Store_visit__c object.  If that is the same object that you are trying to update, then you should not need another "Update" call inside the trigger.  The object is already being updated, so why don't you try using a before trigger instead of after?  That way, you can just fill in the missing field value using Trigger.new and once the trigger code is completed, the DML operation will finish saving.

 

trigger CheckInTrigger on Store_visit__c (before insert, before update) {

//Find all Field_Check_Ins where owner,date, and storeId matches Store_visit__c owner, date and storeId

List<Field_Check_In__c> cis = [SELECT Id FROM Field_Check_In__c WHERE (CheckIn_Date__c IN : Trigger.new.Actual_Date_of_Visit__c ) AND ( Retail_Store__c IN : Trigger.new.Retail_Store_Visited_del__c ) AND (Owner IN : Trigger.new.Owner)];

List<Store_visit__c> cisToUpdate = new    List<Store_visit__c>();
//Loop through related CheckIns and set Store_visit__c.
for ( Field_Check_In__c c: cis){
   for (Store_visit__c x : Trigger.new)
      if ((x.Actual_Date_of_Visit__c == c.CheckIn_Date__c)&&(x.Owner == c.Owner)&&  
(x.Retail_Store_Visited_del__c == c.Retail_Store__c)){                 //you may need to change the above line to be x.OwnerId == c.OwnerId x.Related_Field_Check_In__c = c.id; } } }