Skip to content
Home » Program to Create Demand from Idea in ServiceNow

Program to Create Demand from Idea in ServiceNow

This program is designed to create a demand record in ServiceNow from an idea record. The program automatically populates the demand record with relevant information from the idea record, such as business case, short description, submitter, and more. It also copies over work notes and comments from the idea record to the demand record.

Code

//run before update
//condition  = current.state == 2 && current.demand.nil() && previous.state != 2

// gs.hasRole('demand_manager')  && current.state == 2 && current.demand.nil() && previous.state != 2
var demandTable = "dmn_demand";
if(GlidePluginManager.isActive('com.snc.project_management_v3')){
	demandTable = SNC.PPMConfig.getDemandTable(current.getTableName());
}

var demand = new GlideRecord(demandTable);
demand.initialize();
var fields = ['business_case', 'short_description', 'submitter', 'sys_domain', 'business_unit', 'department', 'impacted_business_units', 'business_capabilities', 'business_applications'];
for(var i in fields){
	var field = fields[i];
	if(demand.isValidField(field)){
		demand.setValue(field, current.getValue(field));
	}
}
//carry over and_or set feilds from idea to demand
demand.setValue("category","strategic");
demand.setValue('u_domain',current.module);
demand.setValue("type", "enhancement");
demand.setValue("parent", current.sys_id);
demand.setValue("idea", current.sys_id);
demand.setValue('u_sub_domain',getSubDomain(current.sys_id));
demand.setValue('assignment_group', current.u_it_assignment_group);
demand.setValue('priority',current.priority);
demand.setValue('u_growth',current.u_growth2);
demand.setValue('u_quality_safety',current.u_quality_and_safety2);
demand.setValue('u_financial',current.u_financial2);
demand.setValue('u_ppl',current.u_people2);
demand.setValue('u_it_wrk_effort',current.u_analyst_work_effort);
demand.setValue('u_complexity',current.u_complexity_of_coordination);
demand.setValue('u_training',current.u_training_needs);
demand.setValue('u_cust_wrk_effort',current.u_r_d_focus);
demand.setValue('u_prj_advanatages',current.u_epic_advantages);
demand.setValue('u_targeted_date',current.u_target_date);
demand.setValue('u_add_justification',current.u_additional_justification);
demand.setValue('u_est_loe',current.u_est_loe);//NG added 1.20.22 NOAH separation requirements

//since impacted portfolio is a list field you need to convert the values to strings in order to carry them over to the new record
var impactedPort = current.u_impacted_portfolio.toString();
if(impactedPort != ''){
demand.setValue('u_impacted_portfolios',impactedPort);	
}


demand.work_notes = current.work_notes.getJournalEntry(-1);
demand.work_notes = current.comments.getJournalEntry(-1);

var desc = current.idea_description; 
var regX = /<\/?[^>]+(>|$)/g;
var regxFormatted = desc.replace(regX, " "); //format desc with above regex
var spaceFormatted = regxFormatted.replaceAll(' ', " "); //replace all nbsp html characters with a space
finalDesc = spaceFormatted.replaceAll('–', "-"); // replace all ndash encoded html w/ ascii dash
demand.setValue("description", finalDesc);

if(GlidePluginManager.isActive('com.snc.apm')){
	demand.setValue("size", current.effort);
	if(current.pm_program)
		demand.setValue("primary_program", current.pm_program);
	if(current.start_by_fiscal_year)
		demand.setValue("start_date", new GlideDateTime(current.start_by_fiscal_year.fiscal_start_date_time).date);
	if(current.implement_by_fiscal_year)
		demand.setValue("requested_by", new GlideDateTime(current.implement_by_fiscal_year.fiscal_end_date_time).date);
	
	var demandPrice = parseFloat(current.estimated_benefit.getReferenceValue());
	demand.setValue("financial_benefit",  current.estimated_benefit.getReferenceCurrencyCode() + ';' + demandPrice);
}

var dmnId = demand.insert();
demand.get(dmnId);
current.demand = dmnId;
current.stage = 'demand';
GlideSysAttachment.copy('idea', current.sys_id, 'dmn_demand', demand.sys_id); // second paramenter is the current record and new record you created
var link = ' <a href ="/' + demandTable + '.do?sysparm_query=number%3D' + demand.getValue('number') + '">'+ demand.getValue('number') +'</a>';
var message = gs.getMessage("Demand {0} has been created");
	message = message.replace("{0}", link);
		
		gs.addInfoMessage(message);


function getSubDomain(id){
var gr = new GlideRecord('im_m2m_idea_category');
	gr.addQuery('idea',id);
	gr.query();
	if(gr.next()){
		return gr.category_id;
	}
	
}

Code Explanation

Firstly, the program checks if the “com.snc.project_management_v3” plugin is active. If it is, the program retrieves the demand table from the plugin configuration. Otherwise, it uses the default demand table “dmn_demand”.

Next, a new GlideRecord object is created for the demand table. The demand record is initialized and a list of fields to be carried over from the idea to the demand record is defined.

Using a loop, the program checks if each field is valid in the demand record and sets its value to the corresponding value in the current idea record.

Special handling is done for the “impacted_portfolio” field, which is a list field. The values of this field are converted to strings before being set in the demand record.

The program also sets various other fields in the demand record, such as category, type, parent, idea, and assignment group. It also sets fields related to priority, growth, quality and safety, financials, people, IT work effort, complexity, training needs, customer work effort, project advantages, targeted date, additional justification, estimated LOE, and impacted portfolios.

The work notes and comments from the idea record are copied over to the demand record.

