• Snita Lal
  • NEWBIE
  • 65 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 10
    Questions
  • 20
    Replies
Hi,

We have created a cutom JavaScript button on Opportunity to tick a check box which kicks off a process. This creates an OCS Contract record and links to the opportunity.

We would like the return URL to redirect to the new related record but we keep getting unidentified indentifier or redirect to the home page.

Our code is below, any help would be very much appreciated! 
{!REQUIRESCRIPT("/soap/ajax/32.0/connection.js")} 

if({!Opportunity.Contract_Ready_to_Convert__c} == false) 
{ 
var c = new sforce.SObject("Opportunity"); 
c.id = "{!Opportunity.Id}"; 
c.Contract_Ready_to_Convert__c = true; 
result = sforce.connection.update([c]); 
if ( result[0].getBoolean( "success" )) 
{ 
window.location.href = ('/'+c.OCS_Contract__c); 
} 
else 
{ 
alert( result[0].errors.message); 
} 
} 
else 
{ 
alert("A contract already exists for this opportunity"); 
}
Thanks,

Snita
 
Fairly new to apex and we are trying to build a mechanism for sending a VF page to specific users that are defined as lookups on the custom object that sends the email.

We have managed to get an invocable method which calls an @future class which builds the attachment from the VF and sends the emai. The reason we have this seperately is the @future allows for the record to be updated and a populated VF page be posted to the email as an attachment.

We start to hit issues when we look to add more variables in the Invocable Method, other than the ID. Specifically trying to access the email address of the users added to the lookups on the record. At the moment the class sends to a static address for testing purpose. Appreciate the documentation for invocable methods highlights this fact.

We have seen examples of Invocable Method Handlers used in combination with an Invocable Method to collect additional variables, however we're unsure how this would work when the invocable method, calls a future class which does all the work. Unsure of how to structure this and any guidance or advice would be gratefully appreciated.

Current code posted below.
public class MakeApexCallout {

  @InvocableMethod
   public static void invokeapexcallout(list<Net_Promoter__c> nps) {
     Futureapexcallout.apexcallout(nps[0].id);
   }
}
 
public class Futureapexcallout{

  @future(Callout=true)
  public static void apexcallout(string Id){
  
  List<String> EmailIds = '******'.split(',');

        pageReference pdfPage = Page.NPS_Survey;
        pdfPage.getParameters().put('id',Id);
        blob b = pdfpage.getContentaspdf(); 

        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();

        Messaging.EmailFileAttachment efa1 = new Messaging.EmailFileAttachment();
        efa1.setFileName('NPS.pdf');
        efa1.setBody(b);

        String addresses;
        email.setSubject( 'Check VF From PB' +String.valueOf(DateTime.now()));
        email.setToAddresses( EmailIds  );
        email.setPlainTextBody('Hi MD, Please see attached the current NPS for ***');
        email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa1});
        Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
    }
}

 
Hi,

We are using the below extension class and visualforce page on Account to use in a custom action to create a record. When using the Salesforce1 app the visualforce page shows in a classic view and not in lighning whilst we are using the new lightningStylesheets="true" and we've ticked the checkbox to make the page available in mobile and lightning.

We've ended up down this route as we wanted the user to be redirected to the record created and not the parent on Salesforce1 because the user is expected to do more functionality on the record once created. Ideally we want to achieve this in the simplest way as we feel the process is over engineered.

Any help would be greatly apprieciated.

Thanks,

Snita
 
public with sharing class OppExtension {

Account acc;

public Operational_Opportunities__c opp{get;set;}

public OppExtension(ApexPages.StandardController controller) {
    this.acc= (Account)controller.getRecord();
    opp = new Operational_Opportunities__c();
}

public PageReference save() {
    opp.account__c= acc.Id;
    insert opp;
    
    PageReference pg = new PageReference('/'+Opp.id);
        pg.setRedirect(true);
        return pg;  
}
}

