Hi! I'm trying to make that every time a user edit...
# suitescript
s
Hi! I'm trying to make that every time a user edits a record and when the 'custrecord_gf_data_prevista' field is empty, I change the duedate date. However, it doesn't change anything and doesn't return any error. This is an old problem that I still haven't been able to solve, but I'm still trying to do it today. Can someone help me?
Copy code
var currentRecord = context.newRecord;
        var transactionRec = record.load({
            type: currentRecord.type,
            id: currentRecord.id
        });

        var terms = transactionRec.getValue({
            fieldId: 'duenextmonthifwithindays'
        })

        var fluxoDataPrevista = transactionRec.getValue({
            fieldId: 'custrecord_gf_data_prevista'
        })

        var data = new Date(dataEntrega)

        var formatDate = format.format({
            value: data,
            type: format.Type.DATE
        })

        var dataEntrega = transactionRec.getValue({
            fieldId: 'duedate'
        })

        if (context.type === context.UserEventType.EDIT) {

            if (fluxoDataPrevista === '') {
                currentRecord.setValue({
                    fieldId: 'duedate',
                    value: formatDate.setDate(formatDate.getDate() + getDay(90))
                })
            }
        }
j
Where is this happening?
Data cannot be manipulated for records that are loaded in beforeLoad scripts. If you attempt to update a record loaded in beforeLoad, the logic is ignored.
😄 1
c
Same for aftersubmit
😄 1
b
its probably a combination of things
👏 1
your date manipulation looks off,
var data = new Date(dataEntrega)
is wrong since it uses
dataEntrega
before its given a value
👏 1
so essentially you have a garbage Date that you are trying to manipulate
👏 1
that should have given you an error; since you dont, that suggests that this is an afterSubmit user event script
👏 1
since changes to the newRecord are ignored there
👏 1
my advice remains the same as the first time you came here with this code
👏 1
take small steps
👏 1
make your code able to set the duedate to any Date, i personally recommend today's date since thats easy
👏 1
then make your code able to add a hardcoded amount of days to that Date
👏 1
then finally add the code to lookup the amount of days to add so you dont need to hardcode the amount of days to add
👏 1
j
How do you know that
dataEntrega
hasn’t been given a value earlier in the script?
😄 1
it is a bit weird that it’s given a value AFTER but could have a value before as well.
😄 1
b
the only way
formatDate.setDate
doesnt throw an error is if
data
isn't a Date
👏 1
the normal output of
format.format
is a string
👏 1
s
thank you guys
hey @battk This path I'm following in the code, is it the best?
@jen On declares the delivery date AFTER, it was due to inattention 😄
b
dont take this too badly, but your code is not close to a functional solution
at best only 2 statements lead you closer to your solution
s
@battk I imagined 😢
@battk Which are they?
b
knowing which lines honestly wont help you much, keeping the lines which I think I would keep still wouldn't get you usable code
👏 1
its just that I dont think they will eventually cause errors
👏 1
my recomendation remains, make your code take small steps towards your solution
👏 1
the first being updating the duedate to any Date
👏 1
if you dont know how to update the record in the first place, then you probably want to start at updating any field, memo being pretty popular for this purpose
👏 1
s
@battk Thanks for your patience. I will try my best here. 😄