- jvandeve
- NEWBIE
- 25 Points
- Member since 2013
- Senior Salesforce Consultant | Belgium User Group Leader
- 4C Consulting
-
ChatterFeed
-
1Best Answers
-
0Likes Received
-
0Likes Given
-
4Questions
-
8Replies
Need help show/hide quickaction button calling lightning component
I'm hoping on some help from one of you Lightning Guru's out there ;-)
First of all I'm mighty proud I created my first lightning component that is called by a quickaction and it works! Yeay me ;-)
But now I've got the following requirement and I don't know how to handle and move further with it.
Preferably and I think it would be a great idea for Salesforce to just make this possible OOTB, I would like to render my custom action conditionally on the page. In classic we would therefore create 2 recordtypes and page layouts, one with the button the other without and then use Worklfow rule to switch the recordtype based on some fieldcriteria right... The problem is that this switching in lightning doesn't happen very seemlessly for the user (it looks like there is sometimes a lag in updating the pageview), soo.....
I can think of 2 possible solutions (but I don't know how):
- Create a simple lightning component which I would drag on the recordpage, that doesn't render anything, but would just change the style class to show or hide of a button with a name that I can set as an attribute and a second field in which I can set the condition to render it by entering field api name conditional statement (like in a formula) or something... for example:
To give you insight, or hopefully so anybody could help me I'll post the code of my existing component that is called through a quickaction on the opportunity object:
CreateOrderFromOppty.cmp
<aura:component controller="CreateOrderFromOpptyCTRL" implements="force:lightningQuickActionWithoutHeader,force:hasRecordId"> <aura:attribute name="opportunity" type="Opportunity" /> <aura:attribute name="newOrder" type="Order" default="{ 'sobjectType': 'Order' }" /> <!-- default to empty record --> <aura:attribute name="hasErrors" type="Boolean" description="Indicate if there were failures when validating the order." /> <aura:handler name="init" value="{!this}" action="{!c.doInit}" /> <!-- Display a header with details about the opportunity --> <div class="slds-page-header" role="banner"> <p class="slds-text-heading--label">{!v.opportunity.Name}</p> <h1 class="slds-page-header__title slds-m-right--small slds-truncate slds-align-left">Create New Order</h1> </div> <!-- Display form validation errors, if any --> <aura:if isTrue="{!v.hasErrors}"> <div class="recordSaveError"> <ui:message title="Error" severity="error" closable="true"> The new order can't be saved because it's not valid. Please review and correct the errors in the form. </ui:message> </div> </aura:if> <!-- Display the new corder form --> <div class="slds-form--stacked"> <div class="slds-form-element slds-required"> <label class="slds-form-element__label" for="orderName">* Order Name: </label> <div class="slds-form-element__control"> <ui:inputText class="slds-input" aura:id="orderName" value="{!v.newOrder.Name}" required="true"/> </div> </div> <c:strike_lookup class="slds-input slds-required" aura:id="orderBillingAccount" value="{!v.newOrder.blng__BillingAccount__c}" label="Billing Account" object="Account" searchField="Name" placeholder="Select a Billing Account" iconName="standard:account" subtitleField="Industry" order="Name" limit="5" loadingMessage="Loading..." errorMessage="Invalid input" showRecentRecords="true" required="true" /> <div class="slds-form-element"> <label class="slds-form-element__label" for="orderStartDate">* Start Date: </label> <div class="slds-form-element__control"> <ui:inputDate class="slds-input" aura:id="orderStartDate" value="{!v.newOrder.EffectiveDate}" displayDatePicker="true" /> </div> </div> <div class="slds-form-element"> <label class="slds-form-element__label" for="orderPONumber">PO Number: </label> <div class="slds-form-element__control"> <ui:inputText class="slds-input" aura:id="orderPONumber" value="{!v.newOrder.PoNumber}" /> </div> </div> <div class="slds-form-element"> <label class="slds-form-element__label" for="orderPODate">PO Date: </label> <div class="slds-form-element__control"> <ui:inputDate class="slds-input" aura:id="orderPODate" value="{!v.newOrder.PoDate}" displayDatePicker="true" /> </div> </div> <div class="slds-form-element"> <label class="slds-form-element__label" for="orderOpportunity">Shipping Street: </label> <div class="slds-form-element__control"> <ui:inputText class="slds-input" aura:id="orderOpportunity" value="{!v.newOrder.ShippingStreet}" /> </div> </div> <div class="slds-form-element"> <label class="slds-form-element__label" for="orderOpportunity">Shipping PostalCode: </label> <div class="slds-form-element__control"> <ui:inputText class="slds-input" aura:id="orderOpportunity" value="{!v.newOrder.ShippingPostalCode}" /> </div> </div> <div class="slds-form-element"> <label class="slds-form-element__label" for="orderOpportunity">Shipping City: </label> <div class="slds-form-element__control"> <ui:inputText class="slds-input" aura:id="orderOpportunity" value="{!v.newOrder.ShippingCity}" /> </div> </div> <div class="slds-form-element"> <label class="slds-form-element__label" for="orderOpportunity">Shipping Country: </label> <div class="slds-form-element__control"> <ui:inputText class="slds-input" aura:id="orderOpportunity" value="{!v.newOrder.ShippingCountry}" /> </div> </div> <div class="slds-form-element"> <ui:button label="Cancel" press="{!c.handleCancel}" class="slds-button slds-button--neutral" /> <ui:button label="Save Order" press="{!c.handleSaveOrder}" class="slds-button slds-button--brand" /> </div> </div> </aura:component>
it's controller:
({ doInit : function(component, event, helper) { // Prepare the action to load opportunity record var action = component.get("c.getOpportunity"); action.setParams({"opportunityId": component.get("v.recordId")}); // Configure response handler action.setCallback(this, function(response) { var state = response.getState(); if(component.isValid() && state === "SUCCESS") { var opportunity = response.getReturnValue(); component.set("v.opportunity", opportunity); var tempOrder = component.get("v.newOrder"); tempOrder.blng__BillingAccount__c = opportunity.AccountId; tempOrder.ShippingStreet = opportunity.Account.ShippingStreet; tempOrder.ShippingPostalCode = opportunity.Account.ShippingPostalCode; tempOrder.ShippingCity = opportunity.Account.ShippingCity; tempOrder.ShippingCountry = opportunity.Account.ShippingCountry; component.set("v.newOrder", tempOrder); } else { console.log('Problem getting opportunity, response state: ' + state); } }); $A.enqueueAction(action); }, handleSaveOrder: function(component, event, helper) { if(helper.validateOrderForm(component)) { component.set("v.hasErrors", false); // Prepare the action to create the new order var saveOrderAction = component.get("c.saveOrder"); saveOrderAction.setParams({ "order": component.get("v.newOrder"), "opportunityId": component.get("v.recordId"), "opportunity": component.get("v.opportunity") }); // Configure the response handler for the action saveOrderAction.setCallback(this, function(response) { var state = response.getState(); if(component.isValid() && state === "SUCCESS") { // Prepare a toast UI message var resultsToast = $A.get("e.force:showToast"); resultsToast.setParams({ "title": "Order Saved", "message": "The new order was created." }); // Update the UI: close panel, show toast, refresh account page $A.get("e.force:closeQuickAction").fire(); resultsToast.fire(); $A.get("e.force:refreshView").fire(); } else if (state === "ERROR") { console.log('Problem saving order, response state: ' + state); } else { console.log('Unknown problem, response state: ' + state); } }); // Send the request to create the new order $A.enqueueAction(saveOrderAction); } else { // New order form failed validation, show a message to review errors component.set("v.hasErrors", true); } }, handleCancel: function(component, event, helper) { $A.get("e.force:closeQuickAction").fire(); } })
it's Helper:
({ validateOrderForm: function(component) { var validOrder = true; // First and Last Name are required var orderNameField = component.find("orderName"); if($A.util.isEmpty(orderNameField.get("v.value"))) { validOrder = false; orderNameField.set("v.errors", [{message:"Order Name can't be blank"}]); } else { orderNameField.set("v.errors", null); } /* var orderAccountField = component.find("orderAccount"); if($A.util.isEmpty(orderAccountField.get("v.value"))) { validOrder = false; orderAccountField.set("v.errors", [{message:"Account can't be blank"}]); } else { orderAccountField.set("v.errors", null); } */ // Verify we have an opportunity to attach it to var opportunity = component.get("v.opportunity"); if($A.util.isEmpty(opportunity)) { validOrder = false; console.log("Quick action context doesn't have a valid opportunity."); } // TODO: (Maybe) Validate extra fields return(validOrder); } })
It uses other components for lookupfields, but that is not really relevant ;-)
Thanks for any advise or help!!
- jvandeve
- July 27, 2017
- Like
- 0
Newly created recordid not passed to URL? Help
I've got a question. I've got a Visualforce page and controller that have to create or a Business Account and Contact OR a PersonAccount from a custom object populated with fields of that custom object.
It does create the correct records with correct recordtypes, but the redirect after it should just go to the newly created account, but it says Null and an error message is displayed that the URL no longer exists.
Weirdly enough in sidebar I can see the newly created records and when I click on them I get their record detailpage. Weird huh.
Can someone tell me what I'm doing wrong or any advise to better do the code? I have to tell I'm not a spectacular coder ;-)
The VF Page:
<apex:page controller="createAccountOrContactController"> <apex:form id="theForm" > <apex:pageBlock title="Creating a Business Account and related Contact" rendered="{!createBA}"> <apex:pageBlockButtons > <apex:commandButton action="{!confirmBA}" value="Confirm"/> <apex:commandButton action="{!cancel}" value="Cancel"/> </apex:pageBlockButtons> <apex:pagemessages ></apex:pagemessages> <apex:pageBlockSection title="Business Account Details" columns="2"> <apex:outputField value="{!acct.Name}"/> <apex:outputField value="{!acct.C_Street__c}"/> <apex:outputField value="{!acct.C_City__c}"/> <apex:outputField value="{!acct.C_Zipcode__c}"/> <apex:outputField value="{!acct.C_Country__c}"/> <apex:outputField value="{!acct.Ondernemingsnummer__c}" /> <apex:outputField value="{!acct.BTW_Plichtig__c}" /> <apex:outputField value="{!acct.VAT__c}" /> <apex:outputField value="{!acct.Phone}" /> <apex:outputField value="{!acct.Email__c}" /> </apex:pageBlockSection> <apex:pageBlockSection title="Contact Details" columns="2"> <apex:outputField value="{!con.LastName}"/> <apex:outputField value="{!con.FirstName}"/> <apex:outputField value="{!con.Street__c}"/> <apex:outputField value="{!con.C_City__c}"/> <apex:outputField value="{!con.C_Zipcode__c}"/> <apex:outputField value="{!con.C_Country__c}"/> <apex:outputField value="{!con.Phone}"/> <apex:outputField value="{!con.MobilePhone}"/> <apex:outputField value="{!con.Email}"/> <apex:outputField value="{!con.HasOptedOutOfEmail}"/> </apex:pageBlockSection> </apex:pageBlock> <apex:pageBlock title="Creating a Person Account" rendered="{!createPA}"> <apex:pageBlockButtons > <apex:commandButton action="{!confirmPA}" value="Confirm"/> <apex:commandButton action="{!cancel}" value="Cancel"/> </apex:pageBlockButtons> <apex:pagemessages ></apex:pagemessages> <apex:pageBlockSection title="Person Account Details" columns="2"> <apex:outputField value="{!pacct.Street__pc}"/> <apex:outputField value="{!pacct.C_City__pc}"/> <apex:outputField value="{!pacct.C_Zipcode__pc}"/> <apex:outputField value="{!pacct.C_Country__pc}"/> <apex:outputField value="{!pacct.Email__c}"/> <apex:outputField value="{!pacct.LastName2__c}"/> <apex:outputField value="{!pacct.FirstName__c}"/> <apex:outputField value="{!pacct.Language__c}"/> <apex:outputField value="{!pacct.Phone}"/> <apex:outputField value="{!pacct.MobilePhone__c}"/> <apex:outputField value="{!pacct.PersonEmail}"/> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>The Controller:
public class createAccountOrContactController { public Id idStr = ApexPages.currentPage().getParameters().get('addressId'); public Address__c theAddress = [Select Id, Name, Name__c, First_Name__c, Last_Name__c,Language_Code__c, Data_Privacy__c, VIP__c, Private_E_Mail__c, Mail_Stop__c, Mobile_Phone_No__c,Home_Phone_No__c, Address__c, City__c, Phone_No__c, E_Mail__c, Vat__c, BTW_Plichtig__c, Enterprise_No__c, Post_Code__c, Postcode__c, Country__c, Customer_Type__c from Address__c where Id = :idStr LIMIT 1]; public User currentuser = [Select Id, Name, Email, Profile.Name from User where Id =:userinfo.getuserId()]; //public String recordType {get; set;} public Boolean createBA {get; set;} public Boolean createPA {get; set;} public createAccountOrContactController(){ if(theAddress.Customer_Type__c == 'Business'){ createBA = true; createPA = false; }else{ createPA = true; createBA = false; } } public String acctRecordType{ get { if(currentuser.Profile.Name == 'Sales Rep Vriesdonk' && theAddress.Customer_Type__c == 'Business'){ acctRecordType = '012250000004Rkg'; }else if(currentuser.Profile.Name == 'Sales Rep Swinnen' && theAddress.Customer_Type__c == 'Business'){ acctRecordType = '012250000004RkC'; }else if(currentuser.Profile.Name == 'Sales Rep Euromotors' && theAddress.Customer_Type__c == 'Business'){ acctRecordType = '012250000004Rkb'; }else if(currentuser.Profile.Name == 'Sales Rep Joosen' && theAddress.Customer_Type__c == 'Business'){ acctRecordType = '012250000004Rkl'; }else if(currentuser.Profile.Name == 'Sales Rep Vriesdonk' && theAddress.Customer_Type__c != 'Business'){ acctRecordType = '012250000004RkR'; }else if(currentuser.Profile.Name == 'Sales Rep Swinnen' && theAddress.Customer_Type__c != 'Business'){ acctRecordType = '012250000004RkH'; }else if(currentuser.Profile.Name == 'Sales Rep Euromotors' && theAddress.Customer_Type__c != 'Business'){ acctRecordType = '012250000004RkW'; }else if(currentuser.Profile.Name == 'Sales Rep Joosen' && theAddress.Customer_Type__c != 'Business'){ acctRecordType = '012250000004RkM'; }else if(theAddress.Customer_Type__c == 'Business'){ acctRecordType = '01224000000bQJa'; }else{ acctRecordType = '01224000000bQsc'; } return acctRecordType; } set; } public Account acct{ get { acct = new Account(); acct.Name = theAddress.Name__c; acct.RecordTypeId = acctRecordType; acct.C_Street__c = theAddress.Address__c; acct.C_City__c = theAddress.City__c; acct.C_Zipcode__c = theAddress.Post_Code__c; acct.C_Country__c = theAddress.Country__c; acct.Address__c = theAddress.Id; acct.Ondernemingsnummer__c = theAddress.Enterprise_No__c; acct.Vat__c = theAddress.Vat__c; acct.BTW_Plichtig__c = theAddress.BTW_Plichtig__c; acct.Phone = theAddress.Phone_No__c; acct.Email__c = theAddress.E_Mail__c; acct.LastName2__c = theAddress.Last_Name__c; acct.FirstName__c = theAddress.First_Name__c; acct.Language__c = theAddress.Language_Code__c; acct.MobilePhone__c = theAddress.Mobile_Phone_No__c; return acct; } set; } public Account pacct{ get { pacct = new Account(); pacct.RecordTypeId = acctRecordType; pacct.C_Street__c = theAddress.Address__c; pacct.C_City__c = theAddress.City__c; pacct.C_Zipcode__c = theAddress.Post_Code__c; pacct.C_Country__c = theAddress.Country__c; pacct.Street__pc = theAddress.Address__c; pacct.C_City__pc = theAddress.City__c; pacct.C_Zipcode__pc = theAddress.Post_Code__c; pacct.C_Country__pc = theAddress.Country__c; pacct.Postcode_lk__pc = theAddress.Postcode__c; pacct.Address__c = theAddress.Id; pacct.Phone = theAddress.Phone_No__c; pacct.Email__c = theAddress.E_Mail__c; pacct.LastName = theAddress.Last_Name__c; pacct.LastName2__c = theAddress.Last_Name__c; pacct.FirstName = theAddress.First_Name__c; pacct.FirstName__c = theAddress.First_Name__c; pacct.Language__c = theAddress.Language_Code__c; pacct.MobilePhone__c = theAddress.Mobile_Phone_No__c; pacct.PersonEmail = theAddress.Private_E_mail__c; return pacct; } set; } public Contact con{ get { if (con == null) con = new Contact(); con.LastName = theAddress.Last_Name__c; con.FirstName = theAddress.First_Name__c; con.Street__c = theAddress.Address__c; con.C_City__c = theAddress.City__c; con.C_Zipcode__c = theAddress.Post_Code__c; con.C_Country__c = theAddress.Country__c; con.Address_Contact__c = theAddress.Id; con.Language__c = theAddress.Language_Code__c; con.VIP__c = theAddress.VIP__c; con.DataPrivacy__c = theAddress.Data_Privacy__c; con.Phone = theAddress.Home_Phone_No__c; con.MobilePhone = theAddress.Mobile_Phone_No__c; con.Email = theAddress.Private_E_Mail__c; con.HasOptedOutOfEmail = theAddress.Mail_Stop__c; return con; } set; } public PageReference confirmBA(){ insert acct; con.AccountId = acct.Id; insert con; PageReference success = new PageReference('/' + acct.Id); success.setRedirect(true); return success; //return new PageReference('/' + acct.Id); } public PageReference confirmPA(){ insert pacct; PageReference success = new PageReference('/' + pacct.Id); success.setRedirect(true); return success; //return new PageReference('/' + pacct.id); } public PageReference cancel(){ return new PageReference('/' + theAddress.Id); } }
When I hit confirm whether is does the createPA or createBA method, I get the following error for URL, but you can see the records are created.
You can see in URL after slash / there is written null, but why? Oh why?!
- jvandeve
- July 09, 2015
- Like
- 0
VisualForce page and custom controller work but don't save values to database
Hi all,
I'm a Salesforce admin and not a developer, so please be gentle! My situation is as follows. I want a specific profile to be able to modify some specific fields on users, but without giving them access to setup!
Created a VFpage using a custom controller. This is the custom controller:
public class alfabet { public PageReference quicksave() { return null; } private List<userSet> userSetList{get;set;} private string userListQuery; private set<user> selectedUser; public List<string> alphabet{get;set;} public boolean fals{get;set;} public alfabet(){ fals=false; alphabet=new string[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'All' }; userSetList = new List<userSet>(); selectedUser = new set<user>(); userListQuery= 'select id,name,Regio__c,Regio_User__c,Profile.Name,FTE__c,Scorecard_Activity__c,Scorecard_Sales_Category__c,Scorecard_Sector_Region__c from user where IsActive=True limit 1000'; } Private void updateSelectedUser(){ for(userSet cs:userSetList ){ if(cs.checked) selecteduser.add(cs.ouser); else{ if(selecteduser.contains(cs.ouser)) selecteduser.remove(cs.ouser); } } } public ApexPages.StandardSetController standardSetCon { get { if(standardSetCon == null) { standardSetCon = new ApexPages.StandardSetController(Database.getQueryLocator(userListQuery)); // sets the number of records in each page set standardSetCon .setPageSize(25); } return standardSetCon ; } set; } public List<userSet> getCurrentList() { updateSelectedUser(); userSetList = new List<userSet>(); for (user category : (List<user>)standardSetCon.getRecords()){ if(selectedUser.contains(category)) userSetList.add(new userSet(category,'true')); else userSetList.add(new userSet(category)); } fals=false; return userSetList; } public PageReference refreshList2() { standardSetCon = null; string s; if(apexpages.currentpage().getparameters().get('alpha') == 'All') s='%'; else s= apexpages.currentpage().getparameters().get('alpha')+'%'; userListQuery= 'select id,name,Regio__c,Regio_User__c,Profile.Name,FTE__c,Scorecard_Activity__c,Scorecard_Sales_Category__c,Scorecard_Sector_Region__c from user where IsActive=true AND name like' +'\''+s +'\''+ ' limit 5000'; return null; } public List<user> getDisplaySelectedList(){ updateSelectedUser(); List<user> displaycon = new list<user>(); displaycon.addall(selecteduser); return displaycon; } public class userSet { public Boolean checked{ get; set; } public user ouser { get; set;} public userSet(){ ouser = new user(); checked = false; } public userSet(user c){ ouser = c; checked = false; } public userSet(user c,string s){ ouser = c; checked = true; } } }
The VF page shows filterlinks above the page a b c d e f g.... to show users beginning with that letter. The records are preceded by checkboxes. When a check a couple of users and I click a button "Display Selected", the selected users are shown in a second block with a couple of updatable picklistfields from the user object. When I change the values of the different entries and picklists and then hit "Update Users", I would like the fields on the userrecord to be updated, but it doen't update them. And I've been searching for hours now and can't find the solution.
This is the code of the Visual Force page:
<apex:page Controller="alfabet" tabstyle="User"> <style type="text/css"> .loadingIcon { background-image: url(/img/loading.gif); width: 16px; height: 16px; } </style> <script type="text/javascript"> function checkAll(cb,cbid) { var inputElem = document.getElementsByTagName("input"); for(var i=0; i<inputElem.length; i++) { if(inputElem[i].id.indexOf(cbid)!=-1){ inputElem[i].checked = cb.checked; } } } </script> <apex:form id="form"> <br/> <div style="align:right;"> <apex:repeat value="{!alphabet}" var="a"> <apex:commandLink value="{!a}" action="{!refreshList2}" rerender="form" style="{!if($CurrentPage.parameters.alpha=a,'font-weight:bold','')}" status="stat"> <apex:param name="alpha" value="{!a}"/> </apex:commandLink> | </apex:repeat> </div> <br/> <apex:pageBlock id="block"> <apex:pageBlockButtons > <apex:commandButton rendered="{!standardsetcon.hasprevious}" value="Previous" action="{!standardsetcon.previous}" rerender="block,block2" status="stat"/> <apex:commandButton rendered="{!standardsetcon.hasnext}" value="Next" action="{!standardsetcon.next}" rerender="block,block2" status="stat"/> </apex:pageBlockButtons> <apex:actionStatus id="stat"> <apex:facet name="start"> <apex:outputPanel layout="block" styleClass="message infoM4"> <apex:panelGrid columns="2" styleClass="messageTable" columnClasses="messageCell" style="padding:0px;margin:0px;"> <apex:panelGroup > <img class="loadingIcon" src="/s.gif"/> </apex:panelGroup> <apex:panelGroup > <div class="messageText">Please wait...</div> </apex:panelGroup> </apex:panelGrid> </apex:outputPanel> </apex:facet> <apex:facet name="stop"> <apex:pageBlockTable value="{!CurrentList}" var="c" id="table"> <apex:column > <apex:facet name="header"><apex:inputcheckbox onclick="checkAll(this,'check')" value="{!fals}" /></apex:facet> <apex:inputcheckbox value="{!c.checked}" id="check"> </apex:inputcheckbox> </apex:column> <apex:column value="{!c.ouser.name}" headerValue="Name"/> <apex:column value="{!c.ouser.Profile.Name}" headerValue="Profile"/> <apex:column value="{!c.ouser.Regio__c}" headerValue="Region"/> <apex:column value="{!c.ouser.Regio_User__c}" headerValue="Region User"/> </apex:pageBlockTable> </apex:facet> </apex:actionStatus> </apex:pageBlock> <apex:pageBlock title="Selected Users" id="block2"> <apex:commandButton value="Display Selected" rerender="block2"/> <apex:pageBlockTable value="{!DisplaySelectedList}" var="c"> <apex:column value="{!c.name}" headerValue="Name"/> <apex:column headerValue="FTE"> <apex:inputField value="{!c.FTE__c}"> </apex:inputfield> </apex:column> <apex:column headerValue="Activity"> <apex:inputField value="{!c.Scorecard_Activity__c}"> </apex:inputfield> </apex:column> <apex:column headerValue="Sales Category"> <apex:inputField value="{!c.Scorecard_Sales_Category__c}"> </apex:inputfield> </apex:column> <apex:column headerValue="Sector / Region"> <apex:inputField value="{!c.Scorecard_Sector_Region__c}"> </apex:inputfield> </apex:column> </apex:pageBlockTable> <apex:commandButton action="{!quicksave}" value="Update Users" /> </apex:pageBlock> </apex:form> </apex:page>
Till here everything works fine and it does exactly what it has to do, but it doesn't write the changes back to the database! HELP
I would really appreciate someone to write down the exact code (I guess in the controller) to be adapted or added to get the button to save those values in their respective records.
kindly appreciated!
- jvandeve
- December 03, 2013
- Like
- 0
Need help with trigger to autopopulate lookup field on oppty
Hi guys,
I never wrote a trigger myself but I need one now, so I searched the internet and have found what I want but for some reason it doesn't work for me. I hope you can help me with this.
Here is the situation:
New custom Object: XtendedUser__c
This object is linked with a lookupfield to a SF-user by the field Linked_to_User__c
On the opportunity i created a new lookupfield called XtendedUser__c wich has to link to a record of the custom object XtendedUser__c where the opportunityownerid = the id of the user in Linked_to_User__c of the custom object.
This is the code so far:
trigger PopulateXtendedUser on Opportunity (before update, before insert) {
//Set of XtendedUser Ids
Set<Id> XtendedUserIds = new Set<Id>();
for (Opportunity opportunityNew : Trigger.new) {
XtendedUserIds.add(opportunityNew.XtendedUser__c); //A set of XtendedUsers
}
Map<Id, XtendedUser__c> pXtendedUsers = new Map<Id, XtendedUser__c>([SELECT a.id, a.linked_to_User__c, a.Name FROM XtendedUser__c a WHERE ownerid IN :XtendedUserIds]);
for(Opportunity fcon : Trigger.New){
system.debug('Going to update OpptyXtendedUserlookupfield');
if(pXtendedUsers != null && pXtendedUsers.get(fcon.XtendedUser__c) != null)
fcon.XtendedUser__c = pXtendedUsers.get(fcon.XtendedUser__c).Id;
System.debug('OpptyXtendedUserfield should be updated');
}
}
The code saves so no error, but the field on Oppty doesn't autopopulate.
Can someone help me a bit please?
- jvandeve
- November 29, 2013
- Like
- 0
Newly created recordid not passed to URL? Help
I've got a question. I've got a Visualforce page and controller that have to create or a Business Account and Contact OR a PersonAccount from a custom object populated with fields of that custom object.
It does create the correct records with correct recordtypes, but the redirect after it should just go to the newly created account, but it says Null and an error message is displayed that the URL no longer exists.
Weirdly enough in sidebar I can see the newly created records and when I click on them I get their record detailpage. Weird huh.
Can someone tell me what I'm doing wrong or any advise to better do the code? I have to tell I'm not a spectacular coder ;-)
The VF Page:
<apex:page controller="createAccountOrContactController"> <apex:form id="theForm" > <apex:pageBlock title="Creating a Business Account and related Contact" rendered="{!createBA}"> <apex:pageBlockButtons > <apex:commandButton action="{!confirmBA}" value="Confirm"/> <apex:commandButton action="{!cancel}" value="Cancel"/> </apex:pageBlockButtons> <apex:pagemessages ></apex:pagemessages> <apex:pageBlockSection title="Business Account Details" columns="2"> <apex:outputField value="{!acct.Name}"/> <apex:outputField value="{!acct.C_Street__c}"/> <apex:outputField value="{!acct.C_City__c}"/> <apex:outputField value="{!acct.C_Zipcode__c}"/> <apex:outputField value="{!acct.C_Country__c}"/> <apex:outputField value="{!acct.Ondernemingsnummer__c}" /> <apex:outputField value="{!acct.BTW_Plichtig__c}" /> <apex:outputField value="{!acct.VAT__c}" /> <apex:outputField value="{!acct.Phone}" /> <apex:outputField value="{!acct.Email__c}" /> </apex:pageBlockSection> <apex:pageBlockSection title="Contact Details" columns="2"> <apex:outputField value="{!con.LastName}"/> <apex:outputField value="{!con.FirstName}"/> <apex:outputField value="{!con.Street__c}"/> <apex:outputField value="{!con.C_City__c}"/> <apex:outputField value="{!con.C_Zipcode__c}"/> <apex:outputField value="{!con.C_Country__c}"/> <apex:outputField value="{!con.Phone}"/> <apex:outputField value="{!con.MobilePhone}"/> <apex:outputField value="{!con.Email}"/> <apex:outputField value="{!con.HasOptedOutOfEmail}"/> </apex:pageBlockSection> </apex:pageBlock> <apex:pageBlock title="Creating a Person Account" rendered="{!createPA}"> <apex:pageBlockButtons > <apex:commandButton action="{!confirmPA}" value="Confirm"/> <apex:commandButton action="{!cancel}" value="Cancel"/> </apex:pageBlockButtons> <apex:pagemessages ></apex:pagemessages> <apex:pageBlockSection title="Person Account Details" columns="2"> <apex:outputField value="{!pacct.Street__pc}"/> <apex:outputField value="{!pacct.C_City__pc}"/> <apex:outputField value="{!pacct.C_Zipcode__pc}"/> <apex:outputField value="{!pacct.C_Country__pc}"/> <apex:outputField value="{!pacct.Email__c}"/> <apex:outputField value="{!pacct.LastName2__c}"/> <apex:outputField value="{!pacct.FirstName__c}"/> <apex:outputField value="{!pacct.Language__c}"/> <apex:outputField value="{!pacct.Phone}"/> <apex:outputField value="{!pacct.MobilePhone__c}"/> <apex:outputField value="{!pacct.PersonEmail}"/> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>The Controller:
public class createAccountOrContactController { public Id idStr = ApexPages.currentPage().getParameters().get('addressId'); public Address__c theAddress = [Select Id, Name, Name__c, First_Name__c, Last_Name__c,Language_Code__c, Data_Privacy__c, VIP__c, Private_E_Mail__c, Mail_Stop__c, Mobile_Phone_No__c,Home_Phone_No__c, Address__c, City__c, Phone_No__c, E_Mail__c, Vat__c, BTW_Plichtig__c, Enterprise_No__c, Post_Code__c, Postcode__c, Country__c, Customer_Type__c from Address__c where Id = :idStr LIMIT 1]; public User currentuser = [Select Id, Name, Email, Profile.Name from User where Id =:userinfo.getuserId()]; //public String recordType {get; set;} public Boolean createBA {get; set;} public Boolean createPA {get; set;} public createAccountOrContactController(){ if(theAddress.Customer_Type__c == 'Business'){ createBA = true; createPA = false; }else{ createPA = true; createBA = false; } } public String acctRecordType{ get { if(currentuser.Profile.Name == 'Sales Rep Vriesdonk' && theAddress.Customer_Type__c == 'Business'){ acctRecordType = '012250000004Rkg'; }else if(currentuser.Profile.Name == 'Sales Rep Swinnen' && theAddress.Customer_Type__c == 'Business'){ acctRecordType = '012250000004RkC'; }else if(currentuser.Profile.Name == 'Sales Rep Euromotors' && theAddress.Customer_Type__c == 'Business'){ acctRecordType = '012250000004Rkb'; }else if(currentuser.Profile.Name == 'Sales Rep Joosen' && theAddress.Customer_Type__c == 'Business'){ acctRecordType = '012250000004Rkl'; }else if(currentuser.Profile.Name == 'Sales Rep Vriesdonk' && theAddress.Customer_Type__c != 'Business'){ acctRecordType = '012250000004RkR'; }else if(currentuser.Profile.Name == 'Sales Rep Swinnen' && theAddress.Customer_Type__c != 'Business'){ acctRecordType = '012250000004RkH'; }else if(currentuser.Profile.Name == 'Sales Rep Euromotors' && theAddress.Customer_Type__c != 'Business'){ acctRecordType = '012250000004RkW'; }else if(currentuser.Profile.Name == 'Sales Rep Joosen' && theAddress.Customer_Type__c != 'Business'){ acctRecordType = '012250000004RkM'; }else if(theAddress.Customer_Type__c == 'Business'){ acctRecordType = '01224000000bQJa'; }else{ acctRecordType = '01224000000bQsc'; } return acctRecordType; } set; } public Account acct{ get { acct = new Account(); acct.Name = theAddress.Name__c; acct.RecordTypeId = acctRecordType; acct.C_Street__c = theAddress.Address__c; acct.C_City__c = theAddress.City__c; acct.C_Zipcode__c = theAddress.Post_Code__c; acct.C_Country__c = theAddress.Country__c; acct.Address__c = theAddress.Id; acct.Ondernemingsnummer__c = theAddress.Enterprise_No__c; acct.Vat__c = theAddress.Vat__c; acct.BTW_Plichtig__c = theAddress.BTW_Plichtig__c; acct.Phone = theAddress.Phone_No__c; acct.Email__c = theAddress.E_Mail__c; acct.LastName2__c = theAddress.Last_Name__c; acct.FirstName__c = theAddress.First_Name__c; acct.Language__c = theAddress.Language_Code__c; acct.MobilePhone__c = theAddress.Mobile_Phone_No__c; return acct; } set; } public Account pacct{ get { pacct = new Account(); pacct.RecordTypeId = acctRecordType; pacct.C_Street__c = theAddress.Address__c; pacct.C_City__c = theAddress.City__c; pacct.C_Zipcode__c = theAddress.Post_Code__c; pacct.C_Country__c = theAddress.Country__c; pacct.Street__pc = theAddress.Address__c; pacct.C_City__pc = theAddress.City__c; pacct.C_Zipcode__pc = theAddress.Post_Code__c; pacct.C_Country__pc = theAddress.Country__c; pacct.Postcode_lk__pc = theAddress.Postcode__c; pacct.Address__c = theAddress.Id; pacct.Phone = theAddress.Phone_No__c; pacct.Email__c = theAddress.E_Mail__c; pacct.LastName = theAddress.Last_Name__c; pacct.LastName2__c = theAddress.Last_Name__c; pacct.FirstName = theAddress.First_Name__c; pacct.FirstName__c = theAddress.First_Name__c; pacct.Language__c = theAddress.Language_Code__c; pacct.MobilePhone__c = theAddress.Mobile_Phone_No__c; pacct.PersonEmail = theAddress.Private_E_mail__c; return pacct; } set; } public Contact con{ get { if (con == null) con = new Contact(); con.LastName = theAddress.Last_Name__c; con.FirstName = theAddress.First_Name__c; con.Street__c = theAddress.Address__c; con.C_City__c = theAddress.City__c; con.C_Zipcode__c = theAddress.Post_Code__c; con.C_Country__c = theAddress.Country__c; con.Address_Contact__c = theAddress.Id; con.Language__c = theAddress.Language_Code__c; con.VIP__c = theAddress.VIP__c; con.DataPrivacy__c = theAddress.Data_Privacy__c; con.Phone = theAddress.Home_Phone_No__c; con.MobilePhone = theAddress.Mobile_Phone_No__c; con.Email = theAddress.Private_E_Mail__c; con.HasOptedOutOfEmail = theAddress.Mail_Stop__c; return con; } set; } public PageReference confirmBA(){ insert acct; con.AccountId = acct.Id; insert con; PageReference success = new PageReference('/' + acct.Id); success.setRedirect(true); return success; //return new PageReference('/' + acct.Id); } public PageReference confirmPA(){ insert pacct; PageReference success = new PageReference('/' + pacct.Id); success.setRedirect(true); return success; //return new PageReference('/' + pacct.id); } public PageReference cancel(){ return new PageReference('/' + theAddress.Id); } }
When I hit confirm whether is does the createPA or createBA method, I get the following error for URL, but you can see the records are created.
You can see in URL after slash / there is written null, but why? Oh why?!
- jvandeve
- July 09, 2015
- Like
- 0
- Joseph Sukumar
- September 22, 2014
- Like
- 0
VisualForce page and custom controller work but don't save values to database
Hi all,
I'm a Salesforce admin and not a developer, so please be gentle! My situation is as follows. I want a specific profile to be able to modify some specific fields on users, but without giving them access to setup!
Created a VFpage using a custom controller. This is the custom controller:
public class alfabet { public PageReference quicksave() { return null; } private List<userSet> userSetList{get;set;} private string userListQuery; private set<user> selectedUser; public List<string> alphabet{get;set;} public boolean fals{get;set;} public alfabet(){ fals=false; alphabet=new string[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'All' }; userSetList = new List<userSet>(); selectedUser = new set<user>(); userListQuery= 'select id,name,Regio__c,Regio_User__c,Profile.Name,FTE__c,Scorecard_Activity__c,Scorecard_Sales_Category__c,Scorecard_Sector_Region__c from user where IsActive=True limit 1000'; } Private void updateSelectedUser(){ for(userSet cs:userSetList ){ if(cs.checked) selecteduser.add(cs.ouser); else{ if(selecteduser.contains(cs.ouser)) selecteduser.remove(cs.ouser); } } } public ApexPages.StandardSetController standardSetCon { get { if(standardSetCon == null) { standardSetCon = new ApexPages.StandardSetController(Database.getQueryLocator(userListQuery)); // sets the number of records in each page set standardSetCon .setPageSize(25); } return standardSetCon ; } set; } public List<userSet> getCurrentList() { updateSelectedUser(); userSetList = new List<userSet>(); for (user category : (List<user>)standardSetCon.getRecords()){ if(selectedUser.contains(category)) userSetList.add(new userSet(category,'true')); else userSetList.add(new userSet(category)); } fals=false; return userSetList; } public PageReference refreshList2() { standardSetCon = null; string s; if(apexpages.currentpage().getparameters().get('alpha') == 'All') s='%'; else s= apexpages.currentpage().getparameters().get('alpha')+'%'; userListQuery= 'select id,name,Regio__c,Regio_User__c,Profile.Name,FTE__c,Scorecard_Activity__c,Scorecard_Sales_Category__c,Scorecard_Sector_Region__c from user where IsActive=true AND name like' +'\''+s +'\''+ ' limit 5000'; return null; } public List<user> getDisplaySelectedList(){ updateSelectedUser(); List<user> displaycon = new list<user>(); displaycon.addall(selecteduser); return displaycon; } public class userSet { public Boolean checked{ get; set; } public user ouser { get; set;} public userSet(){ ouser = new user(); checked = false; } public userSet(user c){ ouser = c; checked = false; } public userSet(user c,string s){ ouser = c; checked = true; } } }
The VF page shows filterlinks above the page a b c d e f g.... to show users beginning with that letter. The records are preceded by checkboxes. When a check a couple of users and I click a button "Display Selected", the selected users are shown in a second block with a couple of updatable picklistfields from the user object. When I change the values of the different entries and picklists and then hit "Update Users", I would like the fields on the userrecord to be updated, but it doen't update them. And I've been searching for hours now and can't find the solution.
This is the code of the Visual Force page:
<apex:page Controller="alfabet" tabstyle="User"> <style type="text/css"> .loadingIcon { background-image: url(/img/loading.gif); width: 16px; height: 16px; } </style> <script type="text/javascript"> function checkAll(cb,cbid) { var inputElem = document.getElementsByTagName("input"); for(var i=0; i<inputElem.length; i++) { if(inputElem[i].id.indexOf(cbid)!=-1){ inputElem[i].checked = cb.checked; } } } </script> <apex:form id="form"> <br/> <div style="align:right;"> <apex:repeat value="{!alphabet}" var="a"> <apex:commandLink value="{!a}" action="{!refreshList2}" rerender="form" style="{!if($CurrentPage.parameters.alpha=a,'font-weight:bold','')}" status="stat"> <apex:param name="alpha" value="{!a}"/> </apex:commandLink> | </apex:repeat> </div> <br/> <apex:pageBlock id="block"> <apex:pageBlockButtons > <apex:commandButton rendered="{!standardsetcon.hasprevious}" value="Previous" action="{!standardsetcon.previous}" rerender="block,block2" status="stat"/> <apex:commandButton rendered="{!standardsetcon.hasnext}" value="Next" action="{!standardsetcon.next}" rerender="block,block2" status="stat"/> </apex:pageBlockButtons> <apex:actionStatus id="stat"> <apex:facet name="start"> <apex:outputPanel layout="block" styleClass="message infoM4"> <apex:panelGrid columns="2" styleClass="messageTable" columnClasses="messageCell" style="padding:0px;margin:0px;"> <apex:panelGroup > <img class="loadingIcon" src="/s.gif"/> </apex:panelGroup> <apex:panelGroup > <div class="messageText">Please wait...</div> </apex:panelGroup> </apex:panelGrid> </apex:outputPanel> </apex:facet> <apex:facet name="stop"> <apex:pageBlockTable value="{!CurrentList}" var="c" id="table"> <apex:column > <apex:facet name="header"><apex:inputcheckbox onclick="checkAll(this,'check')" value="{!fals}" /></apex:facet> <apex:inputcheckbox value="{!c.checked}" id="check"> </apex:inputcheckbox> </apex:column> <apex:column value="{!c.ouser.name}" headerValue="Name"/> <apex:column value="{!c.ouser.Profile.Name}" headerValue="Profile"/> <apex:column value="{!c.ouser.Regio__c}" headerValue="Region"/> <apex:column value="{!c.ouser.Regio_User__c}" headerValue="Region User"/> </apex:pageBlockTable> </apex:facet> </apex:actionStatus> </apex:pageBlock> <apex:pageBlock title="Selected Users" id="block2"> <apex:commandButton value="Display Selected" rerender="block2"/> <apex:pageBlockTable value="{!DisplaySelectedList}" var="c"> <apex:column value="{!c.name}" headerValue="Name"/> <apex:column headerValue="FTE"> <apex:inputField value="{!c.FTE__c}"> </apex:inputfield> </apex:column> <apex:column headerValue="Activity"> <apex:inputField value="{!c.Scorecard_Activity__c}"> </apex:inputfield> </apex:column> <apex:column headerValue="Sales Category"> <apex:inputField value="{!c.Scorecard_Sales_Category__c}"> </apex:inputfield> </apex:column> <apex:column headerValue="Sector / Region"> <apex:inputField value="{!c.Scorecard_Sector_Region__c}"> </apex:inputfield> </apex:column> </apex:pageBlockTable> <apex:commandButton action="{!quicksave}" value="Update Users" /> </apex:pageBlock> </apex:form> </apex:page>
Till here everything works fine and it does exactly what it has to do, but it doesn't write the changes back to the database! HELP
I would really appreciate someone to write down the exact code (I guess in the controller) to be adapted or added to get the button to save those values in their respective records.
kindly appreciated!
- jvandeve
- December 03, 2013
- Like
- 0
Need help with trigger to autopopulate lookup field on oppty
Hi guys,
I never wrote a trigger myself but I need one now, so I searched the internet and have found what I want but for some reason it doesn't work for me. I hope you can help me with this.
Here is the situation:
New custom Object: XtendedUser__c
This object is linked with a lookupfield to a SF-user by the field Linked_to_User__c
On the opportunity i created a new lookupfield called XtendedUser__c wich has to link to a record of the custom object XtendedUser__c where the opportunityownerid = the id of the user in Linked_to_User__c of the custom object.
This is the code so far:
trigger PopulateXtendedUser on Opportunity (before update, before insert) {
//Set of XtendedUser Ids
Set<Id> XtendedUserIds = new Set<Id>();
for (Opportunity opportunityNew : Trigger.new) {
XtendedUserIds.add(opportunityNew.XtendedUser__c); //A set of XtendedUsers
}
Map<Id, XtendedUser__c> pXtendedUsers = new Map<Id, XtendedUser__c>([SELECT a.id, a.linked_to_User__c, a.Name FROM XtendedUser__c a WHERE ownerid IN :XtendedUserIds]);
for(Opportunity fcon : Trigger.New){
system.debug('Going to update OpptyXtendedUserlookupfield');
if(pXtendedUsers != null && pXtendedUsers.get(fcon.XtendedUser__c) != null)
fcon.XtendedUser__c = pXtendedUsers.get(fcon.XtendedUser__c).Id;
System.debug('OpptyXtendedUserfield should be updated');
}
}
The code saves so no error, but the field on Oppty doesn't autopopulate.
Can someone help me a bit please?
- jvandeve
- November 29, 2013
- Like
- 0