<apex:page standardController="Account" extensions="OppExtension" lightningStylesheets="true" >
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection > 

  <apex:outputField value="{!Account.Name}"/>
  <apex:inputField value="{!opp.Name}"/>
  <apex:inputField value="{!opp.Type__c}"/>
  <apex:inputField value="{!opp.Stage__c}"/>
  <apex:inputField value="{!opp.Quote_Submission_Date__c}"/>
  <apex:inputField value="{!opp.Close_Date__c}"/>
  <apex:inputField value="{!opp.Service_Start_Date__c}"/>

<apex:commandButton value="Save" Action="{!Save}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

 
Hi,

We've started using quick actions to create Cases and we want an option, like you do in the Case edit screen, to choose whether to assign via assignment rules or not.

The below trigger pushes everything through the assignment rules even if created by the quick action, and due to the fact that I cannot include the assign via assignment rules checkbox on the quick action I have created a field called use_assignment_rules__c. I would therefore like to nest an IF statement into this trigger to only run the trigger when the checkbox is equal to true. Struggling to make this work could anyone advise on how to get the trigger to work, any help would be greatly appreciated.

Thank you,

Snita
 
trigger AssignmentRulesonCaseTrigger on Case (after insert) {
    List<Id> caseIds = new List<Id>{};
        if(trigger.IsAfter && trigger.isInsert){
            for (Case theCase:trigger.new) 
                caseIds.add(theCase.Id);
            
            List<Case> cases = new List<Case>{}; 
            for(Case c : [Select Id from Case where Id in :caseIds])
            {
                Database.DMLOptions dmo = new Database.DMLOptions();
     
                dmo.assignmentRuleHeader.useDefaultRule = true;
                c.setOptions(dmo);
                
                cases.add(c);
            
    }
            Database.upsert(cases);
        }
}

 
Hi,

We've been working on this trigger for a week now and to say its drive us crazy is an understatement. We've been round the housees a little simply on the basis we're trying to expose the line items to a visualforce force.com profile page - If anyone has done this before then they'll appreciate the headache products creates.

We've had several workable inserts that have either repeatedly inserted new lines or that insert without populating the opportunity lookup. We've got to the point that our trigger hits a dml error using the field references we've provided. Assuming that if we collect the opportunity ID from the line item and that this isn't populated prior to insert is why we hit the error. We've seen this done where the ID is insert after the save and therefore bypassing the error but unfortunately this surpasses our knowledge.

Please can someone help and advise the appropriate change in order to fix this?
 
trigger BidProductsInsert on OpportunityLineItem(after insert, after update) {
   
    Map<Id, OpportunityLineItem> opl = new Map<Id, OpportunityLineItem>();
    Map<Id, Opportunity> opps = new Map<Id, Opportunity>();
    Map<Id, PricebookEntry> prices = new Map<Id, PricebookEntry>();
    Map<opportunitylineitem, bid_products__c> lines = new Map<opportunitylineitem, bid_products__c>();

    for(OpportunityLineItem record: Trigger.new) {
        opps.put(record.OpportunityId, null);
        prices.put(record.PricebookEntryId, null);
    }
    
    opl.putAll([SELECT Id, OpportunityID FROM OpportunityLineItem WHERE Id IN :opl.keySet()]);
    prices.putAll([SELECT Id, Product2.Name, Product2Id FROM PricebookEntry WHERE Id IN :prices.keySet()]);
   
    for(OpportunityLineItem record: Trigger.new) {
        lines.put(record,
            new bid_products__c(
                Name='Opp-Product', 
                Product2__c=prices.get(record.PricebookEntryId).Product2Id, 
                Unit_Price__c=record.UnitPrice, 
                Quantity__c=record.Quantity, 
                Product_Name__c=prices.get(record.PricebookEntryId).Product2.Name,   
                Opportunity__c=opl.get(record.OpportunityID).Id,           
                Id=record.Bid_Products__c));
    }
    
    upsert lines.values();

    for(OpportunityLineItem record: Trigger.new) {
        record.bid_products__c = lines.get(record).Id;
    }
}

