0

I created a lightning component and added to the Case Page Layout, this lightning component when clicked should create a Contract and associate with the Case. The lightning component is like below

<aura:component implements="force:hasRecordId" access="global"  controller="createContractLightningComp">
    <aura:attribute name="createContract" type="Case" default="{'sobjectType':'Case'}"/>
    <lightning:button variant="brand" label="Create Contract" onclick="{!c.createContract}"  />      
   <lightning:spinner aura:id="mySpinner" alternativeText="Processing.." title="Processing.." variant="brand" size="large" class="slds-hide"/> 
</aura:component>

And the helper class is like

({
    createContract_helper : function(c,e,h) {
    var spinner = c.find("mySpinner");
    $A.util.removeClass(spinner, "slds-hide");
    var set_action1 = c.get("c.createContract");
    set_action1.setParams({caseId: c.get('v.recordId')});
    set_action1.setCallback(this, function(result)
    {
        var returnData = result.getReturnValue();
        if (result.getState() === 'SUCCESS')
            {
                $A.util.addClass(spinner, "slds-hide");
                var toastEvent = $A.get("e.force:showToast");
                toastEvent.setParams({
                    message: 'The Contract has been created!' ,
                    type: 'success',
                    mode: 'pester'
                });
                toastEvent.fire(); 
            }                 
            else if(result.getState() === 'ERROR')
            {
                $A.util.addClass(spinner, "slds-hide");
                var errors = result.getError();
                if (errors) {
                    if (errors[0] && errors[0].message) {
                        $A.get("e.force:showToast")
                        .setParams({
                            type: 'error',
                            mode: 'pester',
                            message: errors[0].message }).fire();                       
                    }}}
        $A.get("e.force:refreshView").fire();
    });
    $A.enqueueAction(set_action1);       
},})

Also the Apex Class is like

public class createContractLightningComp {
    @AuraEnabled
    public static String createContract(String caseId){
        
        List<Case> case_recs = [SELECT id,Contract__c,Category_Contracts__c,AccountId, ContactId from Case WHERE Id = :caseId];

        String recordType;
        String msg;        
        Boolean CDA_Two_Way;
        
       for (Case rec : case_recs)
        {
            try{      
                if(rec.Category_Contracts__c=='SOW'){
                        recordType='XXXXXXXXXXXX'; 
                    }
                    else if(rec.Category_Contracts__c=='T&C')
                    {
                        recordType='YYYYYYYYYYYYYYYY'; 
                    } 
                    
                    Contract cc;
                    cc = new Contract(AccountId = rec.AccountId,Contact__c = rec.ContactId,RecordtypeId =recordType, Status = 'Pending');
                    insert cc;                         
                    
                    if (cc.id != null)
                    {
                        System.debug('@@Inserted ID '+ cc.id);
                        rec.Contract__c = cc.id;
                        update rec;                        
                    }
                    
                    msg = ' Success The Contract has been created';
                    return msg;                   
            }
            catch(DmlException e) 
            {
                for (Integer i = 0; i < e.getNumDml(); i++) {
                    msg =+ e.getDmlMessage(i) +  '\n' ; 
                    System.debug('getDmlFieldNames=' + e.getDmlFieldNames(i));
                    System.debug('getDmlMessage=' + e.getDmlMessage(i));  
                }
                throw new AuraHandledException(msg);
            }
            catch(Exception e)
            {
                throw new AuraHandledException(e.getMessage());
            }
            finally { }      
        }
        return msg;
    }
}

But whenever the lightning component button is clicked it just puts the spinner on the page and I get the Sorry to Interrupt error

enter image description here

I tried to debug just the Apex Class everything working as expected though. Can anyone please help me with this issue.

  • Can you please add the technical stuff from the error popup and also check the console log if there is something additional helpful error message? – sanket kumar Jul 7 '20 at 3:31
  • @sanketkumar I couldnt expand the Technical Stuff. But when I tried to put the debugger and tried to click the button on F12 mode I see that the helper.js is executed again and again – user81642 Jul 7 '20 at 3:36
  • Can you also add the controller of the aura component? It's interesting to see helper executing again and again – sanket kumar Jul 7 '20 at 3:37
4

Your JS controller method and apex controller method name is same createContract. That's why its going in an infinite loop.

E.g. from controller its going to helper, from helper again controller and again helper...

  • 1
    Thank you so much, just realized that – user81642 Jul 7 '20 at 4:01

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.