Hi everyone, I am encountering the following error...
# suitescript
s
Hi everyone, I am encountering the following error while trying to create the credit memo, although the field value "6" for the taxcode exists -
Copy code
"type":"error.SuiteScriptError","name":"INVALID_FLD_VALUE","message":"You have entered an Invalid Field Value 6 for the following field: taxcode"
Below is the code. Where am I going wrong?
Copy code
/**
 * @NApiVersion 2.1
 * @NScriptType Restlet
 * @NModuleScope SameAccount
 */
define(['N/record', 'N/format', 'N/error', 'N/log'], 
function(record, format, error, log) {
    
    /**
     * Function to handle POST request - creates a Credit Memo
     * @param {Object} requestBody - The request body from the external system
     * @returns {Object} Response object with created Credit Memo ID and other details
     */
    function doPost(requestBody) {
        try {
            log.debug('Request Body', JSON.stringify(requestBody));
            
            // Validate required fields
            if (!requestBody.entity) {
                throw error.create({
                    name: 'MISSING_REQ_PARAM',
                    message: 'Entity (customer) is required'
                });
            }
            
            // Create Credit Memo record
            const creditMemo = record.create({
                type: record.Type.CREDIT_MEMO,
                isDynamic: true
            });
            
            // Set header level fields
            creditMemo.setValue({
                fieldId: 'entity',
                value: requestBody.entity
            });
            
            // Set trandate if provided (format: mm/dd/yyyy)
            if (requestBody.trandate) {
                const parsedDate = format.parse({
                    value: requestBody.trandate,
                    type: format.Type.DATE
                });
                
                creditMemo.setValue({
                    fieldId: 'trandate',
                    value: parsedDate
                });
            }
            
            // Set tax details override flag
            if (requestBody.taxdetailsoveride) {
                creditMemo.setValue({
                    fieldId: 'taxdetailsoverride',
                    value: requestBody.taxdetailsoveride
                });
            }
            
            // Add line items
            if (requestBody.lines && Array.isArray(requestBody.lines)) {
                for (let i = 0; i < requestBody.lines.length; i++) {
                    const line = requestBody.lines[i];
                    
                    // Select new line
                    creditMemo.selectNewLine({
                        sublistId: 'item'
                    });
                    
                    // Set line item
                    creditMemo.setCurrentSublistValue({
                        sublistId: 'item',
                        fieldId: 'item',
                        value: line.item
                    });
                    
                    // Set amount
                    if (line.amount) {
                        creditMemo.setCurrentSublistValue({
                            sublistId: 'item',
                            fieldId: 'amount',
                            value: line.amount
                        });
                    }
                    
                    // Set HSN code if provided
                    if (line.hsncode) {
                        creditMemo.setCurrentSublistValue({
                            sublistId: 'item',
                            fieldId: 'custcol_in_hsn_code', // Adjust field ID as needed
                            value: line.hsncode
                        });
                    }
                    
                    // Commit the line - only need to commit once
                    creditMemo.commitLine({
                        sublistId: 'item'
                    });
                }
            }
            
            // Handle tax details separately after all item lines are added
            if (requestBody.taxdetails && Array.isArray(requestBody.taxdetails)) {
                for (let j = 0; j < requestBody.taxdetails.length; j++) {
                    const taxDetail = requestBody.taxdetails[j];
                    
                    // Select the tax details sublist
                    creditMemo.selectNewLine({
                        sublistId: 'taxdetails'
                    });
                    
                    // Tax code is required - make sure we have a valid value before proceeding
                    if (!taxDetail.taxcode) {
                        throw error.create({
                            name: 'MISSING_REQ_PARAM',
                            message: 'Tax code is required for tax details'
                        });
                    }
                    
                    // Set tax code first since it's required
                    creditMemo.setCurrentSublistValue({
                        sublistId: 'taxdetails',
                        fieldId: 'taxcode',
                        value: taxDetail.taxcode
                    });
                    
                    // Now set the other tax detail fields
                    if (taxDetail.linename) {
                        creditMemo.setCurrentSublistValue({
                            sublistId: 'taxdetails',
                            fieldId: 'linename',
                            value: taxDetail.linename
                        });
                    }
                    
                    if (taxDetail.linenumber) {
                        creditMemo.setCurrentSublistValue({
                            sublistId: 'taxdetails',
                            fieldId: 'linenumber',
                            value: taxDetail.linenumber
                        });
                    }
                    
                    if (taxDetail.linetype) {
                        creditMemo.setCurrentSublistValue({
                            sublistId: 'taxdetails',
                            fieldId: 'linetype', 
                            value: taxDetail.linetype
                        });
                    }
                    
                    if (taxDetail.netamount) {
                        creditMemo.setCurrentSublistValue({
                            sublistId: 'taxdetails',
                            fieldId: 'netamount',
                            value: taxDetail.netamount
                        });
                    }
                    
                    if (taxDetail.taxamount) {
                        creditMemo.setCurrentSublistValue({
                            sublistId: 'taxdetails',
                            fieldId: 'taxamount',
                            value: taxDetail.taxamount
                        });
                    }
                    
                    if (taxDetail.taxbasis) {
                        creditMemo.setCurrentSublistValue({
                            sublistId: 'taxdetails',
                            fieldId: 'taxbasis',
                            value: taxDetail.taxbasis
                        });
                    }
                    
                    if (taxDetail.taxdetailsreference) {
                        creditMemo.setCurrentSublistValue({
                            sublistId: 'taxdetails',
                            fieldId: 'taxdetailsreference',
                            value: taxDetail.taxdetailsreference
                        });
                    }
                    
                    if (taxDetail.taxrate) {
                        // Remove percentage symbol if present and convert to number
                        let rateValue = taxDetail.taxrate;
                        if (typeof rateValue === 'string') {
                            rateValue = rateValue.replace('%', '');
                        }
                        
                        creditMemo.setCurrentSublistValue({
                            sublistId: 'taxdetails',
                            fieldId: 'taxrate',
                            value: parseFloat(rateValue)
                        });
                    }
                    
                    if (taxDetail.taxtype) {
                        creditMemo.setCurrentSublistValue({
                            sublistId: 'taxdetails',
                            fieldId: 'taxtype',
                            value: taxDetail.taxtype
                        });
                    }
                    
                    // Commit the tax detail line
                    creditMemo.commitLine({
                        sublistId: 'taxdetails'
                    });
                }
            }
            
            // Save the credit memo
            const creditMemoId = creditMemo.save();
            
            // Return success response
            return {
                success: true,
                creditMemoId: creditMemoId,
                message: 'Credit Memo created successfully'
            };
            
        } catch (e) {
            log.error('Error creating Credit Memo', e);
            
            // Return error response
            return {
                success: false,
                error: {
                    code: e.name,
                    message: e.message
                }
            };
        }
    }

    /**
     * Function to handle GET request - retrieves a Credit Memo by ID
     * @param {Object} requestParams - Contains the ID of the Credit Memo to retrieve
     * @returns {Object} Credit Memo data
     */
    function doGet(requestParams) {
        try {
            // Check if ID is provided
            if (!requestParams.id) {
                throw error.create({
                    name: 'MISSING_REQ_PARAM',
                    message: 'Credit Memo ID is required'
                });
            }
            
            // Load Credit Memo record
            const creditMemo = record.load({
                type: record.Type.CREDIT_MEMO,
                id: requestParams.id,
                isDynamic: false
            });
            
            // Get basic info
            const creditMemoData = {
                id: creditMemo.id,
                tranId: creditMemo.getValue({fieldId: 'tranid'}),
                entity: creditMemo.getValue({fieldId: 'entity'}),
                tranDate: creditMemo.getValue({fieldId: 'trandate'}),
                total: creditMemo.getValue({fieldId: 'total'}),
                status: creditMemo.getValue({fieldId: 'status'})
            };
            
            return {
                success: true,
                creditMemo: creditMemoData
            };
            
        } catch (e) {
            log.error('Error retrieving Credit Memo', e);
            
            return {
                success: false,
                error: {
                    code: e.name,
                    message: e.message
                }
            };
        }
    }

    // RESTlet entry points
    return {
        post: doPost,
        get: doGet
    };
});
a
when you're working with a record in dynamic mode the order you set fields in matters.
susbsidiary
is the field that almost always needs to be set first, and I don't see you setting that at all. since all kinds of things can be invalid if the subsidiary isn't set it may just be that submitting your tax detail is the first check that occurs and gives you the error message, and as you suspect the tax code isn't invalid at all... its the combination of tax code = 6 and subsidiary = null that's the issue.
s
I tried setting the subsidiary as well, but still the same error