Thank you,

Snita
Hi,

We've built a controller that we use for our custom object that just allows us to show the content in a visualforce page, slightly different to a standard Salesforce layout. 

Everything seems to work perfectly until we start to introduce lookup fields to the account because the user will always create a new custom object Erad__c from the account the value will always be populated, however when we reference this field just in the same way as a text or number field the value isn't stored. We are relatively new to this sort of thing so any advice that could be offered would be appreciated.

I'm guessing that we need to store the value in the controller correctly and refernce back to the visualforce page but we're unsure on how to exactly go about it.

Visualforce page
<apex:page standardController="Erad__c" extensions="insertnewrecordController">

Hello {!$User.FirstName}!    
   
   <p>You are viewing the Erad model. Please enter the Relationship grade below based on the image and the date your assesment was made.</p>

<apex:form >
<apex:pageBlock >
<apex:pageBlockSection >
<apex:pageBlockSectionItem >
<apex:outputLabel >Account</apex:outputLabel>
<apex:inputField id="Account" value="{!Erad__c.Account__c}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel >Relationship Type</apex:outputLabel>
<apex:inputField id="RelationshipType" value="{!Erad__c.Relationship_Type__c}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel >Date Completed</apex:outputLabel>
<apex:inputField id="DateCompleted" value="{!Erad__c.Date_Completed__c}"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
<apex:pageBlockButtons >
<apex:commandButton id="Save" action="{!Save}" value="Save"/>
<apex:commandButton id="Cancel" action="{!Cancel}" value="Cancel"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
<apex:image url="{!$Resource.Erad}" width="500" height="500"/>
</apex:page>

Controller
 
public class insertnewrecordController{
public erad__c cds{get; set;}
public erad__c setcds(erad__c op){
this.cds=op;
return cds;
}
public insertnewrecordController(ApexPages.StandardController controller) {
cds=new erad__c();
}

public pagereference Save(){

erad__c cd = new erad__c();
cd.Account__c=cds.Account__c;
cd.Relationship_Type__c=cds.Relationship_Type__c;
cd.Date_Completed__c=cds.Date_Completed__c;
insert cd;
Pagereference pg = new Pagereference('/' + cd.id);
pg.setredirect(true);
return pg;


}
 
}

Thanks,

Snita
Hi,

We've been working on a trigger which creates a contract upon an Opportunity being complete and 'Closed Won'. We've managed to get the record to create and copy over important info, although we can't manage to figure the validation logic. Granted we have little apex knowledge and therefore we've attempted to put most of the logic outside the trigger and within a workflowed checkbox on the Opportunity.

However, this don't eliminate the duplication of records if the user edits a Won opportunity. I know the better versed Salesforce developer would have the list check the record doesn't exist in the first place before the trigger even starts and therefore we're reaching out hoping you guys could support us in finishing this piece of work.

Any help or advice would be gratefully appreciated. 
trigger CreateContract on Opportunity (after insert, after update) {

    List<Contract> conttoinsert = new List<Contract>();

    for (Opportunity opp : Trigger.new) {

        if (opp.StageName == 'Closed Won' && opp.RecordTypeID == '012N00000004pSi' && opp.Record_Complete__c == TRUE) {
            Contract con = new Contract();
            con.Name                  = opp.name;
            con.Account               = opp.Account;
            con.CurrencyIsoCode       = opp.CurrencyIsoCode;
            con.AccountId             = opp.AccountId;
            con.StartDate             = opp.Service_Start_Date__c;
            con.Contract_Value__c     = opp.Total_Performance__c;
            con.Description           = opp.Opportunity_Description__c;
            con.Sales_Lead__c         = opp.OwnerID;
            conttoinsert.add(con);
        } 
    }

    if ( !conttoinsert.isEmpty()) {
        insert conttoinsert;

}
}

 
Hi,

