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
Dchris222Dchris222 

Query / Insert to a Reference Field/Lookup Field.

How do you querry a reference/lookup field and then read what that ID is and based on that ID, populate another reference/lookup field. I know what the values of both Id's are I am just having trouble getting it to insert and read correctly.

 

This is the code I am having problems with.

 

 

Equip2 = [SELECT Name, Modality__c, Modality_Name__c, CT_Models__c, CT_Modality_Picklist__c FROM Equipment__c WHERE Modality__c != NULL ORDER BY Name ASC];  

 

for(Equipment__c Eqp : Equip2)    {
        
            
                if(Eqp.Modality__c =  'a0a30000004XQiKAAW') {          // If lookup field modality = this id, then insert below
                
                      Eqp.Modality_Name__c = 'a0a30000004XQiKAAW';    //Populate the other lookup field
                      Eqp.CT_Modality_Picklist__c = 'CT Unknown';    // Specify Picklist Value
                      Eqp.CT_Models__c = 'Unknown';     // Specify Picklist Value
                }

             ....

             .....

             ....

               else  {
                    
                      system.debug('No modalities match');
                         
                }                       
        }    
           insert Equip2;

             ....

             .....

             ....

 

Ritesh AswaneyRitesh Aswaney

I wouldn't really recommending hard-coding a salesforce id - couldn't you match on the name of the Modality or something - something which is more likely to remain consistent across environments. 


What's the exact error / problem you're encountering?

Dchris222Dchris222

The name of the Modality is a lookup field which when you query it you you only recieve the id. Our organization is starting from scratch with a new Modalities section called "CT Modalities", but we want to maintain the connections from the old section called "Modalities". We  therefore are migrating all the old section's field information to the new sections fields. We have a full sandbox populated with our organizations data that we are using to create and test the apex code.

 

Ritesh AswaneyRitesh Aswaney

You could use the Salesforce default Name field, if the Modality_Name__c is a lookup. Anyways, what's the problem you're facing?

Dchris222Dchris222

Here is my code. Currently I am recieving "Expression cannot be assigned" on line 0.

 

I was under the impression that when you querry lookup fields you don't get the name you get the id. Am I incorrect?

 

 

global class EquipModCleanup {

    List<Equipment__c>Equip2;
 
    public void Equipment1() {
      	
      	Equip2 = [SELECT Name, Modality__c, Modality_Name__c, CT_Models__c, CT_Modality_Picklist__c FROM Equipment__c WHERE Modality__c != NULL ORDER BY Name ASC];   
      	
	
        for(Equipment__c Eqp : Equip2){
        	
            	//CT Unknown
            	
            	if(Modality__c.Id = 'a0B300000036Ka1EAE') {    //Current Modality Lookup Field
            	
            	      Eqp.Modality_Name__c = 'a0a30000004XQiKAAW';  //New "CT Modality" Lookup field 
            	      Eqp.CT_Modality_Picklist__c = 'CT Unknown';
            	      Eqp.CT_Models__c = 'Unknown'; 	 
            	}
            	
            	//Elscint 2 Slice CT
            	 else if(Modality__c.Id = 'a0B300000035vweEAA') {
            	 	 
            	      Eqp.Modality_Name__c = 'a0a30000004XNgpAAG';  
            	      Eqp.CT_Modality_Picklist__c = 'Elscint 2 Slice CT';
            	      Eqp.CT_Models__c = 'Twin Duo';
            	 }
            	 
            	 else if(Modality__c.Id = 'a0B300000036KVjEAM') {
            	 	
            	 	  Eqp.Modality_Name__c = 'a0a30000004XNgpAAG';  
            	      Eqp.CT_Modality_Picklist__c = 'Elscint 2 Slice CT';
            	      Eqp.CT_Models__c = 'Twin Flash Duo';
            	 }
            	 
            	 else if(Modality__c.Id = 'a0B300000036KXOEA2') {
            	 	
            	      Eqp.Modality_Name__c = 'a0a30000004XNgpAAG';  
            	      Eqp.CT_Modality_Picklist__c = 'Elscint 2 Slice CT';
            	      Eqp.CT_Models__c = 'Twin Helicat Duo';
            	 }
            	 
            	 else if(Modality__c.Id = 'a0B300000036KZREA2') {
            	 	
            	      Eqp.Modality_Name__c = 'a0a30000004XNgpAAG';  
            	      Eqp.CT_Modality_Picklist__c = 'Elscint 2 Slice CT';
            	      Eqp.CT_Models__c = 'Twin/RTS';
            	 }
            	 
            	//Elscint Single Slice CT
            	 else if(Modality__c.Id = 'a0B300000035qb7EAA') {  
            	 	 
                      Eqp.Modality_Name__c = 'a0a30000004XNgkAAG';
                      Eqp.CT_Modality_Picklist__c = 'Elscint Single Slice CT';
            	      Eqp.CT_Models__c = 'Select ST';          
                  }
                    
                //Elscint Unknown CT
                
                else if(Modality__c.Id = 'a0B300000036LHMEA2') {
                	
                      Eqp.Modality_Name__c = 'a0a30000004XNgfAAG';
                      Eqp.CT_Modality_Picklist__c = 'Elscint Unknown CT';
            	      Eqp.CT_Models__c = 'Unknown';        	
                }
                
                //GE 16 Slice CT
                
                else if(Modality__c.Id = 'a0B300000035oAKEAY') {
                	
                      Eqp.Modality_Name__c = 'a0a30000004XNfmAAG';
                      Eqp.CT_Modality_Picklist__c = 'GE 16 Slice CT';
            	      Eqp.CT_Models__c = 'Brightspeed 16';        	
                }
                
                else if(Modality__c.Id = 'a0B300000035psnEAA') {
                	
                      Eqp.Modality_Name__c = 'a0a30000004XNfmAAG';
                      Eqp.CT_Modality_Picklist__c = 'GE 16 Slice CT';
            	      Eqp.CT_Models__c = 'LightSpeed 16';        	
                }
                
                else if(Modality__c.Id = 'a0B300000035oAKEAY') {
                	
                      Eqp.Modality_Name__c = 'a0a30000004XNfmAAG';
                      Eqp.CT_Modality_Picklist__c = 'GE 16 Slice CT';
            	      Eqp.CT_Models__c = 'Brightspeed 16';        	
                }
                 
                  
                 else  {
            		
            		  system.debug('No modalities match');
            		 	 
            	}                 	  
        }    
           update Equip2;
  }   
}