Hello all, I am trying to trigger another user eve...
# suitescript
j
Hello all, I am trying to trigger another user event script by redirecting to a suitelet in my code from a different user event script. I am working with SuiteScript 1.0, but for some reason when i call the redirect function, it does not activate my other user event script.
s
UserEvents cannot fire other UserEvents
j
yes, the work around is to use one user event script to redirect to the mentioned Suitelet. From the suitelet you should then be able to open a record which activates the associated user event script
as far as i know atleast
b
A redirect from a suitelet at best will get you to beforeLoad
And its very unusual to do real work in a beforeLoad
j
i have the script that gets valled after submitting the record
but the issue is that after calling that nlapiSetRedirectURL() function, it never hits the suitelet code
b
You might want to share your script, its usually a terrible idea to modify the record being loaded in a beforeLoad
j
Copy code
nlapiLogExecution("DEBUG","STARTING PRE-DESIGNED GRAPHIC AUTOMATION");

 function main(request, response)
 {
    nlapiLogExecution('DEBUG','Getting Request');
    var s3Id = request.getParameter('s3RecordId');
    var item = request.getParameter('lineItem');
    //var context = nlapiGetContext();
    //var req_param = context.request.parameters;

    nlapiLogExecution('DEBUG','Loading Amazon S3 Record');
    var s3Record = nlapiLoadRecord('customrecord_sc_s3_files', s3Id);
    nlapiLogExecution('DEBUG','S3 Record Id', s3Id);
    //var s3Record = nlapiLoadRecord('customrecord_sc_s3_files', req_param.s3RecordId);
    //nlapiLogExecution('DEBUG','S3 Record Id', req_param.s3RecordId);

    //Block Quick Proof Email Checkbox
    nlapiLogExecution('DEBUG','Blocking quickproof email');
    s3Record.setFieldValue('custrecord_blockquickproofemail', 'T');
    
    //Set File Uploaded by Customer Field
    nlapiLogExecution('DEBUG','Loading Item Record');
    var itemRecord = nlapiLoadRecord('assemblyitem', item);
    nlapiLogExecution('DEBUG','Line Item', item);
    //var itemRecord = nlapiLoadRecord('assemblyitem', req_param.lineItem);
    //nlapiLogExecution('DEBUG','Line Item', req_param.lineItem);


    nlapiLogExecution('DEBUG','Setting File Uploaded By Customer Field');
    var fileUploadedByCustomer = itemRecord.getFieldValue('custitem_s3_pd_graphic_link');
    s3Record.setFieldValue('custrecord_fileuploadedbycustomer', fileUploadedByCustomer);
    nlapiLogExecution('DEBUG','File Uploaded By Customer', fileUploadedByCustomer);

    nlapiSubmitRecord(s3Record);
 }
