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 error



This is the first trigger I'm trying to write, and ofcourse I retrieve an error:


System.StringException: Invalid id: 5911


To explain. I managed to create  a lookup relation between Account and an object with postalcodes.

By inserting a 4 digit postalcode into the lookupfield "Postcode__c"  three fields are automatically filled, this is working perfectly. 

This trigger needs to populate the lookup field "Postcode__c" with the value from the formula field

"Postalcode_4dig__c" (this is a left formula getting only the numbers from the shippingpostalcode) --> so that the user doesn't have to manually copy the "Postalcode_4dig__c" into the lookupfield.



// This trigger populates the field "Postcode"
trigger tgrPopulatePostcode on Account (before insert, before update) { 
for(Account a :{ 
// write custom lookupfield "Postcode" 
a.Postcode__c = a.Postalcode_4dig__c;




// test for the 'Populate Postcode' trigger
private class tstPopulatePostcode {
static testMethod void test() {
// create dummy Account
Account a = new Account(Name='TEST', Shippingstreet = 'Street 10', Shippingpostalcode = '5911 BB', Shippingcity = 'Stad', Shippingcountry = 'Nederland', Phone = '077-5401111',Type = 'Prospect', Potential_amount__c = 1000000,
Industry = 'Hospitality', Sub_Industry__c = 'Cultuur', Business_Unit__c = 'Zuid', Account_stage__c = 'Suspect');

insert a;

// get the custom "Postcode" field value
a.Postcode__c = [SELECT Postalcode_4dig__c FROM Account WHERE
Id = :a.Id LIMIT 1].Postalcode_4dig__c;

// test "Postcode" 
System.assert( a.Postcode__c == a.Postalcode_4dig__c);



What am I doing wrong?




Hi ,


The thing is that Your Postcoes is object . So when you are assigning values into it . It should have those values stored in it.


for example - if you are creating a contact and inserting a account in it, you select account by clicking the magnifing glass. and you select the account.


So same case will be aapplied here. PostalCodes should have those values which is being calculated by the formula and you are assiging to it.







I understand what your saying, but it doesn't help me in the wright direction.

I just want to populate the lookupfield "Postcode__c" with the value from "Postalcode_4dig__c" (this one is also on the account pagelayout) so that the user doesn't have to copy the number into the lookup field (and I eventually can hide this field).




I forgot to mention the tracing:


Class.tstPopulatePostcode.test: line 17, column 1


You need to set the lookup field to the value of hte ID of the records where Postalcode_4dig__c came from


The Postalcode_4dig__c is just a custom field on the accountpage (it contains a 'left formula' to retrieve the numbers out of the Dutch postalcode --> 6010 AC will be 6010) And I need this number to be copied in the lookup field. 


If your answer is still the correct one, please help me to set the lookup field to the value.





Postcode__c need to be set to the id of the record to which it is looking up.........


You are getting an invalid ID error because you are trying to put the value of postalcode4dig into the postalcode field and it is not a valid ID.


So, If the ID of the postalcode needs to be the ID of the record where the postalcode4dig came from you will need to identify the ID of that record and put it in postal code field


Thanks for your reply. But before I spend another day on the forum and try to adjust my trigger, can you look at my picture and tell me if it;s possible at all.

I only want to insert the field "Postalcode 4 dig" into the lookup field "Postcode".


When I manually insert the "Postalcode 4 dig" into the lookup field "Postcode" two fields will automatically be filled with help of a formula. So everything is working fine, except I don't want that the users have to copy the 4 digit postalcode into the lookup field.