We've managed to get the trigger working fine in our production system although our processes have recently changed which means that we only want the trigger to fire when certain conditions are met. Please see below the working trigger with the IF statement placed where we believe it should be however it still fires even when these conditions are met. We've tried a couple times to place the IF statement in other areas and either hit errors and we are a little lost.
 
trigger UpdateAccountLookup on Opportunity (before insert,before update){

Set<id> setSiteids=new set<id>();
map<Id, String> mapsiteIdToopptId = new map<Id, String>();
Map<Id, String> mapOpptIdToAccId = new Map<Id, String>();

list<account> acclst = new list<account>();

    For(Opportunity o : trigger.new){  
       
        setSiteids.add(o.Site_Location__c);
        mapsiteIdToopptId.put(o.Site_Location__c, o.Id);
    }
    
    for(Site__c objSite : [Select Id , Parent_Account__c from Site__c where Id IN :setSiteids ])
    {
        mapOpptIdToAccId.put(mapsiteIdToopptId.get(objSite.Id), objSite.Parent_Account__c);
    }
    
    for(Opportunity o : trigger.new){
    
        if(o.New_Site__c == false  || o.Takeover__c == false){
        
        o.AccountId = mapOpptIdToAccId.get(o.Id);
    }
}
}

Any help would be greatfully appreciated.

Thanks,

Snita

 

Hi

We have a custom lookup field that has been created on the Opportunity Line Item object called Product Owner. We would like this to be auto populated with the current user when left blank upon saving. 

We've tried to create a trigger to do this but run into errors upon saving the product line

trigger updatefield on OpportunityLineItem (after insert) {
  user defaultuser = [select id from user where name = 'default user'];
  for (OpportunityLineItem record:trigger.new) {
    if(record.Product_Owner__c ==null) {
      record.Product_Owner__c = defaultuser.id;
    }
  }
}

Any help or advice would be much appreciated.

Thanks,

Snita

Hi,

We have a fully functional trigger that fires when a new Opportunity is created from our custom objects and the trigger will prepopulate the Account lookup field and avoid the user assigning manually to potential the wrong account. 

Problem arises that we have to disable the required population of the account field on the layout to stop it hitting the validation before saving at which point the trigger will fire.

We have tried to write some validation rules that work tandem with this but we seem to be in a bit of a loop.

Please can you guys maybe suggest the best course of action as without taking a step back I don't see us making much progress.

We do believe that maybe firing the trigger when only a certain action is excuted but we wouldn't know how to go about writing this into our trigger below, your help would be greatfully appreciated and any advice you can offer we would take on board.

Thanks,

Snita

Trigger UpdateAccountLookup on Opportunity (before insert,before update) {

Set<id> setSiteids=new set<id>();
map<Id, String> mapsiteIdToopptId = new map<Id, String>();
Map<Id, String> mapOpptIdToAccId = new Map<Id, String>();


list<account> acclst = new list<account>();

    For(Opportunity o : trigger.new){
        
        setSiteids.add(o.Site_Location__c);
        mapsiteIdToopptId.put(o.Site_Location__c, o.Id);
    }
    
    for(Site__c objSite : [Select Id , Parent_Account__c from Site__c where Id IN :setSiteids ])
    {
        mapOpptIdToAccId.put(mapsiteIdToopptId.get(objSite.Id), objSite.Parent_Account__c);
    }
    
    for(Opportunity o : trigger.new)
    {
        o.AccountId = mapOpptIdToAccId.get(o.Id);
    }
}


 

Hi

We have a custom lookup field that has been created on the Opportunity Line Item object called Product Owner. We would like this to be auto populated with the current user when left blank upon saving. 

We've tried to create a trigger to do this but run into errors upon saving the product line

