+ Start a Discussion
Michael PuglieseMichael Pugliese 

Custom Web2Case Issues

We're having an issue with a custom Web2Case visualforce page and apex class. When cases are submitted, an error is displayed:
"List has no rows for assignment to SObject", this error triggers a custom label indicating the case wasn't submitted. After testing, it looks like the case is created regardless of the error, but it's confusing our users who assume it was not. 
public with sharing class Web2CaseCont {
    
    public Case c { get; set; }
    public Boolean isSubmitted {get; private set;}
    public String msg {get; private set;}
    public String isDebug {get; private set;} 
    public Attachment att {get {
        if (att == null)
            att = new Attachment();
        return att;
    }
    set;}
         
    public Web2CaseCont() {
        this.isSubmitted = false;
        c = new Case();
        AssignFromParameters();
    }
    
    public PageReference submitCase() {
        try{
            c.SuppliedName = c.SuppliedFirstName__c + ' ' + c.SuppliedLastName__c;
            List<Contact> cnt = [Select Id, Account.Id from Contact where Email =: c.SuppliedEmail LIMIT 10];
            if((!cnt.isEmpty()) && (cnt.size() == 1)){
                c.ContactId = cnt.get(0).Id;
                c.AccountId = cnt.get(0).Account.Id;
            }
            
            // Specify DML options to ensure the assignment rules are executed
            Database.DMLOptions dmlOpts = new Database.DMLOptions();
            dmlOpts.assignmentRuleHeader.useDefaultRule = true;
            dmlOpts.EmailHeader.triggerAutoResponseEmail = true;
            c.setOptions(dmlOpts);
            
            INSERT c;
            this.isSubmitted = true;
            case c2 = [select CaseNumber from case where Id =: c.Id];
            msg = System.label.WebCaseSuccess + ' ' + c2.CaseNumber+'.  ';
            AddAttachment();
        }catch (Exception e){
          ApexPages.addMessages(e);
          msg = System.label.WebCaseFailure;
          return null;
        }
        return null;     
    }
    
    public void AssignFromParameters(){
        Map<string,string> pageParameters = ApexPages.currentPage().getParameters();
        c.SuppliedCompany = pageParameters.get('co');
        c.Product_Line__c = pageParameters.get('pline');
        c.Origin = pageParameters.get('origin');
        isDebug = pageParameters.get('debug');
        if ((isDebug == null) || (isDebug.toLowerCase() != 'true'))
            isDebug = 'false';
        if(c.Origin == null)
            c.Origin = 'Web';
    }
    
    public void AddAttachment(){
        if((att.Name !=null) && (c.Id != null)){
            att.ParentId = c.Id;
            insert(att);
            msg = msg + '<br/> The attachment "' + att.Name + '" uploaded successfully.';
            att = null; //set to null or viewstate will error.  
            }  
    }
}

I can see where the error is displayed, but I have limited apex knowledge so having trouble figuring out what's causing the issue. Any information would be a great start!

Thanks, 

Michael Pugliese
Harish RamachandruniHarish Ramachandruni
Hi ,


Can check With out sharing  in class .


Regards ,
Harish.R.
 
Alain CabonAlain Cabon
Hello Michael,

INSERT c;
this.isSubmitted = true;
case c2 = [select CaseNumber from case where Id =: c.Id];

Can you confirm that your problem is triggered by the line 37?


As you have done the insert of c just above, the error line 37 is not logical. With the case, you have to read as you have done. All is correct.
I had a doubt with the automatic property but that works and that is not reason of the problem.

Regards
Alain