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
Akhil Katkam 5Akhil Katkam 5 

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
User-added image


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
 

Best Answer chosen by Akhil Katkam 5
Suraj Tripathi 47Suraj Tripathi 47
Hi Akhil, 

Yes you need to change your apex code because you have passed Competition__c object record id in Aura component check this:-
User-added image

And Inside the apex code you have get the records of competitor__c object and compare the id of  Competition__c object check this:-
User-added image

So you will change apex code and check this code:-
@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<Competition__c> cmptList=[select id,Name  from Competition__c where id=:recId];
        for(Competition__c comp : cmptList){
           o.MainCompetitors__c = comp.Name; 
        }
       update o; 
        system.debug('opp' +o);

    }

 

All Answers

Suraj Tripathi 47Suraj Tripathi 47
Hi akhil,

Apex code:-
You can take reference from this below code.
@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){
           o.MainCompetitors__c = comp.Name; 
        }
       update o; 
        system.debug('opp' +o);

    }
In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer.
 
Akhil Katkam 5Akhil Katkam 5
Hi Suraj,
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
Suraj Tripathi 47Suraj Tripathi 47
Hi Akhil

Please remove this alert line [alert("From server: " + response.getReturnValue());] Because fetchCompetitions function return void value so response.getReturnValue() return null.
User-added image
Akhil Katkam 5Akhil Katkam 5
Hi Suraj , thanks for reply , what should i write if i remove that , as it is in if statement for state == success 

Thanks in Advance
Suraj Tripathi 47Suraj Tripathi 47
Hi Akhil,
You can write Alert(' opportunity update successfully!!'); instead of the Alert("From server: " + response.getReturnValue());
Akhil Katkam 5Akhil Katkam 5
Hi suraj , there is no change , value is not updating , do we need to take any parent to child or child to parent query in apex?
Suraj Tripathi 47Suraj Tripathi 47
Hi Akhil, Please add debug [system.debug('opp--> '+o); system.debug('cmptList--> '+cmptList);] in apex controller. And check what is the value in both the debug. And then tell me the values in both the debug.
Akhil Katkam 5Akhil Katkam 5
User-added image


hi Suraj , please check  here 4th row is cmptlist() 5th row is opp


 
Akhil Katkam 5Akhil Katkam 5
Hi suraj , 

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 
Suraj Tripathi 47Suraj Tripathi 47
Hi Akhil, 

Yes you need to change your apex code because you have passed Competition__c object record id in Aura component check this:-
User-added image

And Inside the apex code you have get the records of competitor__c object and compare the id of  Competition__c object check this:-
User-added image

So you will change apex code and check this code:-
@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<Competition__c> cmptList=[select id,Name  from Competition__c where id=:recId];
        for(Competition__c comp : cmptList){
           o.MainCompetitors__c = comp.Name; 
        }
       update o; 
        system.debug('opp' +o);

    }

 
This was selected as the best answer
Akhil Katkam 5Akhil Katkam 5
Hi suraj , thank you very much , yes its the mistake i have done , now its cleared , thanks for ur help