+ Start a Discussion
edrickwongedrickwong 

Tigger to update Picklist field from one object to another object

Hi All,

 

I'm very brand new to apex coding and need a little help creating a trigger that will update a picklist field (Occupied_Status__c) from one custom object (Inspection_Checklist__c) to another custom object (Property_Evaluation__c) only when another field is TRUE in the original object that is being updated (Inspection_Checklist__c.Inspection_Complete__c = TRUE).

 

Below is the code I've created so far, I might be completely off, but any help would be appreciated. Thanks in advance! 

 

Right now I'm getting the following error in line 6: Save error: unexpected token: ':'

 

 

trigger OccupancyStatus on Inspection_Checklist__c (after update) {
	
	List<Property_Evaluation__c> eval =
	    [SELECT j.Occupied_Status__c
	     FROM Inspection_Checklist__c j
	     WHERE j.Inspection_Checklist__c.Inspection_Complete__c = TRUE : Trigger.new
	      FOR UPDATE];
	      
	for (Property_Evaluation__c li: eval){
		li.Occupied_Status__c=li.Inspection_Checklist__r.Occupied_Status__c;
	}
    update eval;
}

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
DharmeshDharmesh

here you go

 

 

trigger OccupancyStatus on Inspection_Checklist__c (before update) {
   List<Property_Evaluation__c> peList = new List<Property_Evaluation__c>();

         for(Inspection_Checklist__c  ic : [select id,Inspection_Complete__c ,Occupied_Status__c,Property_Evaluation__r.id from Inspection_Checklist__c where                                                                      id in :Trigger.newMap.keySet()])

        {

           if(ic.Inspection_Complete__c &amp;&amp; ic.Property_Evaluation__r!=null &amp;&amp; ic.Occupied_Status__c !=null){  

peList.add(new Property_Evaluation__c(id=ic.Property_Evaluation__r.id,Occupied_Status__c =ic.Occupied_Status__c,));

            }          

        }        

       update peList;

}

All Answers

DharmeshDharmesh

I dont know the relation between the object you have described but I assume particular relation and here is the code if it works for you

if it doesnt work would you please describe in detail so that it would help me understand the problem

trigger OccupancyStatus on Inspection_Checklist__c (before update) {									

	for(Inspection_Checklist__c ic : Trigger.new){
		if(c.Inspection_Complete__c){
			ic.Property_Evaluation__r.Occupied_Status__c=ic.Occupied_Status__c;
		}
	}
}
edrickwongedrickwong

Hi Dharmesh,

 

Here is the error that I am getting:

 

System.NullPointerException: Attempt to de-reference a null object: Trigger.OccupancyStatus: line 5, column 13

 

I have a one to many relationship where the Property_Evaluation is the parent and the Inspection_Checklist is the child. I'm want the parent updated whenever the child is updated.

 

Thanks!

DharmeshDharmesh

Try following code

trigger OccupancyStatus on Inspection_Checklist__c (before update) {									

	for(Inspection_Checklist__c ic : Trigger.new){
		if(ic.Inspection_Complete__c && ic.Property_Evaluation__r!=null && ic.Occupied_Status__c !=null){
			ic.Property_Evaluation__r.Occupied_Status__c=ic.Occupied_Status__c;
		}
	}
}
nagasnagas

hi as per seeing your scenarion i am writing a trigger hope it works for you

 

 

trigger fieldupdate on Inspection_checklist_c (before update)
{

Map<id,property_evaluation__c> proper = new Map<id,property_evaluation__c>();

for(property_evaluation__c pev : [select id, occupied_status__c from property_evaluation__c where id=:Trigger.new[0].property_evaluation__c])

{
  if(!proper.containskey(pev.id))
  {
    proper.put(pev.id.pev);
  }  
}

for(inspection_checklist__c ins :trigger.new)
{

 if(ins.Inspection_Complete__c == TRUE)
  

   proper.get(ins.id).occupied_status__c = ins.occupied_status__c;
  }



}

 

 

nagasnagas

 

trigger fieldupdate on property_evaluation__c (before update)
{

Map<id,inspection_checklist__c> proper = new Map<id,inspection_checklist__c>();

for(inspection_checklist__c pev : [select id, occupied_status__c,inspection_complete__c,property_evaluation__c from inspection_checklist__c where property_evaluation__c =:Trigger.new[0].id] and inspection_complete__c = true)

{
  if(!proper.containskey(pev.id))
  {
    proper.put(pev.id.pev);
  }  
}

for(property_evaluation__c ins :trigger.new)
{
  if(proper.containskey(ins.id))
   ins.occupied_status__c = proper.get(ins.id).occupied_status__c;
  }



}

 try this one instead of the other one i made some changes...........

 

edrickwongedrickwong

Dharmesh,

 

The code returns no errors, however it is not updating the Property_Evaluation__c.Occupied_Status__c field after I update the Inspection_Checklist__c object.

