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
Adam PietraszekAdam Pietraszek 

Set picklist value on in custom field on opportunity page with custom buttons on Lightning Component

I've taken over our org from the previous dev and even though I'm not a developer (I'm the admin) I need to fix this issue. I have limited dev experience but I figured I'll give this a shot.
We are using lightning components for our opportunities. We also have a few custom fields on the page layout as well in addition to the lightning component. 
The lightning component has a custom button that changes the stages of the opportunity. 
I would like to add a line of code to that button that will also change the value of one of the custom picklists. This picklist does not exist in the lightning component - it's just a custom field on the layout itself. 
The button click action is on the ComponentHelper.js so I'm adding the line there but I'm having issues.
The custom field is Demo_Audit_Status__c so I thought the line of code would be:
component.set("Demo_Audit_Status__c", "No Demo Needed");
I'm pretty sure I'm oversimplifying what I need but I'd rather fix this before having to reach out to the original devs.
Best Answer chosen by Adam Pietraszek
Santosh Reddy MaddhuriSantosh Reddy Maddhuri
Hi Adam, 

With the given information , i am assuming below piece of code is used to set the field values.If this is the method that called inside button Then add the following line of code.That might do the trick.

FROM:
confirmRequestAndSave: function(component, event, helper) { 
        var cmpTarget = component.find("requestConfirm");
       	component.set("v.demoForm.Request_Status__c", "Pending"); 
        $A.util.addClass(cmpTarget, "vis");
        component.set("v.sendNext", true);        
        component.set("v.requestConfirmed", true);   
        helper.checkData(component, event, helper);
    }

TO:
confirmRequestAndSave: function(component, event, helper) { 
        var cmpTarget = component.find("requestConfirm");
       	component.set("v.demoForm.Request_Status__c", "Pending"); 
       	component.set("v.demoForm.Demo_Audit_Status__c", "No Demo Needed"); // Added this line
        $A.util.addClass(cmpTarget, "vis");
        component.set("v.sendNext", true);        
        component.set("v.requestConfirmed", true);   
        helper.checkData(component, event, helper);
    }



Hope this helps!

Regards,
Santosh.

All Answers

Santosh Reddy MaddhuriSantosh Reddy Maddhuri
Hi Adam,

Please share your controller code to get more insight.That woould be helpful in resolving your issue.

 
Adam PietraszekAdam Pietraszek
Hi thanks for the response. I'm pasting the entire controller code below b/c I'm not sure which part of it is actually necessary. There are 3 buttons on the form and the one I need is the one that is labeled "No Demo Needed."
 
({

	doInit : function(component, event, helper) {
  		helper.buildPage(component, event, helper);
	},

	checkAddressForm : function(component, event, helper) {	
		helper.checkAddressVisibility(component);
	},

	refreshView : function(component, event, helper) {
        if (event.getParam("oppId") == component.get("v.oppId") ){
            helper.buildPage(component, event, helper);
        }
    },

    refreshComp : function(component, event, helper) {
            helper.buildPage(component, event, helper);
    },

    hideRequestModal: function(component, event, helper) {      
        helper.hideRequestModal(component);
    },

    confirmRequestAndSave: function(component, event, helper) { 
        var cmpTarget = component.find("requestConfirm");
       	component.set("v.demoForm.Request_Status__c", "Pending"); 
        $A.util.addClass(cmpTarget, "vis");
        component.set("v.sendNext", true);        
        component.set("v.requestConfirmed", true);   
        helper.checkData(component, event, helper);
    },

	showParticipantModal : function(component, event, helper) {		
		helper.showParticipantModal(component);
	},

	hideParticipantModal : function(component, event, helper) {		
		helper.hideParticipantModal(component);
	},

	showDemoContactModal : function(component, event, helper) {		
		helper.showDemoContactModal(component);
	},

	hideDemoContactModal: function(component, event, helper) {		
		helper.hideDemoContactModal(component);
	},

	showDemoConfigModal : function(component, event, helper) {		
		helper.showDemoConfigModal(component);
	},

	hideDemoConfigModal: function(component, event, helper) {		
		helper.hideDemoConfigModal(component);
	},

    openModal: function(component, event, helper) {
      // for Display Modal,set the "isOpen" attribute to "true"
      component.set("v.isOpen", true);
   },
 
   closeModal: function(component, event, helper) {
      // for Hide/Close Modal,set the "isOpen" attribute to "Fasle"  
      component.set("v.isOpen", false);
   },
 
   likenClose: function(component, event, helper) {
      // Display alert message on the click on the "Like and Close" button from Modal Footer 
      // and set set the "isOpen" attribute to "False for close the modal Box.
      alert('thanks for like Us :)');
      component.set("v.isOpen", false);
   },
    
	changeConfig: function(component, event, helper) {	
		//var configId = component.get("v.demoCrateConfig.Demo_Crate_Config__c"); 
		var configId = component.find("configLookup").get("v.selectedRecord.Id");
	 	if (configId != ''){
		    var action = component.get("c.getConfigList");
		    action.setParams({ "configId": configId });   
		    action.setCallback(this, function(response) {
		        var state = response.getState();
		        if (component.isValid() && state === "SUCCESS") {	
		        	var answer = JSON.parse( response.getReturnValue() );
					component.set("v.demoForm.Demo_Crate_Config__c", answer.crateConfig.Id ); 
		        	component.set("v.demoConfig", answer.crateConfig);     

		       		component.set("v.crateItems", answer.crateItems );
		       		helper.hideDemoConfigModal(component);
		        }
		        else {
	  				var errors = action.getError();
	                if (errors) {
	                    if (errors[0] && errors[0].message) {
	                        console.log("ERROR: ", errors[0].message);
	                    }
	                }
		            console.log("Failed with state: " + state);
		            helper.showError(component);
		        }
		    });
		    $A.enqueueAction(action);
		}
		else {
			component.find("democonfigLookup").set("v.errors", [{message:"Please, choose crate config "}]);
		}
	},

	clickSaveDemoRequest : function(component, event, helper) {	
		helper.checkData(component, event, helper);
	},

	clickSaveAndNextDemoRequest : function(component, event, helper) {
	    component.set("v.sendNext", true); 
		helper.checkData(component, event, helper);
	},

	clickSkipDemo : function(component, event, helper) {
        helper.skipDemo(component, event, helper);	    		
	},

	changeDemoContact : function(component, event, helper) {	
	    var demoContact = component.get("v.newDemoContact.Demo_Contact__c");  	    
	    if(demoContact != ''){
		    var action = component.get("c.getContact");
		    action.setParams({ "contId":  demoContact});  
		    action.setCallback(this, function(response) {
		        var state = response.getState();
		        if (component.isValid() && state === "SUCCESS") {	
		        	component.set("v.DemoContact", response.getReturnValue() ); 
		       		helper.hideDemoContactModal(component);
                    component.set("v.isOpen", true);
		        }
		        else {
		            console.log("Failed with state: " + state);
		            helper.showError(component);
		        }
		    });
		    $A.enqueueAction(action);
		}
		else {
			component.set("v.DemoContact.Name", 'NONE'); 
			component.set("v.DemoContact.Id", ""); 
			helper.hideDemoContactModal(component);
		}
	},

	addParticipantContact : function(component, event, helper) {	
	    //var contId = component.get("v.newParticipant.Contact__c");
	    var contId = component.find("customLookupParticipant").get("v.selectedRecord.Id");
	    if ( contId != undefined && contId != ''){
		    var action = component.get("c.getContact");
		    action.setParams({ "contId": contId });   
		    action.setCallback(this, function(response) {
		        var state = response.getState();
		        if (component.isValid() && state === "SUCCESS") {	  
		        	var newphys = response.getReturnValue();     		       		
				    var physlist = component.get("v.participantingPhysicians"); 
					var result = 0;
					for (var i = 0; i < physlist.length; i++) { 
					  if (physlist[i].Id === newphys.Id) { 
					    result = 1;
					    break;
					  } 
					}
				    if (result == 0) {
					physlist.push(newphys);
					}
					component.set("v.participantingPhysicians", physlist);					
					helper.hideParticipantModal(component);
					component.find("customLookupParticipant").clear();
		        }
		        else {
		            console.log("Failed with state: " + state);
		            helper.showError(component);
		        }
		    });
		    $A.enqueueAction(action);
		}
	},

	removeParticipantContact : function(component, event, helper) {
		var physlist = component.get("v.participantingPhysicians"); 
		var selectedItem = event.currentTarget;
        var contId = selectedItem.dataset.record;
		var len = physlist.length;
    	for (var i = 0; i < len; i++) { 
    		console.log('Array '+physlist +'index '+ i );
				  if (physlist[i].Id === contId) { 
				  	console.log('Splicing! -- ');
				    physlist.splice(i,1);				    
				    break;
				  } 
		} 		
		component.set("v.participantingPhysicians", physlist);  
	},

})

 
Santosh Reddy MaddhuriSantosh Reddy Maddhuri
Hi Adam, 

With the given information , i am assuming below piece of code is used to set the field values.If this is the method that called inside button Then add the following line of code.That might do the trick.

FROM:
confirmRequestAndSave: function(component, event, helper) { 
        var cmpTarget = component.find("requestConfirm");
       	component.set("v.demoForm.Request_Status__c", "Pending"); 
        $A.util.addClass(cmpTarget, "vis");
        component.set("v.sendNext", true);        
        component.set("v.requestConfirmed", true);   
        helper.checkData(component, event, helper);
    }

TO:
confirmRequestAndSave: function(component, event, helper) { 
        var cmpTarget = component.find("requestConfirm");
       	component.set("v.demoForm.Request_Status__c", "Pending"); 
       	component.set("v.demoForm.Demo_Audit_Status__c", "No Demo Needed"); // Added this line
        $A.util.addClass(cmpTarget, "vis");
        component.set("v.sendNext", true);        
        component.set("v.requestConfirmed", true);   
        helper.checkData(component, event, helper);
    }



Hope this helps!

Regards,
Santosh.

This was selected as the best answer
Adam PietraszekAdam Pietraszek
Santosh, that's actually close to what I'm looking for. You helped me find where the code goes but I need to make a clarification.
This is the button I'm actually looking for:
clickSkipDemo : function(component, event, helper) {
		component.set("v.demoForm.Demo_Audit_Status__c", "No Demo Needed"); // This did not work.    
		console.log("this line"); // <-- I inserted this to test if the button is correct
		helper.skipDemo(component, event, helper);	    		
	},

However the code you gave me doens't actually alter the field. 
I just want to make sure I'm being clear. The object I'm working in is Opportunity. The lightning component is also displayed on the Opportunity layout. And the Demo Audit Status is a custom field on the Opportunity object and layout, NOT in the lightning component.
Adam PietraszekAdam Pietraszek
ok I figured out the second part, here's the code:
clickSkipDemo : function(component, event, helper) {
        component.set("v.opp.Demo_Audit_Status__c", "No Demo Needed");
        helper.skipDemo(component, event, helper);	    		
	},

works as I expect it to. thanks everyone.
Santosh Reddy MaddhuriSantosh Reddy Maddhuri

Hi Adam,

If its helpful, please mark my answer as the best answer. That way someone else with similar issue can benefit and i can get few points too.Glad was able to help you fix your issue.


Regards,

Santosh