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
KyoKyo 

I want Copy Data from Web-to-case Field but error

I want copy data from Web-to-case field SuppliedCompany put in Account_Name__c. But it's error!.

 Error: Invalid Data.

 Review all error messages below to correct your data.

 Apex trigger UpdateType caused an unexpected exception, contact your administrator: UpdateType: execution of AfterInsert caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.UpdateType: line 5, column 127

 

 

trigger UpdateType on Case(after insert,after update) {
    for(Case C:trigger.new){
    
    if(C.Origin == 'Web'){
        Case[] CA = [Select Id,SuppliedCompany,SuppliedEmail,SuppliedName,SuppliedPhone,Account_Name__c from Case Where id = :ApexPages.currentPage().getParameters().get('id') ];
        for(Case Cased:CA){
        //Cased.Contact= C.SuppliedName;
        Cased.Account_Name__c = C.SuppliedCompany;
        //Cased.ContactEmail = C.SuppliedEmail;
        //Cased.ContactPhone = C.SuppliedPhone;
        }
        insert CA;
        update CA;
        }  
        }
    }

 

 

I want copy data from Web-to-case field SuppliedName type Text put in Case.Contact type lookup field. But error!  

Error: Compile Error: Illegal assignment from String to SOBJECT:Contact at line 7 column 9 Cased.Contact= C.SuppliedName;

 

 

Cased.Contact= C.SuppliedName;

 

 

Thank you so much.

BrianWKBrianWK

Kyo,

 

You have a few issues with your trigger.

 

To answer your first question - I think your issue is with your SoQL to get a list of Cases.

 

You're using ApexPages to get the ID from the page. I'm pretty sure ApexPages can be used in Controller methods with visualforce and not in Apex Triggers.

 

Since there's no page (to the perspective of the trigger) it returns a null and causes your error in the SoQL.

 

Other issues you have here.

 

You're looping through all Cases that have been inserted or updated. That's your For Case c : trigger.new line.

 

Within that loop you're doing a SoQL query. This is not a good idea since you can very easily surpass govenor limits and cause errors to your code.

 

More to the point I'm not sure what you're trying to do here. It looks like you're trying to update a Case record from data from the very same case record.

 

What are you attempting to do? What's your use case?

 

KyoKyo

Thank you BrianWK

I want to take information from the Web-to-Case from Field Web Name put in Field Contact in Case. When the case was created by a web-to-case.

 

 

 

 

Ritesh AswaneyRitesh Aswaney

Adding to  Brian, you dont want to be using ApexPages in a trigger, there s no page context to a trigger.

 

SInce you're trying to update records which invoked the trigger, you're best using a before trigger

 

eg.

 

 

trigger UpdateType on Case(before insert,before update) {
    for(Case C:trigger.new){
    
    if(C.Origin == 'Web')
        C.Account_Name__c = C.SuppliedCompany;
        
    }
No SOQL query is needed unless you're trying to query and or/contact and trying to match based on a field such as name.