1

My requirement is I have to create 2 new fields on Lead object 'IsTopLevelAccount__c'(Checkbox field) and 'Parent__c '(Lookup field on account). I map 'IsTopLevelAccount__c' of lead with 'IsTopLevelAccount__c' of account using standard mapping and via apex trigger I map 'Parent__c ' with the standard field 'Parent' of the account which is parent account field. There is an existing validation on the account object as follow:

OR(AND( 
    $User.BypassValidation__c = FALSE,
     OR( 
     ISPICKVAL( ProposedAccountType__c , "Customer" ), 
     ISPICKVAL( ProposedAccountType__c , "" ) 
     ), 
     IsTopLevelAccount__c = FALSE, 
    (ISBLANK( Parent.Id ))
    ),
    AND( 
    $User.BypassValidation__c = FALSE,
     OR( 
     ISPICKVAL( ProposedAccountType__c , "Customer" ), 
     ISPICKVAL( ProposedAccountType__c , "" ) 
     ), 
     IsTopLevelAccount__c = TRUE, 
     NOT((ISBLANK( Parent.Id ))) 
    )
    )

This validation checks during the account creation,either I mark as a top level account for this I set IsTopLevelAccount__c = TRUE or I can add the parent account. On lead object I have created 'Parent__c ' field which is a lookup field and as I can not map lookup field with the standard account field,so I user trigger on account object(afterUpdate) which maps 'Parent__c' with 'Parent' which is standrd field of account as I mentioned already. This validation is working fine on Account object but when in lead conversion when I select an account in 'Parent__c' field the it always throw me the validation error even if I provide the value in Parent__c.In logs I found that it always gives me Parent.Id as null not sure why? but weird thing is if I user ISNULL(Parent.Id) then validation is pass during the lead conversion but on the account object it throws the validation error again not sure why?

This is the weird behavior and I am nopt getting any clue how to solve this.I tried to use OR((ISBLANK( Parent.Id ),(ISNULL( Parent.Id )) but still it is not working. Can anybody suggest why this is happening??

enter image description here

enter image description here

enter image description here

10
  • Validation rules run before your after update trigger on Account
    – cropredy
    Jul 13, 2016 at 13:53
  • @cropredy ok.But I am trying to add the custom field there in order to bypass ISNULL(Parent.id) like OR(ISBLANK( Parent.Id ),AccountValidationCheck__c=true) AccountValidationCheck__c is a custom formula field on lead object that maps with 'AccountValidationCheck__c' of account in this case it should consider ISNULL(Parent.id) but still it returens false there.Any suggession
    – SFDCDJ
    Jul 13, 2016 at 14:09
  • 3
    user33671 - your question is extremely hard to follow and I would suggest you include a example of the data values as they exist on the Lead, and how they are mapped to Account - but not in paragraph form - use some graphical form
    – cropredy
    Jul 13, 2016 at 14:34
  • @cropredy Lead.IsTopLevelAccount__c ---->Mapped to ----->Account.IsTopLevelAccount__c via standard mapping Lead.Parent__c---->Mapped to ----->Account.ParentId(Parent) via trigger.trigger is on lead on (afterupdate) e.g.Create a lead 'Test' and add the Account 'Acc1' in 'Parent__c' when lead is converted into account it should map 'acc1' automatically on account but before it map,it always throws the validation error.
    – SFDCDJ
    Jul 13, 2016 at 14:40
  • I dont know the option how to provide screenshot here
    – SFDCDJ
    Jul 13, 2016 at 14:40

1 Answer 1

2

@user33671

It looks like you are running afoul of the Order of Execution

When Lead Conversion occurs, SFDC will

  • Insert an Account with values mapped per Lead Mapping rules
  • Run Before insert trigger on Account
  • Run Validation Rules on Account
  • Run after insert trigger on Account
  • ... as per rest of Order of Execution

In your OP, you state you are using an after update trigger. As you can see from the Order of Execution, you have to make the Account look valid before the Validation Rules run and the best way to do that is with a before insert trigger.

You are going to need to map Lead.ParentId__c field (type Text) to some Account.ParentIdFromLeadConvert__c field (type Text) in the Lead Mapping rules. Then, your before insert trigger on Account can simply assign the value from Account.ParentIdFromLeadConvert__c to Account.Parent__c.

But Lead.ParentID can't be a formula field as formula fields can't be "Lead" mapped. You'll need to set it with either a before insert/update trigger on Lead or a Workflow/Process Builder Field Update (using the value of Lead.Parent__c as the source for the assignment).

4
  • I am using AfterUpdate trigger on Lead not on Account.are you suggesting to use before insert on account? I have added the code that I have written.Please check..
    – SFDCDJ
    Jul 14, 2016 at 6:45
  • Lead.ParentId__c that i have used here is the formula field of type text which stores the value of Parent__c(Parent Account) which is a lookup field and I have already mapped this field with ConvertedAccountId__c of account.if I assign the value ConvertedAccountId__c(Text) to ParentId of account on before trigger,will it assign?
    – SFDCDJ
    Jul 14, 2016 at 7:03
  • @user33671 If I assign the value ConvertedAccountId__c(Text) to ParentId of account on before trigger,will it assign? -- I don't see why not, in apex, you can assign strings to ID fields.
    – cropredy
    Jul 14, 2016 at 13:20
  • Thank you somuch I have added small code on account before insert trigger and it is working perfectly fine no weird validation error come.Thank You so much for your help.
    – SFDCDJ
    Jul 14, 2016 at 13:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .