Usually this kind of error we will be receiving in test classes.This is due to when your working with any kind of relationship fields in objects.Let me explain with an example below
Error:
Root Cause:
Error:
We have a two objects called Order__c and Time__c and these two objects are having a lookup relationship where Order__c is Child and Time__c is a parent.Now i have to create a test record for Time__c along with Order__c and then I want to update Time__c record Order__c with null value.
@isTest private class TestTimeEntry { static testMethod void validateTime() { Order__c ord = new Order__c(); ord.Name ='Test Order'; insert ord; Time__c tm1 = new Time__c(); tm1.Order__c = ord.id; tm1.name = 'test time'; insert tm1; Time__c tm2 =[Select id,name,Order__c from Time__c where id=:tm1.id]; tm2.Order__c =''; //At runtime we will receive Error here update tm2; } }
Root Cause:
So when you run this test class you will receive a runtime error called System.StringException: Invalid id at an update statement due to assigning an empty string(empty space) to Order__c.Usually all relationship fields will accepts Ids or null key word.
Work Around Solution
So when your trying to assign an empty value for relationship field we have to specify null keyword instead of an empty space('')
Thanks for visiting...Enjoy!
Work Around Solution
So when your trying to assign an empty value for relationship field we have to specify null keyword instead of an empty space('')
@isTest private class TestTimeEntry { static testMethod void validateTime() { Order__c ord = new Order__c(); ord.Name ='Test Order'; insert ord; Time__c tm1 = new Time__c(); tm1.Order__c = ord.id; tm1.name = 'test time'; insert tm1; Time__c tm2 =[Select id,name,Order__c from Time__c where id=:tm1.id]; tm2.Order__c =null; //An empty space replaced with null keyword update tm2; } }
Thanks for visiting...Enjoy!
No comments:
Post a Comment