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
Deepika GulatiDeepika Gulati 

Alert Box for Different error

hi,

 

Following is the Controller and VF page Code:

 

public class ParentChildActivity{
public final task t1;
public final task o;
public String trn;
public String taskIdDemo;
public String ActivityTRN{get;set;}
public Boolean errorAlert{get; set;}
public Integer flag{get; set;}
List<Task> taskObjects=new List<Task>();

public ParentChildActivity(ApexPages.StandardController stdController)
    {   
        this.o= (Task)stdController.getRecord();
        taskIdDemo=o.Id;
       
       t1=[Select Activity_TRN__c,Id from Task where id=:o.Id];
        System.debug('Task Id is '+ t1.Id);

    }
public void settrn(String xTRN)
    {
        trn=xTRN;
    }
public String gettrn()
    {
    return trn;
    }
public PageReference searchAndUpdate()
    {
        for(Task t: [Select Id,Activity_TRN__c,Parent_Activity_TRN__c from Task where Activity_TRN__c=:TRN])
        {
            System.debug('Task Id is in SearchAndUpdate'+t.Id);
            System.debug('Activity TRN is'+t.Activity_TRN__c);
            System.debug('Parent Activity'+t.Parent_Activity_TRN__c);
            if(t1.Id!=t.Id && t1.Activity_TRN__c==t.Parent_Activity_TRN__c)
            {
            flag=2;
            }
            if(t1.Id!=t.Id && t1.Activity_TRN__c!=t.Parent_Activity_TRN__c)
            {
            taskObjects.add(t);
            }
         }
        System.debug('Task Objects is '+ taskObjects);
        if(taskObjects.size()>0)
        {
        flag=1;
        errorAlert=true;
            for(Task t:[Select Id,Activity_TRN__c from task where Id in: taskObjects])
            {
                System.debug('Activity TRN is'+t.Activity_TRN__c);
                t1.Parent_Activity_TRN__c=t.Activity_TRN__c;
                t1.Parent_Activity_TaskId__c=t.Id;
                t1.SalesForceURL__c=URL.getSalesforceBaseUrl().toExternalForm()+'/'+t.id;
                System.debug('Salesforce URL is'+t1.SalesForceURL__c);
                update(t1);
             }
         }
         else
         {
             flag=3;
             errorAlert=false;             
             return null;
          }
     PageReference pageRef = new PageReference('/' + taskIdDemo);           
     pageRef.setRedirect(true);     
     return pageRef;
       }
}

 

 

<apex:page standardController="Task" extensions="ParentChildActivity"  >
    <script type="text/javascript">
      function closeWindow()
        {
          if({!flag==1})
            {
            opener.location.reload();
            window.close();
            }
            else if({!flag==2})           
            {
            alert(' it is the Child of this Task  ');
            }

           else if({!flag==3})

           {

           alert('Invalid TRN');

           }
          }
     </script>

    <apex:form id="form1" >
        <apex:pageBlock id="pb1" >

            <apex:pageBlockSection id="pbs1" collapsible="true" columns="4">
                   <apex:outputLabel value="Activity TRN"></apex:outputLabel>
                  <apex:inputText value="{!trn}"/>
                  <apex:commandButton oncomplete="closeWindow()" value="GO" action="{!searchAndUpdate}" />
                  <apex:outputLink value="/{!$CurrentPage.parameters.id}"><font color="red"><b> Cancel </b></font></apex:outputLink>            
            </apex:pageBlockSection>
        </apex:pageBlock>

    </apex:form>

    <apex:detail subject="{!task.Parent_Activity_TaskId__c}" relatedList="false" />

</apex:page>

 

 

 

 

 

I want to display different error messages for different scenarios:

The Java Script code is not giving me alert box. Can someone Please Help me with this.

Best Answer chosen by Admin (Salesforce Developers) 
kiranmutturukiranmutturu

try to replace the hai with alert('{!flag}'); and check what it is giving

All Answers

kiranmutturukiranmutturu

first put an alert inside the fuction first line itself and check whether it is entering or not...

then try the changes in the red color

 

<script type="text/javascript">
function closeWindow()
{

alert('hai');
if('{!flag}'==1)
{
opener.location.reload();
window.close();
}
else if('{!flag}'==2)
{
alert(' it is the Child of this Task ');
}
else if('{!flag}'==3)
{
alert('Invalid TRN');
}
}
</script>

Deepika GulatiDeepika Gulati

Hello  kiran_mutturu,

 

Thanks For the Help.

 

I tried implementing it , It is showing me the alert box 'hai' but not showing the Alert boxes with Error messages.

 

Can you Please help me with JavaScript Syntax.

 

Thank you.

 

 

 

kiranmutturukiranmutturu

try to replace the hai with alert('{!flag}'); and check what it is giving

This was selected as the best answer
Deepika GulatiDeepika Gulati

Hi,

 

It is Giving Blank.

 

Can we accept Integer values from Controller in JavaScript??

 

Thanks

kiranmutturukiranmutturu

ya what ever value u are getting in javascript u need to assign it to var only na....it will take care abt the type of the variable...u need to set the value in the controller properly .. 

Deepika GulatiDeepika Gulati

I tried doing it this Way.

 

var myVar='{!flag}';
            alert(myVar);

 

 

It is again giving a Blank Value.

 

 

Please Help me with This.

 

Thanks.

 

kiranmutturukiranmutturu

i think the flag value is not set in the controller itself..in the command button fuction..put the debug to see the flag value is set or not in the class and chek once....but from ur code when u click on GO it will first execute the javascript ....at that moment flag will always be null only....in the execution of the function u are setting the flag...

kiranmutturukiranmutturu

in this scenario u may need to use remoteaction..check it out here