edrickwongedrickwong

Nagas,

 

So your trigger is on the Parent object and not on the object that I would be updating? I'm also getting the following error on your code:

 

 

Compile Error: expecting a right parentheses, found 'and' at line 7 column 74	

 

It's looking for a parentheses between Trigger.new[0].id] and

 

DharmeshDharmesh

need to check the value in log as below. I think because of some value is null, its not entering into if loop and hence not updating the value

trigger OccupancyStatus on Inspection_Checklist__c (before update) {									

	for(Inspection_Checklist__c ic : Trigger.new){
                System.debug('---------ic.Inspection_Complete__c---- :'+ic.Inspection_Complete__c);//it should be true
                System.debug('---------ic.Property_Evaluation__r---- :'+ic.Property_Evaluation__r);//it should not be null
                System.debug('---------ic.Occupied_Status__c---- :'+ic.Occupied_Status__c);//it should not be null
		if(ic.Inspection_Complete__c && ic.Property_Evaluation__r!=null && ic.Occupied_Status__c !=null){
			ic.Property_Evaluation__r.Occupied_Status__c=ic.Occupied_Status__c;
		}
	}
}
nagasnagas

in that case try to complie  the first trigger i have given only and let me know where you are getting the error.i didnot compiled it you may get some error.

edrickwongedrickwong

Nagas

 

here's the error I'm receiving

 

 

Invalid foreign key relationship: Property_Evaluation__c.id at line 11 column 16	

 

 

nagasnagas

 

trigger fieldupdate on Inspection_checklist_c (before update)
{

Map<id,property_evaluation__c> proper = new Map<id,property_evaluation__c>();

for(property_evaluation__c pev : [select id, occupied_status__c from property_evaluation__c where id=:Trigger.new[0].property_evaluation__c])

{
  if(!proper.containskey(pev.id))
  {
    proper.put(pev.id,pev);
  }  
}

for(inspection_checklist__c ins :trigger.new)
{

 if(ins.Inspection_Complete__c == TRUE)
  

   proper.get(ins.id).occupied_status__c = ins.occupied_status__c;
  }



}

 

 

nagasnagas

 

trigger fieldupdate on Inspection_checklist_c (before update)
{

Map<id,property_evaluation__c> proper = new Map<id,property_evaluation__c>();

for(property_evaluation__c pev : [select id, occupied_status__c from property_evaluation__c where id=:Trigger.new[0].property_evaluation__c])

{
  if(!proper.containskey(pev.id))
  {
    proper.put(pev.id,pev);
  }  
}

for(inspection_checklist__c ins :trigger.new)
{

 if(ins.Inspection_Complete__c == TRUE)
  {
    if(proper.containskey(ins.id))
     { 

   proper.get(ins.id).occupied_status__c = ins.occupied_status__c;
     }
   }
}


}

 

try this one made some more modifications

 

edrickwongedrickwong

No errors in the code, but it's not updating the Property_Evaluation__c.Occupied_Status__c field....

edrickwongedrickwong

Dharmesh,

 

I added your code, where do I go to get the log?

DharmeshDharmesh

check here

 

Administration Setup->Monitoring->Debug Log

add your name here and execute the code. the detail log will be available at specified location

edrickwongedrickwong

Here's what I get when I first create the obejct, but I dont' get another reference to the trigger after I try updating the record.

 

 

16:20:32.424|CODE_UNIT_STARTED|[EXTERNAL]|01qT0000000Ccxu|OccupancyStatus on Inspection_Checklist trigger event BeforeUpdate for [a06T0000000HJxZ]
16:20:32.424|METHOD_ENTRY|[4]|System.debug(ANY)
16:20:32.424|USER_DEBUG|[4]|DEBUG|---------ic.Inspection_Complete__c---- :false
16:20:32.424|METHOD_EXIT|[4]|System.debug(ANY)
16:20:32.424|METHOD_ENTRY|[5]|System.debug(ANY)
16:20:32.424|USER_DEBUG|[5]|DEBUG|---------ic.Property_Evaluation__r---- :null
16:20:32.424|METHOD_EXIT|[5]|System.debug(ANY)
16:20:32.424|METHOD_ENTRY|[6]|System.debug(ANY)
16:20:32.424|USER_DEBUG|[6]|DEBUG|---------ic.Occupied_Status__c---- :null
16:20:32.424|METHOD_EXIT|[6]|System.debug(ANY)
16:20:32.424|CUMULATIVE_LIMIT_USAGE
16:20:32.424|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 20
  Number of query rows: 0 out of 1000
  Number of SOSL queries: 0 out of 0
  Number of DML statements: 1 out of 20
  Number of DML rows: 1 out of 100
  Number of script statements: 7 out of 10200
  Maximum heap size: 0 out of 3000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10
  Number of find similar calls: 0 out of 0
  Number of System.runAs() invocations: 0 out of 20

