function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Linda 98Linda 98 

VALIDATE input text field value given by USER--URGENT

Hi

I am trying to validate the input text field given by user if it contains 'test' in its name.If so then when user clicks save button,record has to be saved by getting data  from existing record on which button is clicked and clone the record(Not all fields.Some fields should be different)

If the userinput doesnt contain 'test ' then itt  haas to show an error saying to enter corrrect name when he clicks on 'save'.

Please suggest some way .

I am not able to get ID of record on which VF page is opened.

Please help.Need this ASAP!!!!

kaustav goswamikaustav goswami
Let us consider that you have an inputText element in your VF page
<apex:inputText id="testInp" value="{!attribute_value}" />

and you have a save button like this
<apex:commandButton value="Save" onclick="myJavascript();return false;" />

Javascript function:

function myJavascript(){
    var val = document.getElementById("testInp").value;
    if(val != ""){
        if(val.indexOf("test") == -1){
            alert("please enter the correct value");
        }else{
            // call your action function here
        }
    }
}

Additionally please note that if the id of the input element might be different. Please refer to the generated HTML and put the correct id otherwise document.getElementById() function will fail.

Please let me know if this helps.

If not then please post some code so taht we get a better undertsanding of your problem.

Thanks,
Kaustav
Raghu Reddy 14Raghu Reddy 14
Hi,

You need to use $Component.Path.to.Id in JavaScript

Sample Code:

<apex:inputText id="inputText" value="{!whateverValue}" />
<apex:commandButton  id="cmdButton" value="Next" />

<script language="javascript">
           var NextProduct =document.getElementById('{!$Component.inputText}');
           alert(NextProduct);
</script>

https://www.salesforce.com/us/developer/docs/pages/Content/pages_variables_global_component.htm


/Raghu









Linda 98Linda 98
But i am quering in class.. Basically i am checking if user gave value which contain 'test' i am checking this with owner names. So i am quering list of owner's in class and storing in a variable...r If he gives word 'test owner' it has to check the owner name and save it If not error saying enter correct name. I didnt get your reply..can u explain what you meant??
kaustav goswamikaustav goswami
So the main problem is that you are not getting the id of the record on which user has clicked on the page in the controller. You need the id in your class to query the record from database. I though that you needed you show some sort of validation message based on what the user has input in the text field. Can you please post your page and your controller. That will help us to better understand what is it that you are having problem with.

Thanks,
Kaustav 
kaustav goswamikaustav goswami
I received a mail which contains some code for this question.

I have done some modifications. Please refer to the inline comments. If this does not help you then please post your page and specify the location from which you are clicking the button. That is whether the button is present on the detail page of case record or the button is present in some other VF page.

public with sharing class caseclass {
  
    Public string inputownername{get;set;}
    // variable to hold the current case record
    public Case currentCase {get; set;}
    // variable to hold the id of the current case record
    public String currentCaseId {get; set;}
    // this is the new case that will be inserted
    public Case newCaseToBeCreated {get; set;}
   
    public caseclass(ApexPages.StandardController Controller) {
	//stCurrentPageURL = ApexPages.currentPage().getURL();
	//REcordid = System.currentPageReference().getParameters().get('id');
	
	// in the constructor get the id of the case record on which the button was clicked
	// i hope the button is clicked from the detail page of the case
	currentCase = (Case)Controller.getRecord();
	currentCaseId = currentCase.Id;
    }
   
    public PageReference save() {
	// this page reference variable will be returned
	PageReference pgRef = null;
	if(CheckInput() == false){
		ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Owner Name Invalid.'));
		//- no need to return the same page reference this will result in an unexpected behaviour
		//PageReference currPage = new PageReference(stCurrentPageURL); 
	}
	else{
		//HERE I NEED TO INSERT NEW CASE
		// query the required fields from the database for the existing case record
		currentCase = [SELECT Id, OwnerId, AccountId FROM Case WHERE Id = :currentCaseId];
		if(currentCase != null){
			// instantiate the new case variable
			newCaseToBeCreated = new Case();
			newCaseToBeCreated.OwnerId = currentCase.OwnerId; // replace with relevant fields here
			newCaseToBeCreated.AccountId = currentCase.AccountId; // replace with relevant fields here
			// insert the new case - insert only if you need other wise do not execute the insert statement
			try{
				insert newCaseToBeCreated;
				pgRef = 'your relevant page reference'; // redirect the user to whatever page you need
			}catch(Exception ex){
				System.debug('#### error while inserting new record #### ' + ex.getMessage())
			}
		}
	}
	return pgRef;
    }
	
    private boolean CheckInput(){ 
	boolean InputValid = false;
	if(inputownername != null && !inputownername.contains('test')){
		InputValid = false;
	}
	else {
	  InputValid = true;
	}
	return InputValid;
    }
}

