0

I am trying to convert my existing vf page including controller to the lightning component. I am done with the component but having issue while writing @AuraEnabled method regarding the controller's PageReference method and constructor.

I am inserting opportunity on the "Save" button click in my component.

Button:{!c.saveOpportunity}"

//calling the getNewOppId method from NewOpportunityController and referring it to Save Button

    saveOpportunity : function(component,event,helper){
    var action = component.get("c.getNewOppId");
    action.setCallback(this, function(response){
        alert('Inside Callback method');
        if (state == "SUCCESS") {
            var optyId = response.getReturnValue();
            alert(optyId);
            var urlEvent = $A.get("e.force:navigateToURL");
            urlEvent.setParams({
                "url": "/006/o" + optyId
            });
            urlEvent.fire();
        }
    });
    $A.enqueueAction(action); 
}

I think my method "getNewOppId" which I tried to convert from PageRefrence to @AuraEnabled is not working.

Here is the original page reference method:

 public pageReference saveOpp(){
    try{
        Id OpptyOwnerId = [SELECT Id FROM User WHERE Id =: userInfo.getUserId() and IsActive = True LIMIT 1].Id;
        system.debug('++OpptyOwnerId+'+OpptyOwnerId);
        Id RTId = [SELECT Id FROM RecordType WHERE sObjectType ='Opportunity' LIMIT 1].Id;
        Boolean isRegional = false;
        acc= NEW account();
        acc= [select name,id,Sales_Region__c from Account where id =:oppty.Accountid];
        if(oppty.SMB_OPP_Must_Win_level__c == null)oppty.SMB_OPP_Must_Win_level__c='None';
        if(isRegional )
        oppty.SMB_Opp_Must_Win__c = 'Yes';
        else
        oppty.SMB_Opp_Must_Win__c = 'No';
        oppty.OwnerId = OpptyOwnerId;

       if(REC.name == System.Label.OAB_Opportunity_Record_Type){
           oppty.StageName = '2 Qualification';
       }else{
           oppty.StageName = '1 Pre-qualification';
       } 
         if(REC.name ==System.Label.OAB_Opportunity_Record_Type && acc.Sales_Region__c !='DEF'){ 
            system.debug('----------'+REC.name);               
            ApexPages.addmessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'With a Simple DEF record type the account related to the opportunity must be a DEF account.'));
            return null;                                      
        }

         if(REC.name ==System.Label.OAB_Opportunity_Record_Type && (oppty.Order_Type__c =='New Customer' || oppty.Order_Type__c =='New Business/GET')){ 
              oppty.Contract_Type__c= 'New';                           
        }else if(REC.name ==System.Label.OAB_Opportunity_Record_Type &&(oppty.Order_Type__c =='Renewal/KEEP' || oppty.Order_Type__c =='Organic/KEEP')){
                   oppty.Contract_Type__c= 'Existing';
        }

        insert oppty;

        //partner management evolution
        if(REC.name == System.Label.OAB_Opportunity_Record_Type){
            SMB_Partner__c insertNoPartner = new SMB_Partner__c(SMB_Opportunity__c=oppty.Id,
                                                                SMB_Name__c=Label.SMB_CL001_No_Partner_ID,
                                                               SMB_Amount__c=0,SMB_Percentage__c=0,
                                                               SMB_Primary__c=true);
            insert insertNoPartner;
        }
        //assign a contact to a new opportunity
        if(conId!=null){
            OpportunityContactRole oppContact = new OpportunityContactRole(ContactId=conId,IsPrimary=true,
                                                                           OpportunityId=oppty.Id,Role=Label.Contact_Role);
            insert oppContact;
        }
        PageReference pageRef = new PageReference('/' + oppty.Id + '?retURL=%2F' + oppty.Id);
           return pageRef;

    }
    catch(exception ex){         
       if(!ex.getMessage().contains('FIELD_CUSTOM_VALIDATION_EXCEPTION'))
       Apexpages.addMessage(new Apexpages.Message(ApexPages.severity.ERROR, 'Exception Error: ' + ex));                                 
        return null;
    }
     return null; 
}