Here is the suitelet
i am editing record fields after loading
b
does it redirect to the suitelet at all?
j
no, it doessnt seem to. It does not even hit the first line of code outside the main function, the log
b
what does your afterSubmit function look like on your user event script
j
Copy code
function createRecords(type) {
	if (type == 'create' || type == 'edit') {
		
		var salesOrder = nlapiGetNewRecord(), lines = getSalesOrderLines(salesOrder);

		try {
			var currOrderStat = salesOrder.getFieldValue('custbody_orderstatus');
			var customerSource = salesOrder.getFieldValue('custbody_f3_source');

          if (customerSource == "WS Display - beta"){
            dLog('source is not wsdisplay, signmall or manual. exit', ' source is not wsdisplay, signmall or manual. exit');
            return;
          }

			if (!inArray(currOrderStat, arrOrderStat)) {

				var statText = salesOrder.getFieldText('custbody_orderstatus');
				dLog('beforeLoad_showReprints', statText + ' Order status | no need to execute. exit script.');
				return;
			}
		} catch (e) {
          
          nlapiLogExecution('ERROR', 'Record not created');

		}

		if (type == 'create') {
            dLog('attempting to create file records', 'attempting to create S3 and GA records');
			lines = createGraphicApprovalRecords(lines, salesOrder);
			createFileRecords(lines, salesOrder);
            
		} else {
			nlapiLogExecution('DEBUG', 'Calling updateFileRecords');
			updateFileRecords(lines, salesOrder);
           
		}
	}
}
this is the afterSubmit function that gets called from my user event script
b
where is the redirect
j
Copy code
function createGraphicApprovalRecords(lines, salesOrder) {
	var customerId = salesOrder.getFieldValue('entity'), status = GRAPHIC_APPROVAL_STATUS, salesOrderId = salesOrder.getId();
	nlapiLogExecution('DEBUG','In createGraphicApprovalRecords');

	_.each(lines, function(line) {
		var record = nlapiCreateRecord('customrecord_sc_graphic_approval');

		record.setFieldValue('custrecord_sc_ga_customer', customerId);
		record.setFieldValue('custrecord_sc_ga_status', status);
		record.setFieldValue('custrecord_sc_ga_sales_order', salesOrderId);
		record.setFieldValue('custrecord_sc_ga_line_identifier', line.identifier);
		record.setFieldValue('custrecord_sc_ga_item', line.item);
		record.setFieldValue('custrecord_sc_ga_item_quantity', line.quantity);
		record.setFieldValue('custrecord_sc_ga_location', line.location);

		//CHECK FOR PRE-DESIGNED GRAPHIC UPON ORDER CREATION	
		nlapiLogExecution('DEBUG','In CreateGA Line.Order Detail: ',  line.orderDetail);
		//If Order Detail is "Pre-Designed Graphic" set Graphic Approval field: Status to "Approved", Internal ID: 1
		if(line.orderDetail == "Pre-Designed Graphic")
		{
			record.setFieldValue('custrecord_sc_ga_status', GRAPHIC_APPROVED);
			nlapiLogExecution('DEBUG','Graphic Status', 'Pre-Designed, Setting status to Approved');
		}

		line.grapphicApproval = nlapiSubmitRecord(record);

		//Creates link to graphic approval page for designer to upload files in the future
		var url = nlapiResolveURL('SUITELET', 'customscript_sc_ga_approval_designer', 'customdeploy_sc_ga_approval_designer', false), id = line.grapphicApproval;
		record = nlapiLoadRecord('customrecord_sc_graphic_approval', id);
		record.setFieldValue('custrecord_sc_ga_approval_link', url + '&id=' + id);

		nlapiSubmitRecord(record);
		nlapiLogExecution('ERROR', 'graphic approval record created');
	});

	return lines;
}
redirect is called here in this function
"createGraphicApprovalRecords"
which is called from the afterSubmit function "createRecords"
b
i still dont see the redirect
where do you use nlapiSetRedirectURL
j
Oh i see that nlapiResolveURL is being used instead
oh wait wrong function
resending
Copy code
function createFileRecords(lines, salesOrderId) {
	nlapiLogExecution('DEBUG','In createFileRecords');
	_.each(lines, function(line) {
		var record = nlapiCreateRecord('customrecord_sc_s3_files');
		record.setFieldValue('custrecord_sc_amazon_sales_order_id', salesOrderId.getId());
		record.setFieldValue('custrecord_sc_amazon_sales_order', salesOrderId.getId());
		record.setFieldValue('custrecord_sc_s3_line', line.identifier);
		record.setFieldValue('custrecord_sc_line_item', line.item);
		record.setFieldValue('custrecord_sc_uploaded_by_customer', 'T');
		record.setFieldValue('custrecord_sc_s3_graphic_approval', line.grapphicApproval);

		//IF ORDER DETAIL IS PRE-DESIGNED GRAPHIC
		if(line.orderDetail == "Pre-Designed Graphic")
		{
			//redirect to Pre-Designed Graphic suitelet
			nlapiLogExecution('DEBUG', 'Item is Pre-Designed Graphic: REDIRECTING TO PRE-DESIGNED GRAPHIC AUTOMATION SUITELET');
			nlapiSubmitRecord(record);
			var params = {"s3RecordId": record.getId(),
						  "lineItem": line.item};

			nlapiSetRedirectURL('SUITELET','customscript_tflow_pd_graphic_automation','customdeploy_tflow_pd_graphic_automation', null, params);
			nlapiLogExecution('DEBUG', 'Returning from redirect');
			//nlapiLogExecution('DEBUG','Blocking quickproof email');
			//Block Quickproof Email
			//record.setFieldValue('custrecord_blockquickproofemail', 'T');
		}
		else
		{
			nlapiSubmitRecord(record);
		}
		nlapiLogExecution('ERROR', 'file records created');
	});
}
b
are you creating the sales order in the ui?
and are you getting the
Item is Pre-Designed Graphic: REDIRECTING TO PRE-DESIGNED GRAPHIC AUTOMATION SUITELET
log in the script logs?
j
The sales order is created when a customer makes a purchase from our webstore.
And yes, that log does show up in the script logs
b
i dont think you will be able to redirect from a webstore context
j
The log 'Returning from redirect' after calling the redirect shows up as well
b
you can try from the internal netsuite user interface
i believe that should work normally from what you shared
but i personally think you would be better served using a scheduled script to do your work
or at least splitting the code you want from the other user event into a shared library that you use in the sales order one
j
alright, I'll try these out. Thank you for the help and advice!
can scheduled scripts be manually called?
or set on an interval for it to be called only?
b
j
thank you!