trigger updatefield on OpportunityLineItem (after insert) {
  user defaultuser = [select id from user where name = 'default user'];
  for (OpportunityLineItem record:trigger.new) {
    if(record.Product_Owner__c ==null) {
      record.Product_Owner__c = defaultuser.id;
    }
  }
}

Any help or advice would be much appreciated.

Thanks,

Snita

Hi,

We are using the below extension class and visualforce page on Account to use in a custom action to create a record. When using the Salesforce1 app the visualforce page shows in a classic view and not in lighning whilst we are using the new lightningStylesheets="true" and we've ticked the checkbox to make the page available in mobile and lightning.

We've ended up down this route as we wanted the user to be redirected to the record created and not the parent on Salesforce1 because the user is expected to do more functionality on the record once created. Ideally we want to achieve this in the simplest way as we feel the process is over engineered.

Any help would be greatly apprieciated.

Thanks,

Snita
 
public with sharing class OppExtension {

Account acc;

public Operational_Opportunities__c opp{get;set;}

public OppExtension(ApexPages.StandardController controller) {
    this.acc= (Account)controller.getRecord();
    opp = new Operational_Opportunities__c();
}

public PageReference save() {
    opp.account__c= acc.Id;
    insert opp;
    
    PageReference pg = new PageReference('/'+Opp.id);
        pg.setRedirect(true);
        return pg;  
}
}

<apex:page standardController="Account" extensions="OppExtension" lightningStylesheets="true" >
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection > 

  <apex:outputField value="{!Account.Name}"/>
  <apex:inputField value="{!opp.Name}"/>
  <apex:inputField value="{!opp.Type__c}"/>
  <apex:inputField value="{!opp.Stage__c}"/>
  <apex:inputField value="{!opp.Quote_Submission_Date__c}"/>
  <apex:inputField value="{!opp.Close_Date__c}"/>
  <apex:inputField value="{!opp.Service_Start_Date__c}"/>

<apex:commandButton value="Save" Action="{!Save}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

 
Hi,

We've been working on this trigger for a week now and to say its drive us crazy is an understatement. We've been round the housees a little simply on the basis we're trying to expose the line items to a visualforce force.com profile page - If anyone has done this before then they'll appreciate the headache products creates.

We've had several workable inserts that have either repeatedly inserted new lines or that insert without populating the opportunity lookup. We've got to the point that our trigger hits a dml error using the field references we've provided. Assuming that if we collect the opportunity ID from the line item and that this isn't populated prior to insert is why we hit the error. We've seen this done where the ID is insert after the save and therefore bypassing the error but unfortunately this surpasses our knowledge.

Please can someone help and advise the appropriate change in order to fix this?
 
trigger BidProductsInsert on OpportunityLineItem(after insert, after update) {
   
    Map<Id, OpportunityLineItem> opl = new Map<Id, OpportunityLineItem>();
    Map<Id, Opportunity> opps = new Map<Id, Opportunity>();
    Map<Id, PricebookEntry> prices = new Map<Id, PricebookEntry>();
    Map<opportunitylineitem, bid_products__c> lines = new Map<opportunitylineitem, bid_products__c>();

    for(OpportunityLineItem record: Trigger.new) {
        opps.put(record.OpportunityId, null);
        prices.put(record.PricebookEntryId, null);
    }
    
    opl.putAll([SELECT Id, OpportunityID FROM OpportunityLineItem WHERE Id IN :opl.keySet()]);
    prices.putAll([SELECT Id, Product2.Name, Product2Id FROM PricebookEntry WHERE Id IN :prices.keySet()]);
   
    for(OpportunityLineItem record: Trigger.new) {
        lines.put(record,
            new bid_products__c(
                Name='Opp-Product', 
                Product2__c=prices.get(record.PricebookEntryId).Product2Id, 
                Unit_Price__c=record.UnitPrice, 
                Quantity__c=record.Quantity, 
                Product_Name__c=prices.get(record.PricebookEntryId).Product2.Name,   
                Opportunity__c=opl.get(record.OpportunityID).Id,           
                Id=record.Bid_Products__c));
    }
    
    upsert lines.values();

    for(OpportunityLineItem record: Trigger.new) {
        record.bid_products__c = lines.get(record).Id;
    }
}