Please let me know if you have further doubts.

Thanks,
Kaustav
Linda 98Linda 98
Thanks a ton Kaustav.I was able to get this done but the issue now is

1.I am trying to assign the case to queue with certain name.
So i did something liek this

group g= select id from group where name='test';
and newcaseownerid=g.id;
.
.
.
.
.
.then insert newcase;
Ownerid is not being populated.

2.As per my awareness,i think soql is not case sensitive.But it is only checking if i give the exact name.
For instance.:
i gave like this:

TeSt owner

But owner name is like this:  TESt Owner

I get error saying invalid owner name.
It fetcheds record if i give TESt Owner only.
please help !
kaustav goswamikaustav goswami
For the first issue please try using the developer name field.

In salesforce go to setup - Manage Users - Queue - click to view the details of the required queue.
In the details page you will see a field called Queue Name.
Now let us consider the Queue Name value is 'Test Queue'

Using this value form the query like this

Group qGrp = new Group();
gGrp = [SELECT Id, Name FROM Group WHERE Type = 'Queue' AND DeveloperName = 'Test Queue' LIMIT 1];

This should give you the desired queue name.

As for the second issue, you are correct, in salesforce the value in the where clause do not need to match the exact case.

Can you please post your apex code so that we can check if there is anything else that is missing?

Thanks,
Kaustav
Linda 98Linda 98
Thank you but i am able to get queueid..My issue is i am not able to populate ownerid>Posted my code with Comments in UPPERCASE.
I am not able to update The new case owner id to queue id.

case newcase =new case;

newcase.Ownerid=gGrp.id;

insert newcase;

