Tom Hanson
Tom Hanson

Reputation: 935

SuiteScript2.0 - Transforming Opportunity to Sales Order does not set Shipping Address Correctly

I am trying to automate creating a lead, opportunity and sales order in one click of a button. The code below is how I am achieving this on a suilet. Everything get created and set perfectly expect for the Shipping Address on the Sales Order. For some reason the shipping address of the sales order is getting set to custom and only entering the State and Zip Code. I'm pretty sure I don't create an address for the sales order and feel like it should default to the default shipping address on the customer in the same way the billing address does. I have given the data.saleorder data.

function submitdata(data){
    var leadid = createLead(data);
    var opportunityid = createOpportunity(data, leadid);
    var salesorderid;
    if(data.salesorder) {
        salesorderid = transformRecordAndSetNewValues({
            from : {
                type : record.Type.OPPORTUNITY, 
                id : opportunityid
            },
            to : {
                type : record.Type.SALES_ORDER
            },
            values : data.salesorder,
            ignoreMandatoryFields : false,
        });
    }
    return {
        leadid,
        opportunityid,
        salesorderid,
        success : true
    }
}

function transformRecordAndSetNewValues(data){
    var objRecord = record.transform({
        fromType : data.from.type,
        fromId : data.from.id,
        toType : data.to.type,
        isDynamic : true,
        defaultValues : {
            customform : Number(data.values.customform)
        }
    });
    setRecordValues(objRecord, data.values);
    var recId = objRecord.save({
        ignoreMandatoryFields : data.ignoreMandatoryFields
    });
    setRecordForm({
        recordid : recId,
        form : data.values.customform,
        type : data.to.type
    });
    return recId;
}

function setRecordValues(objRecord, values)
{
    //Loop Fields And Add Values
    for(var key in values){
        log.debug(key, values[key])
        if(key == "shippingaddress" || key === "billingaddress"){
            createAddress(objRecord, key, values)
            continue;
        }
        //if(key === "type") continue;
        var value = values[key];
        value = testForBooleanAndConvertToBoolean(value)
        value = testForDateAndConvertToDate(value)
        objRecord.setValue({
            fieldId : key,
            value : value
        })
    }
}

{
    "lines": [
        {
            "item": {
                "id": "1234",
                "sku": "ANSKU",
                "pricelevels": {
                    "1": "118.09091",
                    "6": "82.66364",
                    "7": "47.23636",
                    "9": "112.19",
                    "10": "106.28",
                    "11": "100.38",
                    "12": "94.47"
                },
                "taxcode": {
                    "text": "GST",
                    "value": "7"
                },
                "taxrate": "10.00",
                "displayname": "An Item Descripton",
                "description": "An Item Descripton",
                "type": {
                    "text": "Inventory Item",
                    "value": "InvtPart"
                },
                "model": ""
            },
            "quantity": 1,
            "pricelevel": {
                "value": "1",
                "text": "BASE PRICE"
            },
            "rate": "118.09091",
            "amount": "118.09",
            "taxcode": "GST",
            "taxrate1": "10.00",
            "tax1amt": "11.81",
            "grossamount": "129.90"
        }
    ],
    "customform": 101,
    "formtype": true,
    "department": "7",
    "location": "1",
    "terms": "7",
    "timeslot": "2021-01-28",
    "shipmethod": "1234",
    "id": "123456"
}

Upvotes: 0

Views: 364

Answers (1)

Jala
Jala

Reputation: 919

When you transform to the sales order, set the 'entity' field.

Upvotes: 1

Related Questions