If the “com.snc.apm” plugin is active, additional fields related to size, primary program, start date, requested by, and financial benefit are set in the demand record.

The demand record is then inserted and retrieved using its sys_id. The current idea record is updated with the demand’s sys_id and the stage is set to “demand”.

Attachments from the idea record are copied to the demand record using the GlideSysAttachment.copy() function.

Finally, a message is displayed to inform the user that a demand record has been created, along with a link to the newly created demand record.

Code Example

“`javascript //run before update //condition = current.state == 2 && current.demand.nil() && previous.state != 2

// gs.hasRole(‘demand_manager’) && current.state == 2 && current.demand.nil() && previous.state != 2 var demandTable = “dmn_demand”; if(GlidePluginManager.isActive(‘com.snc.project_management_v3’)){ demandTable = SNC.PPMConfig.getDemandTable(current.getTableName()); }

var demand = new GlideRecord(demandTable); demand.initialize(); var fields = [‘business_case’, ‘short_description’, ‘submitter’, ‘sys_domain’, ‘business_unit’, ‘department’, ‘impacted_business_units’, ‘business_capabilities’, ‘business_applications’]; for(var i in fields){ var field = fields[i]; if(demand.isValidField(field)){ demand.setValue(field, current.getValue(field)); } } //carry over and_or set feilds from idea to demand demand.setValue(“category”,”strategic”); demand.setValue(‘u_domain’,current.module); demand.setValue(“type”, “enhancement”); demand.setValue(“parent”, current.sys_id); demand.setValue(“idea”, current.sys_id); demand.setValue(‘u_sub_domain’,getSubDomain(current.sys_id)); demand.setValue(‘assignment_group’, current.u_it_assignment_group); demand.setValue(‘priority’,current.priority); demand.setValue(‘u_growth’,current.u_growth2); demand.setValue(‘u_quality_safety’,current.u_quality_and_safety2); demand.setValue(‘u_financial’,current.u_financial2); demand.setValue(‘u_ppl’,current.u_people2); demand.setValue(‘u_it_wrk_effort’,current.u_analyst_work_effort); demand.setValue(‘u_complexity’,current.u_complexity_of_coordination); demand.setValue(‘u_training’,current.u_training_needs); demand.setValue(‘u_cust_wrk_effort’,current.u_r_d_focus); demand.setValue(‘u_prj_advanatages’,current.u_epic_advantages); demand.setValue(‘u_targeted_date’,current.u_target_date); demand.setValue(‘u_add_justification’,current.u_additional_justification); demand.setValue(‘u_est_loe’,current.u_est_loe);//NG added 1.20.22 NOAH separation requirements

//since impacted portfolio is a list field you need to convert the values to strings in order to carry them over to the new record var impactedPort = current.u_impacted_portfolio.toString(); if(impactedPort != ”){ demand.setValue(‘u_impacted_portfolios’,impactedPort); }

demand.work_notes = current.work_notes.getJournalEntry(-1); demand.work_notes = current.comments.getJournalEntry(-1);

var desc = current.idea_description; var regX = /<\/?[^>]+(>|$)/g; var regxFormatted = desc.replace(regX, ” “); //format desc with above regex var spaceFormatted = regxFormatted.replaceAll(‘ ‘, ” “); //replace all nbsp html characters with a space finalDesc = spaceFormatted.replaceAll(‘–’, “-“); // replace all ndash encoded html w/ ascii dash demand.setValue(“description”, finalDesc);

if(GlidePluginManager.isActive(‘com.snc.apm’)){ demand.setValue(“size”, current.effort); if(current.pm_program) demand.setValue(“primary_program”, current.pm_program); if(current.start_by_fiscal_year) demand.setValue(“start_date”, new GlideDateTime(current.start_by_fiscal_year.fiscal_start_date_time).date); if(current.implement_by_fiscal_year) demand.setValue(“requested_by”, new GlideDateTime(current.implement_by_fiscal_year.fiscal_end_date_time).date); var demandPrice = parseFloat(current.estimated_benefit.getReferenceValue()); demand.setValue(“financial_benefit”, current.estimated_benefit.getReferenceCurrencyCode() + ‘;’ + demandPrice); }

var dmnId = demand.insert(); demand.get(dmnId); current.demand = dmnId; current.stage = ‘demand’; GlideSysAttachment.copy(‘idea’, current.sys_id, ‘dmn_demand’, demand.sys_id); // second paramenter is the current record and new record you created var link = ‘ ‘+ demand.getValue(‘number’) +’‘; var message = gs.getMessage(“Demand {0} has been created”); message = message.replace(“{0}”, link); gs.addInfoMessage(message);

function getSubDomain(id){ var gr = new GlideRecord(‘im_m2m_idea_category’); gr.addQuery(‘idea’,id); gr.query(); if(gr.next()){ return gr.category_id; } } “`

In this post, we learned how to create a demand record from an idea record in ServiceNow. The provided code snippet demonstrates the step-by-step process of carrying over relevant fields from the idea record to the demand record. It also covers additional functionality such as copying work notes and comments, handling list fields, and attaching files. By using this program, users can efficiently convert their ideas into actionable demands within the ServiceNow platform.

Note: It is important to understand the prerequisites and dependencies of the code snippet before implementing it in a production environment. Also, make sure to test the code thoroughly to ensure its compatibility with your ServiceNow instance.

I hope you found this blog post useful. Stay tuned for more technical content!

Also checkout the following codes.


How to Get the Date One Month Ago in JavaScript – Program to Calculate the Date
Program to Fetch and Display Cat Facts using API with JavaScript