this doesnt work:(

2.I found out my issue...I am able to get my soql but issue is the point where i am checking if my user input exists in the list .

Here is my code:Please help me ASAP..Please..i need this done asap...

public with sharing class caseclass {
    Public string inputownername{get;set;}
    // variable to hold the current case record
    public Case currentCase {get; set;}
    // variable to hold the id of the current case record
    public String currentCaseId {get; set;}
    // this is the new case that will be inserted
    public Case newCase {get; set;}  
    public List<Group> existingqueuenames=[select name from group where name Like 'test%'];
    Public set<String> setofexistingqueuenames= new set<string>(); 
    public caseclass(ApexPages.StandardController Controller) {
    currentCase = (Case)Controller.getRecord();
    currentCaseId = currentCase.Id;
    }
    public PageReference save() {
    PageReference pgRef = null;
  //HERE I AM QUERING QUEUE ID OF QUEUE WHOSE NAME IS SIMILAR TO NAME GIVEN BY USER
     group g =[select  Id from Group where name =:inputownername and type='Queue' limit 1 ];
//I AM GETTING QUEUE ID CORRECTLY
    if(CheckInput() == false){
        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Owner Name Invalid.'));
    }
    else{
        currentCase = [select id,CaseNumber,contactid,Subject,Ownerid,Parentid,Origin,Status,Priority,Type,Accountid from case where Id =:currentCaseId];
        if(currentCase != null){
            newCase = new Case();
            newcase.Contactid=currentCase .Contactid;
            newcase.Origin=currentCase .Origin;
            newcase.Status=currentCase .Status;
            newcase.Priority=currentCase .Priority;
            newcase.Type=currentCase .type;
            newcase.Parentid=currentCase .id;
            newcase.ownerid=g.id;  //DEBUG SHOWS ITS ASSIGNING BUT WHEN I SEE INSERTED RECORD IT DOESNT.
            newcase.Accountid=currentCase .Accountid;
         try{
             insert newCase;
             pgRef =new pagereference ('/'+newcase.id);
            }
            catch(Exception ex){
                System.debug('#### error while inserting new record #### ' + ex.getMessage());
            }
        }
    }
    return pgRef;
    }
    private boolean CheckInput(){
       //HERE I AM ADDING MY LIST OF QUEUE WHICH CONTAINS 'TEST' IN THERE NAME TO A SET
//I AM HAVING 4 QUEUES WITH NAMES 'test 1' ,'test 2', 'test 3','test 4 '  SO MY LIST EXISTINGQUEUENAMES HAS ID AND NAME OF QUEUES.
//THEN HERE I AM ADDING ONLY NAME'S TO SET
          for(Group m :existingqueuenames){ 
          setofexistingqueuenames.add(m.Name);
          }
            boolean InputValid = false;
//HERE I HAVE TO SEE IF QUEUE NAMES CONTAINS INPUTOWNERNAME.IF THEY CONTAIN CREATE A NEW CASE IF NOT THEN SHOW ERROR>
        if(!setofexistingqueuenames.contains(inputownername)){
            InputValid = false;
            }
            else {
            InputValid = true;
            }
            return InputValid;
     }
}








kaustav goswamikaustav goswami
Do you have any active assignment rules for your case object? If yes then can you check by turning that off?
kaustav goswamikaustav goswami
Also make sure that the queue has Case as one of its supported objects.
Linda 98Linda 98
Yes.. case is supported object And how can i solve the case sensitive issue?? How can i check wheteher userinput contains in the list of queues I need to validate that and make sure it accepts all the format for example:if queuename is 'teSt QuEue' in database then it should accept 'test queue' or 'teST QUEUe' Now it only accepts exact match i.e: ''teSt QuEue'
kaustav goswamikaustav goswami
Hi,

I have tried to recreate your requirement in my developer org.

Controller:

public class CaseOwnerAssignmentComm{
    
    Public string inputOwnerName{get;set;}
    // variable to hold the current case record
    public Case currentCase {get; set;}
    // variable to hold the id of the current case record
    public String currentCaseId {get; set;}
    // this is the new case that will be inserted
    public Case newCase {get; set;}
    
    // constructor
    public CaseOwnerAssignmentComm(ApexPages.StandardController Controller) {
        currentCase = (Case)Controller.getRecord();
        currentCaseId = currentCase.Id;
    }
    
    // save method
    public PageReference createCase(){
        PageReference pgRef = null;
        String queueId = '';
        if(inputOwnerName != null){
            List<Group> grp = new List<Group>();
            grp = [SELECT Id, Name FROM Group WHERE Type = 'Queue' AND Name = :inputOwnerName LIMIT 1];
            if(grp != null && grp.size() > 0){
                queueId = grp.get(0).id;
            }
            if(queueId != ''){
                // query the case
                currentCase = [select id,CaseNumber,contactid,Subject,Ownerid,Parentid,Origin,Status,Priority,Type,Accountid from case where Id =:currentCaseId];
                if(currentCase != null){
                    newCase = new Case();
                    //newcase.Contactid=currentCase.Contactid;
                    newcase.Origin='Phone';
                    newcase.Status='New';
                    newcase.Priority='Medium';
                    newcase.Type='Other';
                    //newcase.Parentid=currentCase.id;
                    newcase.ownerid=queueId; 
                    //newcase.Accountid=currentCase.Accountid;
                    try{
                        insert newCase;
                        pgRef = new ApexPages.StandardController(newCase).view();
                        pgRef.setRedirect(true);
                        System.debug('#### redirect url #### ' + pgRef.getUrl());
                    }
                    catch(Exception ex){
                        System.debug('#### error while inserting new record #### ' + ex.getMessage());
                    }
                }
            }else{
                ApexPages.Message msg = new ApexPages.Message(ApexPages.severity.ERROR, 'No matching owner found');
                ApexPages.addMessage(msg);
            }           
        }else{
            ApexPages.Message msg = new ApexPages.Message(ApexPages.severity.ERROR, 'Please provide a owner name');
            ApexPages.addMessage(msg);
        }
        return pgRef;
    }
}

VF Page:
<apex:page id="ownPage" title="Case Owner Page" standardController="Case" extensions="CaseOwnerAssignmentComm">
    <apex:form id="caseOwnForm">
        <apex:pageBlock id="caseOwnPB">
            <apex:pageMessages />
            <apex:pageBlockButtons id="pbButt">
                <apex:commandButton value="Set Owner" action="{!createCase}" />
            </apex:pageBlockButtons>
            <apex:pageBlockSection id="pbSec">
                <apex:pageBlockSectionItem id="secItem">
                    <apex:outputLabel value="Enter Queue Name" for="queueInput" />
                    <apex:inputText value="{!inputOwnerName}" id="queueInput" />
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
This working fine and works independent of the input case. If you do not get help from this then please post both you VF PAGE and controller.

Thanks,
Kaustav