• Jacinta Roberson 4
  • NEWBIE
  • 0 Points
  • Member since 2018

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 10
    Replies
Hi All,

Can you help us with a test class? We already confirmed the apex class works. Do you see anything wrong with the test class?

We are using the class in a LWC and we confirmed that it works when we test it manually. The LWC is using a wire to get the entitlementRec object data.  

The coverage test stops at around at line 9 after the json serialize lines.

The error is: System.NullPointerException: Attempt to de-reference a null object Class.AssetsQuery.getAssetsOwnedForEntList: line 9, column 1 Class.AssetsAndEntitlementsQuery_Test.assetsTest: line 52, column 1

The log says: 14:10:19.129 (3033421382)|VARIABLE_SCOPE_BEGIN|[7]|fieldmapObj|Map|true|false 14:10:19.129 (3033433056)|VARIABLE_ASSIGNMENT|

apex class
public without sharing class AssetsQuery {
@AuraEnabled(cacheable=true)
public static Asset[] getAssetsOwnedForEntList(Id recordId, Object entitlementRec) {
System.debug('In getAssetsOwnedForEntList');
    system.debug('entitlementRec'+entitlementRec);
    Map<String,Object> mapObj = (Map<String,Object>)Json.deserializeUntyped(Json.serialize(entitlementRec));
    Map<String,Object> fieldmapObj = (Map<String,Object>)Json.deserializeUntyped(Json.serialize(mapObj.get('fields')));
    system.debug('mapObj'+mapObj);
    Map<String,Object> accfieldmap = (Map<String,Object>)fieldmapObj.get('AccountId');
    Map<String,Object> prodfieldmap = (Map<String,Object>)fieldmapObj.get('Product__c');
    system.debug('value'+accfieldmap.get('value'));
    String accId = (String)accfieldmap.get('value');
    String prodId = (String)prodfieldmap.get('value');

    return [
        SELECT Id, Name, Account.Name, Engine_Model__r.Name, Product2.Name, Operator_lookup__r.Name, Status
        FROM Asset
        WHERE AccountId = :accId AND (Engine_Model__c = :prodId OR Product2Id = :prodId) AND Status <> null
        ORDER BY Name ASC
    ];
}
}

test class
@isTest(seeAllData=true)
Public class AssetsAndEntitlementsQuery_Test{

static testMethod void assetsTest(){
    Map<String,String> mprecordProduct=new Map<String,String>();
    FOR(RecordType rt:[SELECT Id, Name FROM RecordType WHERE SobjectType = 'Product2']) {
        mprecordProduct.put(rt.Name,rt.id);
    }

    Test.startTest();

    Product2 prd1 = new Product2();
    prd1.Name='CF34-10A';
    prd1.RecordTypeId=mprecordProduct.get('Engine Model');
    prd1.IsActive=true;
    Insert prd1;

    Account acc1 = new Account();
    acc1.name = 'LMNOP Airways';
    acc1.Account_Type__c  = 'CEO - Standard';
    Insert acc1;

    Entitlement ET1 = New Entitlement();
    ET1.Name='GTA LMNOP Airways';
    ET1.Comments__c='Third Parties';
    ET1.GTA_Number__c='12-12-12';
    ET1.StartDate=System.Today();
    ET1.GTA_Type__c='First Tier';
    ET1.AccountId=acc1.Id;
    ET1.Product__c=prd1.Id;
    ET1.EndDate=System.today()+365;
    Insert ET1;

    Asset as1 = New Asset();
    as1.Name='LMNOP 123';
    as1.AccountId=acc1.Id;
    as1.Operator_lookup__c=acc1.Id;
    as1.Product2Id=prd1.Id;
    as1.Engine_Model__c=prd1.Id;
    as1.Status='In Operation';
    Insert as1;

    AssetsQuery.getAssetsOwnedForEntList(ET1.Id,ET1);

    Test.stopTest();
}

}


 
Hi Everyone,

I'm using @wire to pull back a fields value and then trying to pass it in an http callout. Unfortunately, the value is coming back undefined in the debugger and I used the salesforce documentation exactly. Does anyone see what I'm missing?
import { LightningElement, track, api, wire } from 'lwc';//api, wire 

