You need to sign in to do that
Don't have an account?
apex function method having errors
Hi Developer Community ,
I have been working on a task in my project , i will share the ocde of component , js and apex function .
My scenario is , i have a standard object :OPPORTUNITY and
Custom object: competition__c
in my opportunity there is a custom field called “Main_Competitor” (text field ) and in my competition there is a field called “Competitor” (look up field)
i have created a custom aura component and displayed in opportunity record page
in this above image u can see custom field : Main Competitor in details area,
on the right side u can see PRIMARY COMPETITION look up field (i have created a custom look up field )
in this primary competition , if i click on search , then it will show the list of Competitions ,if i select one of the competition name from them and click on set as primary , then this value is to be set a sprimary competition and in this particular selected competition i need to take value of competitor and assign that value to Main competitor in oppoertunity
i have already created all the necesaary requirements , but value is not updating , i think error is in apex code , i will share the code , plz check and let me know how to solve this
Competition.cmp: <aura:component controller="oppcompquery" implements=" ......."> <aura:attribute name="selectedId" type="String" /> <aura:attribute name="competitionlist" type="String" /> <c:lookupField objectAPIName="Competition__c" label="PRIMARY COMPETITION" returnFields="['Name']" queryFields="['Name']" selectedId="{!v.selectedId}" /> <lightning:button label="set as primary" onclick="{!c.request1}" variant="brand" name="name2" class="slds-m-right_small"/> controller.JS: request1: function(component, event, helper) { var recId = component.get("v.selectedId"); alert('hi'); var action = component.get("c.fetchCompetitions"); action.setParams({ "recordId": component.get("v.recordId"), "recId": recId }); // Create a callback that is executed after action.setCallback(this, function(response) { var state = response.getState(); if (state === "SUCCESS") { // Alert the user with the value returned // from the server alert("From server: " + response.getReturnValue()); // You would typically fire a event here to trigger // client-side notification that the server-side // action is complete } else if (state === "INCOMPLETE") { // do something } else if (state === "ERROR") { var errors = response.getError(); if (errors) { if (errors[0] && errors[0].message) { console.log("Error message: " + errors[0].message); } } else { console.log("Unknown error"); } } }); $A.enqueueAction(action); } apex class: @AuraEnabled public static void fetchCompetitions(String recordId, String recId){ System.debug('hi'); system.debug('recordId' +recordId); system.debug('recId' +recId); List<Opportunity> opp=new List<Opportunity>(); Opportunity o = [select id,Name,MainCompetitors__c from Opportunity where id=:recordId]; List<Competitor__c> cmptList=[select id,Name from Competitor__c where id=:recId]; for(Competitor__c comp : cmptList){ Opportunity opList = new Opportunity(); opList.MainCompetitors__c = comp.Name; opp.add(opList); } update opp; system.debug('opp' +opp); }
please help me with solution
Yes you need to change your apex code because you have passed Competition__c object record id in Aura component check this:-
And Inside the apex code you have get the records of competitor__c object and compare the id of Competition__c object check this:-
So you will change apex code and check this code:-
All Answers
Apex code:-
You can take reference from this below code. In case you find any other issue please mention.
If you find your Solution then mark this as the best answer.
Thanks for the reply , there is no change , but there is one pop up is displaying (displaying even before i asked in developer community)
in pop up box : ((From server: null))
can u please check my code even more clearly
Thanks in Advance
Please remove this alert line [alert("From server: " + response.getReturnValue());] Because fetchCompetitions function return void value so response.getReturnValue() return null.
Thanks in Advance
You can write Alert(' opportunity update successfully!!'); instead of the Alert("From server: " + response.getReturnValue());
hi Suraj , please check here 4th row is cmptlist() 5th row is opp
i need to get value from competition__c object (in this competition object there is field competitor__c this value need to be set to main_competitor which is in opportunity , so i think apex code need to change
Yes you need to change your apex code because you have passed Competition__c object record id in Aura component check this:-
And Inside the apex code you have get the records of competitor__c object and compare the id of Competition__c object check this:-
So you will change apex code and check this code:-