Here is the @AuraEnabled method which I break down from above into getNewOppId and PageReference method SaveOpp() which is calling the getNewOppId().

//using Pagereference method to redirecting to the opp detail page

public PageReference saveOpp(){
    Id returnedId = getNewOppId();
    return new PageReference('/'+returnedId);
}

 // Method to get the opportunity Id
@AuraEnabled
public static Id getNewOppId(){
    try{
        Id OpptyOwnerId = [SELECT Id FROM User WHERE Id =: userInfo.getUserId() and IsActive = True LIMIT 1].Id;
        system.debug('++OpptyOwnerId+'+OpptyOwnerId);
        Id RTId = [SELECT Id FROM RecordType WHERE sObjectType ='Opportunity' LIMIT 1].Id;
        Boolean isRegional = false;
        acc= NEW account();
        acc= [select name,id,Sales_Region__c from Account where id =:oppty.Accountid];
        if(oppty.SMB_OPP_Must_Win_level__c == null)oppty.SMB_OPP_Must_Win_level__c='None';
        if(isRegional )
       oppty.SMB_Opp_Must_Win__c = 'Yes';
       else
       oppty.SMB_Opp_Must_Win__c = 'No';

        oppty.OwnerId = OpptyOwnerId;

       if(REC.name == System.Label.OAB_Opportunity_Record_Type){
       system.debug('========'+REC.name);
           oppty.StageName = '2 Qualification';
       }else{
           oppty.StageName = '1 Pre-qualification';
       }


         if(REC.name ==System.Label.OAB_Opportunity_Record_Type && acc.Sales_Region__c !='DEF'){ 
            system.debug('----------'+REC.name);               
            ApexPages.addmessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'With a Simple DEF record type the account related to the opportunity must be a DEF account.'));
            return null;                                      
        }


         if(REC.name ==System.Label.OAB_Opportunity_Record_Type && (oppty.Order_Type__c =='New Customer' || oppty.Order_Type__c =='New Business/GET')){ 
              oppty.Contract_Type__c= 'New';                           
        }else if(REC.name ==System.Label.OAB_Opportunity_Record_Type &&(oppty.Order_Type__c =='Renewal/KEEP' || oppty.Order_Type__c =='Organic/KEEP')){
                   oppty.Contract_Type__c= 'Existing';
        }

        insert oppty;

        //partner management evolution--START
        if(REC.name == System.Label.OAB_Opportunity_Record_Type){
            SMB_Partner__c insertNoPartner = new SMB_Partner__c(SMB_Opportunity__c=oppty.Id,
                                                                SMB_Name__c=Label.SMB_CL001_No_Partner_ID,
                                                               SMB_Amount__c=0,SMB_Percentage__c=0,
                                                               SMB_Primary__c=true);
            insert insertNoPartner;
        }
        //partner management evolution--END

        //assign a contact to a new opportunity
        if(conId!=null){
            OpportunityContactRole oppContact = new OpportunityContactRole(ContactId=conId,IsPrimary=true,
                                                                           OpportunityId=oppty.Id,Role=Label.Contact_Role);
            insert oppContact;
        }

          return oppty.Id;   
    }
    catch(exception ex){        
       if(!ex.getMessage().contains('FIELD_CUSTOM_VALIDATION_EXCEPTION'))
       Apexpages.addMessage(new Apexpages.Message(ApexPages.severity.ERROR, 'Exception Error: ' + ex));                                 
       return null;
    } 
  }

Getting below error when I clicked on the Save button:

    Error in $A.getCallback() ['state' is undefined]
Callback failed: apex://NewOpportunityController/ACTION$getNewOppId

Please let me know what's wrong I am doing here.

1

It says it in the error message - state is not defined.

Define state within the callback like this:

action.setCallback(this, function (response) {
  var state = response.getState();
  if (response.getState() === "SUCCESS") {
  ///ect
  • Thanks for pointing out. I added the state but now getting "internal server error". My getOpportunity() method returning state==="ERROR" instead opportunityId. – Ashutosh Apr 26 '18 at 13:03
  • @Ashutosh internal server error in this case just means there's a run-time error in your controller action. Check your dev console – Antonio Manente Apr 26 '18 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.