public with sharing class Apparel_PageController { public List<Trendsters_Order__c> orders{get;set;} public String contactId{get;set;} public Case objCase{get;set;} public Decimal totalAmount{get;set;} public Decimal cartSize{get;set;} public String currentProductId{get;set;} public List<Trendsterz_Inventory__c> inventries {get;set;} public List<Trendsters_Order__c> cart{get;set;} public Apparel_PageController(){ totalAmount = 0; cartSize = 0; cart = new List<Trendsters_Order__c>(); User objUser = [select ContactId from User where Id = :UserInfo.getUserId()]; contactId = objUser.ContactId; //contactId = '0037F00000a8HVg'; objCase = new Case(ContactId=contactId); inventries = [select Id,Brand__c, Designer_Name__c, Design_Name__c, Price__c,Image_Url__c from Trendsterz_Inventory__c LIMIT 10]; } public void fetchOrders(){ orders = [SELECT Id,Name,Brand__c,Contact__c,Contact__r.Name, Contact__r.Email, Designer_Name__c,Design_Name__c,Email__c,Price__c,Order_Status__c FROM Trendsters_Order__c WHERE Contact__c = :contactId]; } public void submitFeedback(){ insert objCase; } public void addToCart(){ for(Trendsterz_Inventory__c inventry : inventries){ if(currentProductId !=null && inventry.Id == currentProductId){ Trendsters_Order__c order = new Trendsters_Order__c(); system.debug('contactid -->'+contactId); order.Contact__c = contactId; order.Design_Name__c = inventry.Design_Name__c; order.Price__c= inventry.Price__c; cart.add(order); break; } } cartSize = cart.size(); // totalAmount = cart.size(); } public void payProduct(){ system.debug('cart -> ' + cart.size()); insert cart; } } - Error is preity clear. Contact is required and blank. Are you facing any challenge.Tushar Sharma– Tushar Sharma2018-05-12 07:58:33 +00:00Commented May 12, 2018 at 7:58
- In my system debug....it is fetching Contact ID....Saniya Virmani– Saniya Virmani2018-05-12 08:06:48 +00:00Commented May 12, 2018 at 8:06
- After sometime it showed success.....it was happening yesterday also.....it failed and then after sometime automatically it got successSaniya Virmani– Saniya Virmani2018-05-12 08:10:17 +00:00Commented May 12, 2018 at 8:10
- You are getting ContactId always.Tushar Sharma– Tushar Sharma2018-05-12 08:22:27 +00:00Commented May 12, 2018 at 8:22
- M really new on this....it's just been 1 week that I have started working on salesforce.....can u please explain...what could the error be?.....coz exactly as u said Contact Id is getting fetched...but then y Required field Missing error is coming?Saniya Virmani– Saniya Virmani2018-05-12 08:28:37 +00:00Commented May 12, 2018 at 8:28
1 Answer
One possibility to consider is that you are checking the code using different logins (users).
The User.ContactId is populated for user's that have setup their login via Self-Registration for Your Community because that mechanism starts from a Contact and hooks up a User to that Contact. But it is not populated for users that have a standard login created by a System Administrator.
If that is the case, your page should just output a message such as "Only community users can use this page". Or the page logic should be otherwise reconsidered for that case.
- Thanku so much....but it is from a registered user.Saniya Virmani– Saniya Virmani2018-05-12 10:30:07 +00:00Commented May 12, 2018 at 10:30
- @SaniyaVirmani Ah OK. One other thing to check is that there are no triggers or workflows on
Trendsters_Order__cthat are resetting the ContactId.Keith C– Keith C2018-05-12 10:32:51 +00:00Commented May 12, 2018 at 10:32 - nop....@Keith C....there are no triggers and workflowsSaniya Virmani– Saniya Virmani2018-05-12 11:04:55 +00:00Commented May 12, 2018 at 11:04
- @KeithC Hi there! I've had this issue where I get the required field missing error on a specific field on a custom object. This field was deployed as a required field but was changes to not being required after a day. However, we're still getting this error once in a while on new records being inserted. I haven't been able to find out why this is happening. Please help if you have ideas as to what may be causing this. Thanks!Bahman.A– Bahman.A2020-09-17 20:10:15 +00:00Commented Sep 17, 2020 at 20:10