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

Trigger Query to String

I currently am having an issue with grabbing a value from a query (result is only 1) and posting it into a field (Seperate Objects with no relation).


Here is the code I have been testing with:


for (Towers_Watson_Project__c a :
	String projtype =  a.Project_Type__c;
	String stage =  a.Stage__c;	
	String theid = projtype + '-' + stage;  
	TW_Process__c[] test = [SELECT Name, Description__c FROM TW_Process__c WHERE Name =: theid ];
	//String descrip = test[0].Description__c;
	a.TriggerTestUpdate__c = test[0].Description__c;

 If I put a string rather than '=: theid' for the WHERE clause, example 'TEST123' then it works fine. This is the first real trigger I'm trying to write and am a bit lost.


Pretty much just looking for how to convert the query result into a string, not exactly sure how to accomplish.


Any help would be greatly appreciated, thanks in advance.


Also, this is a Before Update trigger.

Edwin VijayEdwin Vijay

Your trigger is all fine. The only possibility  i can see is that the "theid" variable might not be populated properly. Try using 


String theid = 'Test123' and it should work. So, the possibilities are that the "theid" variable is not being populated with the right value or the query is not fetching any records.


Do you get any error message when this trigger is executed?


I had theid populate into a field and it was working fine. It just concatenates 2 fields that are always populated.


I can't even get it to the server, eclipse doesnt give me an error of what is wrong it just says "cannot save files right now" - been doing this since yesterday.


I also tested by hard-coding a string over 'theid' for the query and it pulls in from the table and populated fine (like I was trying to do) but

Once I add in =: theid  to the query it won't let me save.

Edwin VijayEdwin Vijay

Hmm.. pretty strange


Can you try to save the code directly into your salesforce instance rather than eclipse, and let's see if any error is thrown


It wont let me :(

Edwin VijayEdwin Vijay

Does it still say "cannot save files right now"???


Then, maybe you should raise a case with salesforce and check




I read something about possibly needing to have something enabled to do dynamic queries? Is this still prevalent? or is that something old.