Thank you,

Snita
Hi,

We've built a controller that we use for our custom object that just allows us to show the content in a visualforce page, slightly different to a standard Salesforce layout. 

Everything seems to work perfectly until we start to introduce lookup fields to the account because the user will always create a new custom object Erad__c from the account the value will always be populated, however when we reference this field just in the same way as a text or number field the value isn't stored. We are relatively new to this sort of thing so any advice that could be offered would be appreciated.

I'm guessing that we need to store the value in the controller correctly and refernce back to the visualforce page but we're unsure on how to exactly go about it.

Visualforce page
<apex:page standardController="Erad__c" extensions="insertnewrecordController">

Hello {!$User.FirstName}!    
   
   <p>You are viewing the Erad model. Please enter the Relationship grade below based on the image and the date your assesment was made.</p>

<apex:form >
<apex:pageBlock >
<apex:pageBlockSection >
<apex:pageBlockSectionItem >
<apex:outputLabel >Account</apex:outputLabel>
<apex:inputField id="Account" value="{!Erad__c.Account__c}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel >Relationship Type</apex:outputLabel>
<apex:inputField id="RelationshipType" value="{!Erad__c.Relationship_Type__c}"/>
</apex:pageBlockSectionItem>
<apex:pageBlockSectionItem >
<apex:outputLabel >Date Completed</apex:outputLabel>
<apex:inputField id="DateCompleted" value="{!Erad__c.Date_Completed__c}"/>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
<apex:pageBlockButtons >
<apex:commandButton id="Save" action="{!Save}" value="Save"/>
<apex:commandButton id="Cancel" action="{!Cancel}" value="Cancel"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
<apex:image url="{!$Resource.Erad}" width="500" height="500"/>
</apex:page>

Controller
 
public class insertnewrecordController{
public erad__c cds{get; set;}
public erad__c setcds(erad__c op){
this.cds=op;
return cds;
}
public insertnewrecordController(ApexPages.StandardController controller) {
cds=new erad__c();
}

public pagereference Save(){

erad__c cd = new erad__c();
cd.Account__c=cds.Account__c;
cd.Relationship_Type__c=cds.Relationship_Type__c;
cd.Date_Completed__c=cds.Date_Completed__c;
insert cd;
Pagereference pg = new Pagereference('/' + cd.id);
pg.setredirect(true);
return pg;


}
 
}

Thanks,

Snita
Hi,

We've been working on a trigger which creates a contract upon an Opportunity being complete and 'Closed Won'. We've managed to get the record to create and copy over important info, although we can't manage to figure the validation logic. Granted we have little apex knowledge and therefore we've attempted to put most of the logic outside the trigger and within a workflowed checkbox on the Opportunity.

However, this don't eliminate the duplication of records if the user edits a Won opportunity. I know the better versed Salesforce developer would have the list check the record doesn't exist in the first place before the trigger even starts and therefore we're reaching out hoping you guys could support us in finishing this piece of work.

Any help or advice would be gratefully appreciated. 
trigger CreateContract on Opportunity (after insert, after update) {

    List<Contract> conttoinsert = new List<Contract>();

    for (Opportunity opp : Trigger.new) {

        if (opp.StageName == 'Closed Won' && opp.RecordTypeID == '012N00000004pSi' && opp.Record_Complete__c == TRUE) {
            Contract con = new Contract();
            con.Name                  = opp.name;
            con.Account               = opp.Account;
            con.CurrencyIsoCode       = opp.CurrencyIsoCode;
            con.AccountId             = opp.AccountId;
            con.StartDate             = opp.Service_Start_Date__c;
            con.Contract_Value__c     = opp.Total_Performance__c;
            con.Description           = opp.Opportunity_Description__c;
            con.Sales_Lead__c         = opp.OwnerID;
            conttoinsert.add(con);
        } 
    }

    if ( !conttoinsert.isEmpty()) {
        insert conttoinsert;

}
}

 
Hi,

