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
CharlieSCharlieS 

problem with Publisher Actions and Apex trigger

Hi,

 

I am planning to use a Publisher Action on Account to create a quick Opportunity.  Everything works OK except....

 

My opportunities have 2 custom lookup fields :

 

second_account__c - lookup on account

prospect_agency__c - also a lookup on account

 

In one business case I need to swap the account and the agency so I have written a trigger :

trigger opptriggertest on Opportunity (after insert) {

    for(Opportunity prospect: Trigger.new)
    {
            if (prospect.prospect_agency__c <> null) {
 		id oldaccountid = prospect.accountid;
                id oldagencyid  = prospect.prospect_agency__c;
                Opportunity updopp=new opportunity (
                         id 		= prospect.id,
                         accountid 	= oldagencyid,
                	 second_account__c  = oldaccountid);
                	 update updopp;
        }
	}
}

The trigger works fine if I manually create an opportunity.

 

But when I am creating the opportunity via a publisher action I get errors like :

 

10:59:57:564 EXCEPTION_THROWN [12]|System.DmlException: Update failed. First exception on row 0 with id 006W0000004LhLhIAK; first error: INVALID_FIELD_FOR_INSERT_UPDATE, Opportunity: bad field names on insert/update call: Second_Account__c: [Second_Account__c]

 

Does anyone have any ideas ?

thanks

Best Answer chosen by Admin (Salesforce Developers) 
CharlieSCharlieS

I raised a support ticket for this issue and we have had some progress...

 

The problem is that when the trigger is firing from a Publisher Action it can only make changes to the exact fields that are on the publisher layout, or in the default values - i.e. it can't update second_account__c because it can't 'see' it.


This works differently when the trigger fires from a normal user interface - in this case the trigger sees all the fields on the object (opportunity) whether or not the field is displayed on the page layout.


So a workaround is to display the field on the publisher layout but to advise users not to use it - in which case the trigger works.

I am hoping for a better workaround...  If I get any progress I will post it here

All Answers

asish1989asish1989

Please make sure that  lookup field present in the Opportunity .

second_account__c
trigger opptriggertest on Opportunity (after insert) {
	List<Opportunity> listOfOpportunity = new List<Opportunity>();;
    for(Opportunity prospect: Trigger.new)
    {
            if (prospect.prospect_agency__c <> null) {
				id oldaccountid = prospect.accountid;
                id oldagencyid  = prospect.prospect_agency__c;
                Opportunity updopp = new opportunity (
                              id  = prospect.id,
                              accountid = oldagencyid,
                	          second_account__c  = oldaccountid);
							  
                	  
			    listOfOpportunity.add(updopp);
	        }
			
	}
	upsert listOfOpportunity;
}

 

CharlieSCharlieS

I raised a support ticket for this issue and we have had some progress...

 

The problem is that when the trigger is firing from a Publisher Action it can only make changes to the exact fields that are on the publisher layout, or in the default values - i.e. it can't update second_account__c because it can't 'see' it.


This works differently when the trigger fires from a normal user interface - in this case the trigger sees all the fields on the object (opportunity) whether or not the field is displayed on the page layout.


So a workaround is to display the field on the publisher layout but to advise users not to use it - in which case the trigger works.

I am hoping for a better workaround...  If I get any progress I will post it here

This was selected as the best answer