import { getRecord, getFieldValue } from 'lightning/uiRecordApi';

//import field reference from schema
import SERIAL_FIELD from '@salesforce/schema/Asset.Serial_Test__c';

const field = [SERIAL_FIELD];

export default class TestCallout extends LightningElement {
  
    @track toDoData;
    @api recordId;
    @wire(getRecord, { recordId: '$recordId', field})
    asset;
   
    //return serial number field value
    get serial() {
        return getFieldValue(this.asset.data, SERIAL_FIELD);

    }

    //make callout using fetch
    connectedCallback() {

        fetch('https://jsonplaceholder.typicode.com/todos?id'+ this.serial, 
        //endpoint passes serial number from current sfdc record
        {
            // Request type
            method:"GET",
        
            headers:{
                // content type
                "Content-Type": "application/json",
                // adding your access token 
                //"Authorization": 'Basic ' + btoa(username + ":" + password),
                //"Authorization": "OAuth 00DB0000000EfVQ!AQwAQEiiynMU2EsBcS2PhXSQ6KQTTG.Zr0hlDHTFcGcAPqKQOBNDB0rwyASZK44fqIAVe6GrVNZPsAWJ6iqXLNBfSQ.dqvW1",
            }
        })
        .then((response) => {    
            return response.json(); // returning the response in the form of JSON
        })
        .then((jsonResponse) => {
                
                        let objData = {
                            title : '',
                            completed : '',
                        };
                        window.console.log('jsonResponse ===> '+JSON.stringify(jsonResponse));
                        // retriving the response data
                        let jsonData = jsonResponse[0];
            
                        // adding data object
                        objData.title = jsonData.title; 
                        objData.completed = jsonData.completed;
            
                        // adding data object to show in UI
                        this.toDoData = objData;
                    })
                    .catch(error => {
                        window.console.log('callout error ===> '+JSON.stringify(error));
                    })
                } 
            
            }

 
Hi Everyone,

I'm using @wire to pull back a fields value and then trying to pass it in an http callout. Unfortunately, the value is coming back undefined in the debugger and I used the salesforce documentation exactly. Does anyone see what I'm missing?
import { LightningElement, track, api, wire } from 'lwc';//api, wire 

import { getRecord, getFieldValue } from 'lightning/uiRecordApi';

//import field reference from schema
import SERIAL_FIELD from '@salesforce/schema/Asset.Serial_Test__c';

const field = [SERIAL_FIELD];

export default class TestCallout extends LightningElement {
  
    @track toDoData;
    @api recordId;
    @wire(getRecord, { recordId: '$recordId', field})
    asset;
   
    //return serial number field value
    get serial() {
        return getFieldValue(this.asset.data, SERIAL_FIELD);

    }

    //make callout using fetch
    connectedCallback() {

        fetch('https://jsonplaceholder.typicode.com/todos?id'+ this.serial, 
        //endpoint passes serial number from current sfdc record
        {
            // Request type
            method:"GET",
        
            headers:{
                // content type
                "Content-Type": "application/json",
                // adding your access token 
                //"Authorization": 'Basic ' + btoa(username + ":" + password),
                //"Authorization": "OAuth 00DB0000000EfVQ!AQwAQEiiynMU2EsBcS2PhXSQ6KQTTG.Zr0hlDHTFcGcAPqKQOBNDB0rwyASZK44fqIAVe6GrVNZPsAWJ6iqXLNBfSQ.dqvW1",
            }
        })
        .then((response) => {    
            return response.json(); // returning the response in the form of JSON
        })
        .then((jsonResponse) => {
                
                        let objData = {
                            title : '',
                            completed : '',
                        };
                        window.console.log('jsonResponse ===> '+JSON.stringify(jsonResponse));
                        // retriving the response data
                        let jsonData = jsonResponse[0];
            
                        // adding data object
                        objData.title = jsonData.title; 
                        objData.completed = jsonData.completed;
            
                        // adding data object to show in UI
                        this.toDoData = objData;
                    })
                    .catch(error => {
                        window.console.log('callout error ===> '+JSON.stringify(error));
                    })
                } 
            
            }