16:20:32.424|CUMULATIVE_LIMIT_USAGE_END

16:20:32.424|CODE_UNIT_FINISHED|OccupancyStatus on Inspection_Checklist trigger event BeforeUpdate for [a06T0000000HJxZ]
16:20:32.445|WF_ACTIONS_END| Field Update: 5;
16:20:32.445|CODE_UNIT_FINISHED|Workflow:01IT00000000BgJ
16:20:32.476|CODE_UNIT_STARTED|[EXTERNAL]|01qA00000006Quk|ConvertAssets on Property_Evaluation trigger event BeforeUpdate for [a0AT0000000ACcD]
16:20:32.476|CUMULATIVE_LIMIT_USAGE
16:20:32.476|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 20
  Number of query rows: 0 out of 1000
  Number of SOSL queries: 0 out of 0
  Number of DML statements: 1 out of 20
  Number of DML rows: 1 out of 100
  Number of script statements: 10 out of 10200
  Maximum heap size: 0 out of 3000000
  Number of callouts: 0 out of 10
  Number of Email Invocations: 0 out of 10
  Number of fields describes: 0 out of 100
  Number of record type describes: 0 out of 100
  Number of child relationships describes: 0 out of 100
  Number of picklist describes: 0 out of 100
  Number of future calls: 0 out of 10
  Number of find similar calls: 0 out of 0
  Number of System.runAs() invocations: 0 out of 20

16:20:32.476|CUMULATIVE_LIMIT_USAGE_END

 

 

DharmeshDharmesh

try this out. made some change. it should work now

trigger OccupancyStatus on Inspection_Checklist__c (before update) {									

	 List peList = new List();  
        for(Inspection_Checklist__c  ic : [select id,Inspection_Complete__c ,Occupied_Status__c,Property_Evaluation__r.id from Inspection_Checklist__c where      id in :Trigger.newMap.keySet()]){
          if(ic.Inspection_Complete__c && ic.Property_Evaluation__r!=null && ic.Occupied_Status__c !=null){
			  peList.add(new Property_Evaluation__c(id=ic.Property_Evaluation__r.id,Occupied_Status__c =ic.Occupied_Status__c,));
		  }  
        }
        update peList;
}
edrickwongedrickwong

 

unexpected token: 'List' at line 3 column 5

 

I can feel that we're almost there :)

 

DharmeshDharmesh

here you go

 

 

trigger OccupancyStatus on Inspection_Checklist__c (before update) {
   List<Property_Evaluation__c> peList = new List<Property_Evaluation__c>();

         for(Inspection_Checklist__c  ic : [select id,Inspection_Complete__c ,Occupied_Status__c,Property_Evaluation__r.id from Inspection_Checklist__c where                                                                      id in :Trigger.newMap.keySet()])

        {

           if(ic.Inspection_Complete__c &amp;&amp; ic.Property_Evaluation__r!=null &amp;&amp; ic.Occupied_Status__c !=null){  

peList.add(new Property_Evaluation__c(id=ic.Property_Evaluation__r.id,Occupied_Status__c =ic.Occupied_Status__c,));

            }          

        }        

       update peList;

}

This was selected as the best answer
edrickwongedrickwong

Thanks a million!!!

 

the only change I made was (after update), but you did all the hard work.

 

Thanks again Dharmesh.

edrickwongedrickwong

Hey Dharmesh,

 

I wanted to try and write the test method myself before I asked for help, but here's what I got so far, do you have any suggestions?

 

Thanks again.

 

 

@isTest
private class OccupancyStatusTest {

    static testMethod void runOccupancyStatusTestCases() {
    	
        //Declare a list to store the evaluations
	  List<Inspection_Checklist__c> ic = new List<Inspection_Checklist__c>();
	  for (Integer i=0; i < 20; i++){
	        Inspection_Checklist__c a = new Inspection_Checklist__c
	        (ic.Name =i + ' Test Street');
        	(ic.Inspection_Complete__c = TRUE);
        ic.add(a);	
        }
        try{
        	Test.starttest();
        	System.debug('');
        	
        	insert ic;
        	System.debug('');
        	
        	Map<ID,Inspection_Checklist__c> icMap = new Map<ID,Inspection_Checklist__c>(ics);
        	
        	ics = [select id,Inspection_Complete__c ,Occupied_Status__c,Property_Evaluation__r.id from Inspection_Checklist__c where                                                                      
         id in :Trigger.newMap.keySet()];
         
         for(Inspection_Checklist__c ic=ics){
         	
         	System.assertNotEquals(ic.id,null);
         	   System.assertEquals(ic.Occupied_Status__c, 'Occupied');
         }
    
}

 

I'm getting an error on line 26: Duplicate variable: ic

 

DharmeshDharmesh

if you see the trigger.

in test following things should be covered and your coverage should be more than75%

 

1) create both custom objects with set of fields

2) update one custom object so that it update second one depending the field value.