+ Start a Discussion
Lee_CampbellLee_Campbell 

Expression cannot be assigned - way of updating fields with same name on two objects

Hi folks,

 

I'm trying to create an object record that populates fields that have the same API name on another object in a general way. So... when I create a record on object A, a trigger creates a new record on object B, sees which of the fields on the two objects have the same API name, then uses the field values from the record on object A and puts them in the appropriate field in object B. The code below yields the error "expression cannot be assigned" at the line that begins newEoi.get(sharedFields[i]) in the for loop inside the trigger.new loop.

 

trigger PROC_CreateEoi on Procurement__c(after insert){
	//retrieve a list of all of the fields on the Procurement and EPOI objects
	Set<String> matchProcFields = Procurement__c.sObjectType.getDescribe().fields.getMap().keySet();
	Set<String> matEoiFields = EOI__c.sObjectType.getDescribe().fields.getMap().keySet();
	
	List<String> procFieldComparison = new List<String>();
	List<String> eoiFieldComparison = new List<String>();
	
	
	//search for fields that have the same API name in both the Procurement__c object and the EOI__c object.
	
	Set<String> fieldsTemp = new Set<String>();
	for(integer i = 0; i < eoiFieldComparison.size();i++){
		for(integer j = 0;j<procFieldComparison.size();j++){
			if(procFieldComparison[j] == eoiFieldComparison[i]){
				fieldsTemp.add(eoiFieldComparison[i]);
			}
		}
	}
	List<String> sharedFields = new List<String>();
	sharedFields.addAll(fieldsTemp);
	
	for(Procurement__c newProc:trigger.new){
		EOI__c newEoi = new Milestone1_Project__c();
		for(integer i = 0;i<sharedFields.size();i++){
			//assign the variables with the same name to the same value in the newly-created EOI record
			newEoi.get(sharedFields[i]) = newProc.get(sharedFields[i]);
		}
	}
}

 

I've tried doing this with "try and catch" statements and using String.valueof() etc. to see if that would handle the error, but I still get the "Expression cannot be assigned" error. So, any ideas how I assign the field values in this general way, without having to explicitly type all of the field names?

 

Thanks in advance, wonderful people of developerforce :D

 

Lee

Best Answer chosen by Admin (Salesforce Developers) 
Sean TanSean Tan

Use the .put method instead of doing an assignment. The put method is used to generically set values on a SObject just like the get method is used to retrieve values generically.

 

trigger PROC_CreateEoi on Procurement__c(after insert){
	//retrieve a list of all of the fields on the Procurement and EPOI objects
	Set<String> matchProcFields = Procurement__c.sObjectType.getDescribe().fields.getMap().keySet();
	Set<String> matEoiFields = EOI__c.sObjectType.getDescribe().fields.getMap().keySet();
	
	List<String> procFieldComparison = new List<String>();
	List<String> eoiFieldComparison = new List<String>();
	
	
	//search for fields that have the same API name in both the Procurement__c object and the EOI__c object.
	
	Set<String> fieldsTemp = new Set<String>();
	for(integer i = 0; i < eoiFieldComparison.size();i++){
		for(integer j = 0;j<procFieldComparison.size();j++){
			if(procFieldComparison[j] == eoiFieldComparison[i]){
				fieldsTemp.add(eoiFieldComparison[i]);
			}
		}
	}
	List<String> sharedFields = new List<String>();
	sharedFields.addAll(fieldsTemp);
	
	for(Procurement__c newProc:trigger.new){
		EOI__c newEoi = new Milestone1_Project__c();
		for(integer i = 0;i<sharedFields.size();i++){
			//assign the variables with the same name to the same value in the newly-created EOI record
			newEoi.put(sharedFields[i], newProc.get(sharedFields[i]));
		}
	}
}

 

 

 

All Answers

Sean TanSean Tan

Use the .put method instead of doing an assignment. The put method is used to generically set values on a SObject just like the get method is used to retrieve values generically.

 

trigger PROC_CreateEoi on Procurement__c(after insert){
	//retrieve a list of all of the fields on the Procurement and EPOI objects
	Set<String> matchProcFields = Procurement__c.sObjectType.getDescribe().fields.getMap().keySet();
	Set<String> matEoiFields = EOI__c.sObjectType.getDescribe().fields.getMap().keySet();
	
	List<String> procFieldComparison = new List<String>();
	List<String> eoiFieldComparison = new List<String>();
	
	
	//search for fields that have the same API name in both the Procurement__c object and the EOI__c object.
	
	Set<String> fieldsTemp = new Set<String>();
	for(integer i = 0; i < eoiFieldComparison.size();i++){
		for(integer j = 0;j<procFieldComparison.size();j++){
			if(procFieldComparison[j] == eoiFieldComparison[i]){
				fieldsTemp.add(eoiFieldComparison[i]);
			}
		}
	}
	List<String> sharedFields = new List<String>();
	sharedFields.addAll(fieldsTemp);
	
	for(Procurement__c newProc:trigger.new){
		EOI__c newEoi = new Milestone1_Project__c();
		for(integer i = 0;i<sharedFields.size();i++){
			//assign the variables with the same name to the same value in the newly-created EOI record
			newEoi.put(sharedFields[i], newProc.get(sharedFields[i]));
		}
	}
}

 

 

 

This was selected as the best answer
Lee_CampbellLee_Campbell

Brilliant, thank you. Womderfully easy solution!

 

Also, would anyone happen to know if there's method or anything that gives an output that determines whether a field is custom or standard on a custom object? This would be so that I don't have to manually specify that things like SystemModStamp aren't written from one object to the other (along with other non-writeable system fields).

Sean TanSean Tan

There are a couple of ways... one is to just check if the field is appended with __c, if so then you can assume it's a custom field (the easiest and "hackiest" way). The other is to a describe call on your objects which will then let you get a list of fields and from there be able to determine if those fields are custom or standard (may be overkill but it's the most generic way of handling what you're asking).

 

See here about the describe schema details:

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_sobject_describe.htm

Lee_CampbellLee_Campbell

That's great. Thanks very much for your help.

 

Developers are such helpful people!