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
Jessica Clark 12Jessica Clark 12 

Inserting Record with Apex controller

Having one heck of a time trying to figure out why this isn't working. The array I have returns null, which I find really strange. Additionally, the related contact is not being created, but everywhere I look for information shows this as the correct way to insert a record. Am I missing something? Thanks in advance!
@AuraEnabled
    public static string[] insertRelatedContact(string oppId, string agentId ){
        string[] infoArray = new string[] {oppId,agentId};
            
        //INSERT RELATED CONTACT
        Related_Contact__c rc = new Related_Contact__c();
        rc.OpportunityId__c = oppId;
        rc.ContactId__c = agentId;
        rc.Role__c = 'Assigned Agent';
        
        insert rc;
        
        return infoArray;
    }

 
Best Answer chosen by Jessica Clark 12
Jessica Clark 12Jessica Clark 12
Apparently my organization had a custom rule which was catching an error. Thanks for your help!

All Answers

Steven NsubugaSteven Nsubuga
Use the Developer Console to track what happens. It is possible that oppId and agentId are either null or empty strings.
Jessica Clark 12Jessica Clark 12
Thanks, Steven. I'll look again. Unfortunately I'm pretty certain that there are strings passing through because I stepped through my javascript in the inspect console and they're there.
Jessica Clark 12Jessica Clark 12
Here's a screenshot of the two strings passing through:
User-added image
And here is a screenshot of the return value returning null:
User-added image
Steven NsubugaSteven Nsubuga
I suspect you are not calling the action properly. 
Could you post the javascript controller lhere, as well as any other relevant code.
Jessica Clark 12Jessica Clark 12
Javascript Controller (excerp)
var actionInsert = component.get("c.insertRelatedContact");
            actionInsert.setParams({"oppId":oppId,"agentId":agentId});
            actionInsert.setCallback(this,function(result){
                //ARRAY PASSED INFO
                var stringArray1 = result.getReturnValue();
                console.log(stringArray1);
                
                window.setTimeout(
                $A.getCallback( function() {}));
            });
            $A.enqueueAction(actionInsert);

Javascript Controller (bigger picture)
var array = [oppId,refSource,agentSource,refContact,priceRange,intCity,intState,intZip,areaInterest,agentNotes,description,mortNotes,propStreet,propCity,propState,propZip,propAddr,mortStatus,mortType,preQualBool,preQual,sendCU,reasonNoCU,agentId];
        console.log(array);
        
        var actionSet = component.get("c.updateOpportunity");
        actionSet.setParams({
            "oppId":oppId,
            "refSource":refSource,
            "agentSource":agentSource,
            "refContact":refContact,
            "priceRange":priceRange,
            "areaInterest":areaInterest,
            "agentNotes":agentNotes,
            "description":description,
            "mortNotes":mortNotes,
            "propAddr":propAddr,
            "propStreet":propStreet,
            "propCity":propCity,
            "mortStatus":mortStatus,
            "mortType":mortType,
            "preQualBool":preQualBool,
            "preQual":preQual,
            "sendCU":sendCU,
            "reasonNoCU":reasonNoCU,
            "agentId":agentId
        });
        actionSet.setCallback(this, function(result){
            //ARRAY PASSED INFO
            var stringArray = result.getReturnValue();
            console.log(stringArray);
            var oppId = stringArray[18];
            var agentId = stringArray[17];
            
            var actionInsert = component.get("c.insertRelatedContact");
            actionInsert.setParams({"oppId":oppId,"agentId":agentId});
            actionInsert.setCallback(this,function(result){
                //ARRAY PASSED INFO
                var stringArray1 = result.getReturnValue();
                console.log(stringArray1);
                
                window.setTimeout(
                $A.getCallback( function() {}));
            });
            $A.enqueueAction(actionInsert);
            
            window.setTimeout(
                $A.getCallback( function() {}));
        });
        $A.enqueueAction(actionSet);

 
Steven NsubugaSteven Nsubuga
I cannot see anything off.  Check to see if there are any DML errors in the logs. 
Jessica Clark 12Jessica Clark 12
Apparently my organization had a custom rule which was catching an error. Thanks for your help!
This was selected as the best answer