We've managed to get the trigger working fine in our production system although our processes have recently changed which means that we only want the trigger to fire when certain conditions are met. Please see below the working trigger with the IF statement placed where we believe it should be however it still fires even when these conditions are met. We've tried a couple times to place the IF statement in other areas and either hit errors and we are a little lost.
 
trigger UpdateAccountLookup on Opportunity (before insert,before update){

Set<id> setSiteids=new set<id>();
map<Id, String> mapsiteIdToopptId = new map<Id, String>();
Map<Id, String> mapOpptIdToAccId = new Map<Id, String>();

list<account> acclst = new list<account>();

    For(Opportunity o : trigger.new){  
       
        setSiteids.add(o.Site_Location__c);
        mapsiteIdToopptId.put(o.Site_Location__c, o.Id);
    }
    
    for(Site__c objSite : [Select Id , Parent_Account__c from Site__c where Id IN :setSiteids ])
    {
        mapOpptIdToAccId.put(mapsiteIdToopptId.get(objSite.Id), objSite.Parent_Account__c);
    }
    
    for(Opportunity o : trigger.new){
    
        if(o.New_Site__c == false  || o.Takeover__c == false){
        
        o.AccountId = mapOpptIdToAccId.get(o.Id);
    }
}
}

Any help would be greatfully appreciated.

Thanks,

Snita

 

Hi

We have a custom lookup field that has been created on the Opportunity Line Item object called Product Owner. We would like this to be auto populated with the current user when left blank upon saving. 

We've tried to create a trigger to do this but run into errors upon saving the product line

trigger updatefield on OpportunityLineItem (after insert) {
  user defaultuser = [select id from user where name = 'default user'];
  for (OpportunityLineItem record:trigger.new) {
    if(record.Product_Owner__c ==null) {
      record.Product_Owner__c = defaultuser.id;
    }
  }
}

Any help or advice would be much appreciated.

Thanks,

Snita

Hi,

We have a fully functional trigger that fires when a new Opportunity is created from our custom objects and the trigger will prepopulate the Account lookup field and avoid the user assigning manually to potential the wrong account. 

Problem arises that we have to disable the required population of the account field on the layout to stop it hitting the validation before saving at which point the trigger will fire.

We have tried to write some validation rules that work tandem with this but we seem to be in a bit of a loop.

Please can you guys maybe suggest the best course of action as without taking a step back I don't see us making much progress.

We do believe that maybe firing the trigger when only a certain action is excuted but we wouldn't know how to go about writing this into our trigger below, your help would be greatfully appreciated and any advice you can offer we would take on board.

Thanks,

Snita

Trigger UpdateAccountLookup on Opportunity (before insert,before update) {

Set<id> setSiteids=new set<id>();
map<Id, String> mapsiteIdToopptId = new map<Id, String>();
Map<Id, String> mapOpptIdToAccId = new Map<Id, String>();


list<account> acclst = new list<account>();

    For(Opportunity o : trigger.new){
        
        setSiteids.add(o.Site_Location__c);
        mapsiteIdToopptId.put(o.Site_Location__c, o.Id);
    }
    
    for(Site__c objSite : [Select Id , Parent_Account__c from Site__c where Id IN :setSiteids ])
    {
        mapOpptIdToAccId.put(mapsiteIdToopptId.get(objSite.Id), objSite.Parent_Account__c);
    }
    
    for(Opportunity o : trigger.new)
    {
        o.AccountId = mapOpptIdToAccId.get(o.Id);
    }
}