JavaScript onSave Function Example 2: Conditional Query


In this example, a user is creating a Task from an Opportunity. A Lead Source is selected as a parent object, and depending on the value stored in the parent, a different value is stored in the Task record Description field. The following assumptions are made in this example:

  • The MODULEID of the parent module is 4.
  • The MEFIELDNAME of the Lead Source parent field is PIPEKW15.
  • The MEFIELDNAME of the Description field to update is TASKTXT03.


An example of a complete onSave query with header and footer information and a conditional query is as follows:

<html><head><title>onCreateExample2</title><script type="text/javascript"> 
function fieldsOnDefault(){ return "TASKTXT03-4.PIPEKW15"; }
function getDefaultValueOfTASKTXT03(LeadSource){
	if(LeadSource == 'Event - Seminar'){
		return "Task regarding Event Seminar";
	}else if( LeadSource == 'Event - Trade Show'){
		return "Task regarding Event Trade Show";
	}else if( LeadSource == 'Event - Other'){
		return "Task regarding General Event";
	}else{ 
		return "";
	}
}
</script></head><body></Body></html> 


where:

  • return "TASKTXT03-4.PIPEKW15"; changes because you want to calculate the value of TASKTXT03 based on a value stored in module 4, field PIPEKW15.


In this example, the source of the Opportunity (PIPEKW15, LeadSource) will translate, on the creation of a new Task from that Opportunity, to the Task's Description (TASTTXT03).