• Vic Prabhu
  • NEWBIE
  • 0 Points
  • Member since 2014
  • Salesforce Administrator

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 3
    Replies
Hello,

I have an Apex Trigger that excutes whenever a Product is (Inserted/Edited/Deleted) from OpportunityLineItem. The trigger needs to check a picklist value from a custom picklist in Opportunity Object. The code in the trigger will execute if there is matching value, else do nothing. I have the trigger written as follows. I'm not getting any error, the trigger excutes the desired code without checking the IF Condition.

Apex Trigger:

 trigger OppAmountTotal on OpportunityLineItem (after delete, after insert, after update, after undelete) {
    OpportunityLineItem oli = [Select OpportunityId from OpportunityLineItem LIMIT 1];
    Opportunity o = [SELECT CustomPicklist FROM Opportunity WHERE Id = :oli.OpportunityId];
    if(o.CustomPicklist == 'Yes - Add to Total'){
        if(trigger.isInsert || trigger.isUpdate || trigger.isUnDelete){
            list<TotalAmount.fieldDefinition> fieldDefinitions =
            new list<TotalAmount.fieldDefinition> {
                new TotalAmount.fieldDefinition('SUM', 'TotalPrice',
                'Amount')
            };
            TotalAmount.rollUpTrigger(fieldDefinitions, trigger.new,
            'OpportunityLineItem', 'OpportunityId', 'Opportunity', '');
        }
        if(trigger.isDelete){
            list<TotalAmount.fieldDefinition> fieldDefinitions =
            new list<TotalAmount.fieldDefinition> {
                new TotalAmount.fieldDefinition('SUM', 'TotalPrice',
                'Amount')
            };
            TotalAmount.rollUpTrigger(fieldDefinitions, trigger.old,
            'OpportunityLineItem', 'OpportunityId', 'Opportunity', '');
        }
    }
}
Hi All,

I have the following requirement. When cutom button is clicked on Opportunity detail page, i need to update the schedule date for all products associated with that opportunity to match Opportunity close date.

Below is my code, i'm getting Script error on button click, what am i missing?

Button Click Javascript:

{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")}

// Call Apex Web service

var result = sforce.apex.execute(
"ProductScheduleDateUpdate", // class
"updateProductDate", // method
{iDate:"{!Opportunity.CloseDate}", // method arguments
iOppId:"{!Opportunity.Id}"});

}


Apex Class code

global class ProductScheduleDateUpdate
{

    WebService static String updateProductDate(Date iDate, String iOppId){
        List<OpportunityLineItem> oliList = [SELECT Id, OpportunityId FROM OpportunityLineItem
                                        WHERE OpportunityId= : iOppId];

        if(oliList.size() == 0){
            return 'No Opportunity Products to Update';
        }

        List<OpportunityLineItemSchedule> olisList = new List<OpportunityLineItemSchedule>();
        Map<Id, String> oppLineItemIdIdOppIdMap = new Map<Id, String>(); // it is for setting opportunity access level

       
        for(OpportunityLineItemSchedule olis : [SELECT Id, ScheduleDate, OpportunityLineItemId
                                                FROM OpportunityLineItemSchedule
                                                WHERE OpportunityLineItemId IN : oppLineItemIdIdOppIdMap.keySet()]) {
            olis.ScheduleDate = iDate;
            olisList.add(olis);
        }
       
        if(olisList.size() > 0){
            update olisList;
        }
   
        return 'All Product Schedule Date updated to match Opportunity Close Date';
    }
}
Hello,

I have an Apex Trigger that excutes whenever a Product is (Inserted/Edited/Deleted) from OpportunityLineItem. The trigger needs to check a picklist value from a custom picklist in Opportunity Object. The code in the trigger will execute if there is matching value, else do nothing. I have the trigger written as follows. I'm not getting any error, the trigger excutes the desired code without checking the IF Condition.

Apex Trigger:

 trigger OppAmountTotal on OpportunityLineItem (after delete, after insert, after update, after undelete) {
    OpportunityLineItem oli = [Select OpportunityId from OpportunityLineItem LIMIT 1];
    Opportunity o = [SELECT CustomPicklist FROM Opportunity WHERE Id = :oli.OpportunityId];
    if(o.CustomPicklist == 'Yes - Add to Total'){
        if(trigger.isInsert || trigger.isUpdate || trigger.isUnDelete){
            list<TotalAmount.fieldDefinition> fieldDefinitions =
            new list<TotalAmount.fieldDefinition> {
                new TotalAmount.fieldDefinition('SUM', 'TotalPrice',
                'Amount')
            };
            TotalAmount.rollUpTrigger(fieldDefinitions, trigger.new,
            'OpportunityLineItem', 'OpportunityId', 'Opportunity', '');
        }
        if(trigger.isDelete){
            list<TotalAmount.fieldDefinition> fieldDefinitions =
            new list<TotalAmount.fieldDefinition> {
                new TotalAmount.fieldDefinition('SUM', 'TotalPrice',
                'Amount')
            };
            TotalAmount.rollUpTrigger(fieldDefinitions, trigger.old,
            'OpportunityLineItem', 'OpportunityId', 'Opportunity', '');
        }
    }
}
Hi All,

I have the following requirement. When cutom button is clicked on Opportunity detail page, i need to update the schedule date for all products associated with that opportunity to match Opportunity close date.

Below is my code, i'm getting Script error on button click, what am i missing?

Button Click Javascript:

{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")}

// Call Apex Web service

var result = sforce.apex.execute(
"ProductScheduleDateUpdate", // class
"updateProductDate", // method
{iDate:"{!Opportunity.CloseDate}", // method arguments
iOppId:"{!Opportunity.Id}"});

}


Apex Class code

global class ProductScheduleDateUpdate
{

    WebService static String updateProductDate(Date iDate, String iOppId){
        List<OpportunityLineItem> oliList = [SELECT Id, OpportunityId FROM OpportunityLineItem
                                        WHERE OpportunityId= : iOppId];

        if(oliList.size() == 0){
            return 'No Opportunity Products to Update';
        }

        List<OpportunityLineItemSchedule> olisList = new List<OpportunityLineItemSchedule>();
        Map<Id, String> oppLineItemIdIdOppIdMap = new Map<Id, String>(); // it is for setting opportunity access level

       
        for(OpportunityLineItemSchedule olis : [SELECT Id, ScheduleDate, OpportunityLineItemId
                                                FROM OpportunityLineItemSchedule
                                                WHERE OpportunityLineItemId IN : oppLineItemIdIdOppIdMap.keySet()]) {
            olis.ScheduleDate = iDate;
            olisList.add(olis);
        }
       
        if(olisList.size() > 0){
            update olisList;
        }
   
        return 'All Product Schedule Date updated to match Opportunity Close Date';
    }
}